[gambit-list] Numbers and R5RS section 6.2, SRFI-70

Bradley Lucier lucier at math.purdue.edu
Fri Dec 28 11:53:46 EST 2012


I don't know what internal model you have of Gambit's numbers, but here's a block comment in _num#.scm that explains some of the relationships between Gambit's number representations.

;;;============================================================================
;;; Number representation.
;; There are 5 internal representations for numbers:
;;
;; fixnum, bignum, ratnum, flonum, cpxnum
;;
;; Fixnums and bignums form the class of exact-int.
;; Fixnums, bignums and ratnums form the class of exact-real.
;; Fixnums, bignums, ratnums and flonums form the class of noncpxnum.
;; The representation has some invariants:
;;
;; The numerator of a ratnum is a non-zero exact-int.
;; The denominator of a ratnum is an exact-int greater than 1.
;; The numerator and denominator have no common divisors greater than 1.
;;
;; The real part of a cpxnum is a noncpxnum.
;; The imaginary part of a cpxnum is a noncpxnum != fixnum 0
;; The following table gives the mapping of the Scheme exact numbers to their
;; internal representation:
;;
;;    type          representation
;; exact integer  = exact-int (fixnum, bignum)
;; exact rational = exact-real (fixnum, bignum, ratnum)
;; exact real     = exact-real (fixnum, bignum, ratnum)
;; exact complex  = exact-real or cpxnum with exact-real real and imag parts
;; For inexact numbers, the representation is not quite as straightforward.
;;
;; There are 3 special classes of inexact representation:
;; flonum-int : flonum with integer value
;; cpxnum-real: cpxnum with imag part = flonum 0.0 or -0.0
;; cpxnum-int : cpxnum-real with exact-int or flonum-int real part
;;
;; Note: cpxnum-real and cpxnum-int only exist if
;; (macro-cpxnum-are-possibly-real?) returns #t.
;;
;; This gives the following table for Scheme's inexact numbers:
;;
;;      type          representation
;; inexact integer  = flonum-int or cpxnum-int if it exists
;; inexact rational = flonum     or cpxnum-real if it exists
;; inexact real     = flonum     or cpxnum-real if it exists
;; inexact complex  = flonum     or cpxnum with flonum real or imag part

The maximum and minimum fixnums can be queried; for example, in a 64-bit implementation:

> ##max-fixnum
2305843009213693951
> ##min-fixnum
-2305843009213693952

Exact integers outside this range are bignums.  Bignums are limited in size based on how big an individual object can be in Gambit (about (expt 2 24) bytes in a 32-bit implementation and (expt 2 56) bytes (i.e., basically unlimited) in a 64-bit implementation).

Inexact numbers are the same as C doubles (64-bit).  Extended precision floating-point numbers (80-bit numbers) are used in extremis on machines that do not support 64-bit floating-point numbers natively.  (Don't ask.)

fx operations expect fixnum operands and yield fixnum results, fl operations expect flonum operands and yield flonum results.  Code compiled with "safe" declarations (the default) check these assumptions and throw an error if they fail; code compiled with "not safe" declarations do not make these checks and can yield incorrect results or crash the program if these assumptions are violated.

Brad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20121228/4b946e41/attachment.htm>


More information about the Gambit-list mailing list