[gambit-list] Make precise calculations query

Bradley Lucier lucier at math.purdue.edu
Sun Feb 5 11:05:48 EST 2012


On Feb 5, 2012, at 7:48 AM, Mikael wrote:

> Is there any way to perform fixed-point arithmetics on selected operations (to 3, 6, 9 etc decimals) (I think you call this floating-point with parameterized precision), maybe using a bignum-truncate-to-decimals?

In examples/pi/pi.scm there's a small bit of code to do fixed-point operations with k digits in any base:

  (define base^k (expt base k))
  (define (fixed.+ x y) (+ x y))
  (define (fixed.- x y) (- x y))
  (define (fixed.* x y) (quotient (* x y) base^k))
  (define (fixed.square x)  (fixed.* x x))
  (define (fixed./ x y) (quotient (* x base^k) y))
  (define (fixed.sqrt x) (integer-sqrt (* x base^k)))
  (define (number->fixed x) (round (* x base^k)))
  (define (fixed->number x) (/ x base^k))

These operations truncate, not round, number->fixed assumes that its argument is an exact number, and fixed->number returns an exact number, not a flonum, but it's a start.

Brad


More information about the Gambit-list mailing list