Forums > MaxMSP

Calculation errors

December 13, 2012 | 1:23 pm

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. –

December 13, 2012 | 3:05 pm

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


December 13, 2012 | 3:32 pm

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


December 14, 2012 | 3:40 pm

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?


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