Forums > MaxMSP

Round a float

Feb 24 2008 | 4:28 pm

could you tell me please, how to round a float which has 5 numbers after the dot (5 decimals?) to a number that has only 2 decimals?

like

0.12345465

to

0.12

Any help appreciated!

thanks

tEd

Feb 24 2008 | 5:22 pm

Feb 24 2008 | 5:40 pm

Julien,

I miss my math teacher from ground school:)

thank you!

tEd

Feb 24 2008 | 5:41 pm

round up

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 267 224 35 196617 * 10.;
#P newex 246 199 31 196617 t 5 i;
#P newex 246 249 31 196617 / 1.;
#P newex 149 159 27 196617 + 0.5;
#P number 236 112 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 236 174 35 196617 pow;
#P newex 236 149 35 196617 t 10 i;
#P message 149 101 55 196617 0.123456;
#P flonum 149 331 116 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 149 299 34 196617 / 10.;
#P newex 149 275 27 196617 i;
#P newex 149 222 35 196617 * 10.;
#P comment 273 113 100 196617 number of decimals;
#P connect 10 0 9 1;
#P connect 12 0 10 1;
#P connect 11 1 12 0;
#P connect 5 0 9 0;
#P connect 9 0 1 0;
#P fasten 7 0 3 1 241 249 178 249;
#P fasten 7 0 1 1 241 199 179 199;
#P connect 7 0 11 0;
#P connect 11 0 10 0;
#P connect 1 0 2 0;
#P connect 3 0 4 0;
#P connect 2 0 3 0;
#P connect 6 0 7 0;
#P connect 6 1 7 1;
#P connect 8 0 6 0;
#P window clipboard copycount 13;

Feb 24 2008 | 6:46 pm

On 24 Feb 2008, at 17:22, Rabin Julien wrote:

> Something like this ?

Actually, if you do want rounding, rather than truncation, it’s a one-
liner:

[sprintf %.2f]

Feb 24 2008 | 7:24 pm

Feb 25 2008 | 10:43 am

Yep, it’s way more fun to do things by hand :) Should have read on…

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 248 120 103 196617 expr pow(10 \, \$i1);
#P flonum 68 163 116 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 248 78 60 196617 loadmess 3;
#P number 248 100 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 68 120 55 196617 0.123456;
#P comment 285 100 100 196617 number of decimals;
#P newex 68 141 190 196617 expr int((\$f1 + 0.5/ \$f2) * \$f2 ) / \$f2;
#P connect 6 0 0 1;
#P connect 3 0 6 0;
#P connect 0 0 5 0;
#P connect 2 0 0 0;
#P connect 4 0 3 0;
#P window clipboard copycount 7;

Feb 25 2008 | 11:11 am

My colleague suggested that the sprintf way may not be the fastest after all, even though it looks way more compact. So a made a little speedtest featuring sprintf, expr and standard max objects.

The speed says it all – oh and another thing: how do you change the number of decimals when using [sprintf %.#f] ? I can think of so many situations where i will need this :)

#P window setfont "Sans Serif" 9.;
#P window linecount 4;
#P comment 14 336 100 196617 strange – expr does the same as the other chain – yet it is a bit slower;
#P window linecount 1;
#P comment 843 336 100 196617 LOSER;
#P comment 415 336 100 196617 WINNER;
#P newex 206 146 37 196617 !/ 0.5;
#P comment 843 310 100 196617 1310 ms;
#P comment 415 310 100 196617 204 ms;
#P flonum 989 208 86 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 68 199 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 562 367 86 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P toggle 415 24 15 0;
#P flonum 415 231 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 415 196 35 196617 timer;
#P newex 415 68 30 196617 t b b;
#P newex 415 44 51 196617 qmetro 2;
#P newex 435 90 62 196617 uzi 100000;
#P newex 649 127 60 196617 loadmess 4;
#P newex 680 266 35 196617 * 10.;
#P newex 659 241 31 196617 t 5 i;
#P newex 659 291 31 196617 / 1.;
#P newex 562 201 41 196617 + 0.5;
#P number 649 154 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 649 216 35 196617 pow;
#P newex 649 191 35 196617 t 10 i;
#P message 562 143 55 196617 0.123456;
#P newex 562 341 34 196617 / 10.;
#P newex 562 317 27 196617 i;
#P newex 562 264 35 196617 * 10.;
#P comment 686 155 100 196617 number of decimals;
#P toggle 843 24 15 0;
#P flonum 843 231 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 843 196 35 196617 timer;
#P newex 843 68 30 196617 t b b;
#P newex 843 44 51 196617 qmetro 2;
#P newex 863 91 62 196617 uzi 100000;
#P toggle 14 24 15 0;
#P flonum 14 268 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 14 233 35 196617 timer;
#P newex 14 68 30 196617 t b b;
#P newex 14 44 51 196617 qmetro 2;
#P message 989 157 55 196617 0.123456;
#P newex 34 90 62 196617 uzi 100000;
#P newex 989 181 66 196617 sprintf %.4f;
#P newex 248 120 103 196617 expr pow(10 \, \$i1);
#P newex 248 78 60 196617 loadmess 4;
#P number 248 100 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 68 120 55 196617 0.123456;
#P comment 285 100 100 196617 number of decimals;
#P newex 68 170 191 196617 expr int((\$f1 + \$f2) * \$f3 ) / \$f3;
#P comment 14 310 100 196617 236 ms;
#P connect 14 0 10 0;
#P connect 10 0 11 0;
#P connect 11 0 12 0;
#P connect 12 0 13 0;
#P connect 11 1 8 0;
#P connect 11 0 12 1;
#P connect 8 0 3 0;
#P connect 3 0 1 0;
#P connect 1 0 41 0;
#P connect 45 0 1 1;
#P connect 6 0 45 0;
#P connect 5 0 4 0;
#P connect 4 0 6 0;
#P connect 6 0 1 2;
#P connect 39 0 35 0;
#P connect 35 0 36 0;
#P connect 36 0 37 0;
#P connect 37 0 38 0;
#P connect 36 1 34 0;
#P connect 36 0 37 1;
#P connect 34 0 25 0;
#P connect 25 0 29 0;
#P connect 29 0 22 0;
#P connect 22 0 23 0;
#P connect 23 0 24 0;
#P connect 24 0 40 0;
#P fasten 27 0 24 1 654 291 591 291;
#P fasten 27 0 22 1 654 241 592 241;
#P connect 30 0 29 1;
#P connect 33 0 28 0;
#P connect 28 0 26 0;
#P connect 26 0 27 0;
#P connect 27 0 31 0;
#P connect 31 0 30 0;
#P connect 26 1 27 1;
#P connect 31 1 32 0;
#P connect 32 0 30 1;
#P connect 20 0 16 0;
#P connect 16 0 17 0;
#P connect 17 0 18 0;
#P connect 18 0 19 0;
#P connect 17 1 15 0;
#P connect 17 0 18 1;
#P connect 15 0 9 0;
#P connect 9 0 7 0;
#P connect 7 0 42 0;
#P window clipboard copycount 49;

Feb 25 2008 | 11:48 am

On 25 Feb 2008, at 11:11, Bas van der Graaff wrote:

> My colleague suggested that the sprintf way may not be the fastest
> after all, even though it looks way more compact.

Sure – the Max tokeniser is called to turn the intermediate string
back into a float, so it will be slower than the alternatives, should
that matter.

> oh and another thing: how do you change the number of decimals when
> using [sprintf %.#f] ?

I don’t think I received that memo.

[sprintf %.*f] should do it according to the specs I’m looking at, but
the Max sprintf object doesn’t seem to support that.

— N.

Feb 26 2008 | 11:54 pm

Quote: tedor wrote on Sun, 24 February 2008 08:28
—————————————————-
>
> could you tell me please, how to round a float which has 5 numbers after the dot (5 decimals?) to a number that has only 2 decimals?
>
> like
>
> 0.12345465
>
> to
>
> 0.12
>
> Any help appreciated!
>
> thanks
>
> tEd
—————————————————-

Also, lp.round here:

http://cnmat.berkeley.edu/patch/2522

I mean, round.

mz

Feb 27 2008 | 1:45 am

> [sprintf %.*f] should do it according to the specs I’m looking at, but
> the Max sprintf object doesn’t seem to support that.

sprintf %.3f will round to 3 places, %.4f to four, etc.

But I noticed the following (maybe someone has an explanation to this behavior):

if I feed a sprintf %.3f the number 0.2345, it outputs 0.234 (rounds down)

sending 0.23451 will cause an output of 0.235 (rounds up)

hmmm…

best,
Zachary

Feb 27 2008 | 4:37 pm

Quote: Zachary Seldess wrote on Wed, 27 February 2008 02:45
—————————————————-
> if I feed a sprintf %.3f the number 0.2345, it outputs 0.234 (rounds down)
>
—————————————————-

Because you can’t represent 0.2345 *exactly* in binary.

The closest binary representation is 0.23449999

So guess what happens when you round?

If I had a dollar for every time I’ve had to explain this…

Feb 27 2008 | 5:03 pm

Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
—————————————————-
> If I had a dollar for every time I’ve had to explain this…
—————————————————-

That int would probably have wrapped around by now and you’d actually owe money :)

Feb 28 2008 | 8:25 am

Bas van der Graaff schrieb:
> Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
> —————————————————-
>> If I had a dollar for every time I’ve had to explain this…
> —————————————————-
>
> That int would probably have wrapped around by now and you’d actually owe money :)

Ha, the times I have laughed that much hasn’t wrapped yet… ;-))))

Stefan

Stefan Tiedje————x——-
–_____———–|————–
–(_|_ —-|—–|—–()——-
— _|_)—-|—–()————–
———-()——–www.ccmix.com

Feb 28 2008 | 9:13 am

Quote: Bas van der Graaff wrote on Wed, 27 February 2008 18:03
—————————————————-
> Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
> —————————————————-
> > If I had a dollar for every time I’ve had to explain this…
> —————————————————-
>
> That int would probably have wrapped around by now and you’d actually owe money :)
—————————————————-

Oh, with 64-bit unsigned integers, I think I can handle it.-

Viewing 15 posts - 1 through 15 (of 15 total)

Forums > MaxMSP