Huge values of exact numbers

While reading through section 4.3 Numbers of the Reference, I noticed this:

The precision and size of exact numbers is limited only by available memory (and the precision of operations that can produce irrational numbers). In particular, adding, multiplying, subtracting, and dividing exact numbers always produces an exact result.

I think that means that this code:

(let loop ([n 0])
  (displayln n)
  (loop (add1 n)))

...will run until the heat death of the universe, or the memory on the machine is consumed by Racket's insatiable need to store more digits, or (more likely) until the user gets bored and hits ^C. Do I have that right?

How are exact numbers being stored to allow this, and what are the performance implications?

3 Likes

André van Meulebrouck has written a great article on how to implement bignums:

http://preserve.mactech.com/articles/mactech/Vol.08/08.03/BigNums/index.html

In Racket BC bignums used GMP (The GNU Multiple Precision Arithmetic Library) to represent bignums. In Chez Scheme they rolled their own representation.

Wrt to performance. It depends on the usage pattern.

Bignums are slower than fixnums (but then again - they are larger :slight_smile: .

Some encryption algorithms were written to be efficient using all 64 bits.
Due to the type tag fixnums don't have all 64 (or 32) bits available to represent the number.
A naïve implementation of such an algorithm will therefor use bignums - and be slow compared to a C implementation.

7 Likes