Float comes with unexpected decimals

Sep 18, 2011 at 9:31am

Float comes with unexpected decimals

Hello

I do simple math on integers which becomes floats at a point in this very simple patch. Problem is that I have some very small decimals appearing and can’t do what I want to do : create a message to external world (a light software) and recall presets in it with the float at the end. And you know that in this case 10.03 is not 10.02999999 AT ALL :)

Perhaps the preset obj adds a ‘strange’ integer ? Because It seems I have no prob with the recall of the first slot but only with the next.

I know about the Float Display Correction Option, but it do not changes my problem here apparently.

Thanks for any help

Charles

– Pasted Max Patch, click to expand. –
#58907
Sep 18, 2011 at 9:49am

[sprintf] can take floats and reformat them with however many places you want, or you can try [round].

#211669
Sep 18, 2011 at 1:01pm

well preset takes numbers as such …

#211670
Sep 18, 2011 at 3:07pm

Tanks but I’ve already tried round 0.01 and it seemed to me it did not work properly In this case. Can you tell me what is the syntax to round a float in sprintf ?

#211671
Sep 18, 2011 at 8:20pm

Ok ! the %.2f thing in sprintf obj seems to do the trick.
Thanks.

#211672
Sep 19, 2011 at 8:43am

Crazy ! I was wrong : the message obj has the right text but if you print it you’ll see that the ‘wrong’ float comes again ! It stays in memory…

Does someone knows how to deal with floats in this case ?

– Pasted Max Patch, click to expand. –
#211673
Sep 19, 2011 at 1:24pm

Max interprets the value you are creating as a floating point number and so, the “limited precision of floats issue”, or whatever its proper name is – a problem not limited to this application alone BTW – rears its ugly head. (Do a search on this forum for “float representation” for example). It may be possible to resolve this, but it may end up being easier for you if you can revise the expected message formatting, for example in the form of two separate ints (or a seperator other than decimal point you can parse at the receiving end) rather than a float, ie: set/seq/X2LoadAndFireCue 40 6 or set/seq/X2LoadAndFireCue 40:06

#211674
Sep 19, 2011 at 2:54pm

Thanks a lot Spectro
I was afraid about such a thing. Unfortunately, the ‘client’ software is using floats to stock its own presets. I have no other solution than to send a float in the osc message. I’ll try to find a trick.

#211675
Sep 19, 2011 at 9:12pm

Back and forth !
The last patch works indeed. If I send the message right away via udp, everything’s ok. I was taken in by the print object which output the ‘entire’ float again.
Thanks

#211676

You must be logged in to reply to this topic.