A bug with expr and pow?

May 23, 2008 at 2:13am

A bug with expr and pow?

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;

#38021
May 23, 2008 at 3:05am

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

#131732
May 23, 2008 at 9:59am

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.

#131733
May 23, 2008 at 7:34pm

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

#131734
May 24, 2008 at 2:40am

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

#131735

You must be logged in to reply to this topic.