Calculation errors

Dec 13, 2012 at 1:23pm

Calculation errors

When multiplying a value with a billion (for some reason I need to do that), The resulting value is not right. What is wrong and how can I solve it?

– Pasted Max Patch, click to expand. –
#65669
Dec 13, 2012 at 2:41pm

Hi,

Do not forget the point.

– Pasted Max Patch, click to expand. –
#236530
Dec 13, 2012 at 3:05pm

But still the value is not correct, this is the result: 156199993344.

#236531
Dec 13, 2012 at 3:32pm

Long story short, you can’t!

Integer values in Max (and most other programming languages and environments) have a quite limited range, about -2 billions to 2 billions. If you try to represent a value falling outside these boundaries, you’ll obtain some nonsense number.
Floating point values have a much wider range, but at some point you’ll fall outside of it and you’ll get an “infinite” value.
On the other hand, integers within their valid range are always exact; floats are always approximate, and the greater their absolute value, the worse the approximation… So, what you’re doing in your patch is multiplying some number fairly close to 156.2 for some other number fairly close to 10^9, and you’re getting a result fairly close to 1.562*10^11… if it makes any sense…

– Pasted Max Patch, click to expand. –

sorry for the bad news!
aa

#236532
Dec 13, 2012 at 3:40pm

Hi,

Have a look on my last patch here : http://cycling74.com/forums/topic.php?id=44201

#236533
Dec 14, 2012 at 3:40pm

When multiplying a value with a billion (for some reason I need to do that)

It would be worth asking yourself if you really “need to do that”. And how precise the result needs to be.

You’re banging your head against the limits of the IEEE 754 floating point specification. Which, incidentally, is more accurate floating point arithmetic than what was available when NASA put a man on the moon. Just for a point of perspective.

Your “incorrect” result of 156199993344 is off by the enormous «ahem» amount of 0.0004%. If you really higher accuracy, there are other ways of dealing with computer arithmetic to maintain greater precision, but most of them take a fair amount of work; the details depend on your needs. So, just what is that you’re doing that requires picometer accuracy?

#236534
Dec 14, 2012 at 5:27pm

Hi,

If you like headaches, you can do something like that ;-)

– Pasted Max Patch, click to expand. –
#236535

You must be logged in to reply to this topic.