[gambit-list] Make precise calculations query

Bradley Lucier lucier at math.purdue.edu
Sat Feb 4 10:47:03 EST 2012


On Feb 4, 2012, at 4:45 AM, Mikael wrote:

> Dear Marc or Brad, I'd be curious to know if there is any way to ensure that calculations in Gambit become completely exact?
> 
> What I'm looking for is that as flonum operations by nature are inexact (+ 0.1 0.2 would be a good example), would there be a way to use bignum-only operations, or have some kind of configuration that uses fixnum when applicable and otherwise bignum only.

Do you mean floating-point numbers with parametrized precision?  Or do you mean something like this:

[Bradley-Luciers-MacBook-Pro:~] lucier% gsi
Gambit v4.6.3

> (+ #e1.2 #e3.77)
497/100

When I wrote my homework-on-the web system, there was code like

(define (exact-string->number s)
  (string->number (string-append "#e" s)))

to make sure that some of the conversions were exact.

> Is there anything like this - I know fixnum and flonum ops can be accessed under fixnum+ etc., but what about the bignums, (bignum+ 99999999999999999999 2)?

Well, simple + will do this, calling ##+ to convert the 2 to a (non-normalized) bignum, and then calling ##bignum.+ on the pair.

> Afaik the bignum library is completely exact for + and -, and has a precision of at least 10^-17 for the rest of the operations (* / modulo sqrt expt etc), is this the way?

*, modulo and expt are exact (if the first argument to expt is exact and the second is an integer), there is integer-sqrt, which gives you a square root and a remainder.

/ gives you an exact rational when handed two exact integers.

I'm not quite sure what you're looking for.  There are ways to compute with the so-called "constructive reals" or "computable reals" (I have the beginnings of such a package, and several packages using different underlying representations exist for Common Lisp), but is that what you want?

Brad


More information about the Gambit-list mailing list