limiting a float

Aug 14, 2012 at 6:59pm

limiting a float

hello,

can anyone tell me how do I limit the numbers after the point in a float? I’m trying to use toFixed like in JavaScript, but id doesnt seem to work.

Also, is there a documentation of the Javascript in Max, and actually how come some function available in regular Javascript aren’t in Max, I’m a bit confused as to how this thing works…

Thanks!

#63973
Aug 14, 2012 at 7:50pm

you can set limits to a number box using the inspector, just click on the object, press cmd i, and you can set the maximum and minimum values there. You could also use the [minimum] and [maximum] objects to do limiting. Is that what you were after?

#230674
Aug 14, 2012 at 9:49pm

I think this is more a question about decimal places…

#230675
Aug 14, 2012 at 10:50pm

Okay, I see what your getting at. How about this:

– Pasted Max Patch, click to expand. –

This limits the example to three and two decimal places respectively

#230676
Aug 15, 2012 at 12:03am

Or use [sprintf]

– Pasted Max Patch, click to expand. –
#230677
Aug 15, 2012 at 10:14am

yes, i was talking about decimal places. I copy the text and press new from clipboard, but it just shows me the text. I am using Max 4.5…
Isn’t there a way to do this using code? you know? just a function?

thanks!!

#230678
Aug 15, 2012 at 11:47am

like in regular JavaScript I will do:

(0.595).toFixed(2) === ’0.59′

#230679
Aug 15, 2012 at 1:20pm

The problem with using toFixed() in your JavaScript is that if it tries to send out 0.59 (for instance) through an outlet, what comes out is a float. The value 0.59 does not exist in binary floating point. The JavaScript Number class does a lot of funky work to present you with values like 0.59, but all that goes out the door when you send them through a Max outlet, where they become plain-vanilla IEEE floating point values.

There are multiple threads on the board about the idiosyncrasies of binary floating point arithmetic. You need to understand this stuff or Max will bite you where it hurts.

See, for instance, < http://cycling74.com/forums/topic.php?id=24639> in particular the linked article by Peter Elsea.

#230680
Aug 15, 2012 at 2:37pm

Or [round 0.1].

#230681
Aug 15, 2012 at 3:25pm

Hi,

mark2e’s [sprintf] for max 4 ; even if i do not understand why this topic is in the javascript forum ;-)

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 89 115 48 196617 loadbang;
#P flonum 195 274 77 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 195 241 66 196617 sprintf %.4f;
#P newex 89 240 66 196617 sprintf %.2f;
#P flonum 89 275 77 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 89 150 61 196617 0.333333;
#P flonum 89 182 77 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 6 0 1 0;
#P connect 0 0 3 0;
#P connect 0 0 4 0;
#P connect 4 0 5 0;
#P connect 3 0 2 0;
#P connect 1 0 0 0;
#P window clipboard copycount 7;

#230682
Aug 15, 2012 at 6:02pm

Nicholas, thank you very much, this is exactly what I need. Only that I really want this as a code I can use in JS. Is there a way to use this function – sprintf – as a command in the JS?

It will just simplify the amount of patchers I will need to use If I’ll be able to this inside the code….
Thank you everybody else too.

#230683
Aug 16, 2012 at 5:57am

Hello,

“toFixed” method works as expected (according to the “Core JavaScript Reference 1.5″) :

The Number.prototype.toFixed method returns a string representing a Number object in
fixed-point notation, rounded to the number of digits after the decimal point specified
by fractionDigits.

function msg_float(v)
{
    post(v.toFixed(2));
    post("n");
}

In floating point world, i suggest you to read again Peter Castine advices ;-)

#230684

You must be logged in to reply to this topic.