views:

54

answers:

4

i'm trying to get a first lisp program to work using the CLISP implementation, by typing

(print (mod (+ (* 28433 (expt 2 7830457) 1)) (expt 10 10))))

in the REPL.

but it gives me *** - overflow during multiplication of large numbers. i thought lisp features arbitrary size/precision. how could that ever happen then?

+1  A: 

Lisp is a family of languages with dozens of dialects and hundreds of different implementations.

Computers have finite memory. Programs under some operating systems may have limitations about the memory size. Different Common Lisp implementations use different numeric libraries.

You may want to consult your CLISP manual for its limitations of its various data types.

Rainer Joswig
+2  A: 

Chances are there's a better way to solve the problem. I haven't made it that far on PE, but I know the few that I've done so far tend to have "aha!" solutions to problems that seem out of a computer programs range.

This one especially - 2^7830457 is a huge number -- try (format t "~r" (expt 2 160)). You might try to look at the problem in a new light and see if there's a way to look at it that you haven't thought of.

Wayne Werner
+2  A: 

According to http://clisp.cons.org/impnotes/num-concepts.html the maximum size for a bignum is (2^2097088 - 1) and your 2^7830457 is much larger than that.

Perhaps you can look at breaking down that number - perhaps separate out a number of smaller 2^X factors...

Greg Harman
It looks like bignums are stored with 2 bytes for the length of the number in 4-byte words. I wonder what it would take to expand that to 3 or 4 bytes for the length.
Gabe
+1  A: 

Lisp's bignums may hold really large numbers, but they too have their limits.

In your case, you can combine exponentiation and modulus into a single procedure, e.g. as in http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method.

dmitry_vk