Forums > MaxMSP

A bug with expr and pow?

May 23, 2008 | 2:13 am

Dear all,

I can’t explain why this is happening, but when expr is asked to calculate values beyond 10^9, it starts putting out weird numbers. Try 10^10, 10^11 with the patch below.

Any solutions?

#P window setfont "Sans Serif" 9.;
#P number 63 160 185 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P newex 129 87 27 9109513 t b i;
#P newex 60 118 81 9109513 expr pow($i1\,$i2);
#P number 129 60 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 60 82 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 3 1 2 1;
#P connect 1 0 3 0;
#P connect 2 0 4 0;
#P connect 3 0 2 0;
#P connect 0 0 2 0;
#P window clipboard copycount 5;


May 23, 2008 | 3:05 am

You’re running into one of the limitations of computers. Most of the time there is only a certain number of bits available to represent numbers. This is the case in Max. The maximum integer that can be represented is 2147483647. After that it wraps around to -2147483648 (the lowest integer that can be represented).

If you really need numbers bigger than that, you need to look into another solution, like programming your stuff in mxj/Java (see for example, java’s BigInteger class which can support numbers of arbitrary magnitude).


May 23, 2008 | 9:59 am

For values outside the range of 32-bit integers, ie -(2^31) .. (2^31)-1, you will need to use floats.

However, 32-bit floats also have precision issues.

Welcome to digital arithmetic. You might want to read up on the subject.


May 23, 2008 | 7:34 pm

Inouk Demers schrieb:
> I can’t explain why this is happening, but when expr is asked to
> calculate values beyond 10^9, it starts putting out weird numbers.
> Try 10^10, 10^11 with the patch below.
>
> Any solutions?

Use floats. You will have a limitation to a 23-bit mantissa in
resolution, but its certainly usable…

#P window setfont "Sans Serif" 9.;
#P flonum 60 147 105 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P newex 129 87 27 196617 t b f;
#P newex 60 118 105 196617 expr pow($f1\,$f2);
#P number 129 60 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 60 82 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 2 0 4 0;
#P connect 1 0 3 0;
#P connect 3 0 2 0;
#P connect 3 1 2 1;
#P connect 0 0 2 0;
#P window clipboard copycount 5;


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


May 24, 2008 | 2:40 am

I certainly will read up on the subject. I had no idea until now, so thanks for the explanation.


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