fixnum-width, least-fixnum, greatest-fixnum - query the fixnum range
(import (rnrs)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
(fixnum-width)
(least-fixnum)
(greatest-fixnum)
The fixnum range is defined as the closed interval
[−2^(w−1), 2^(w−1)−1] such that
w is an implementation-defined integer w ≥ 24. The
fixnum-width procedure returns the implementation's w. The
least-fixnum procedure returns −2^(w−1). The
greatest-fixnum procedure returns 2^(w−1)−1.
The fixnum range is important to consider when using the fixnum
procedures because these procedures require that their arguments and results
are fixnums. They raise an exception when a result cannot be represented as
a fixnum.
The fixnum width depends very much on implementation details and the machine.
Here are some known fixnum widths.
- Chez Scheme
- The fixnum width is 61 or 30. If the machine's register width is m
then the fixnum width is m - log2(m/8).
- GNU Guile
- The fixnum width is 62 or 30. This is the machine's register width minus
two.
- IronScheme
- The fixnum width is 32.
- Loko Scheme
- The fixnum width is 61.
These procedures each return a single value; a fixnum as per the description
above.
; The results are different for different implementations.
(fixnum-width) => 61
(least-fixnum) => -1152921504606846976
(greatest-fixnum) => 1152921504606846975
This procedure is sometimes used in libraries that provide optimized variants of
code for implementations where the fixnum width is large enough. If the fixnum
width is 32 (or greater) then fixnums can represent all signed 32-bit
integers.
This procedure is only present in R6RS. An alternative to
(fixnum-width) is (+ 1
(bitwise-length (greatest-fixnum))). But
normally there is no need to use fixnum procedures if they are not available;
generic arithmetic can be used instead and will give the same results.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed.
These procedures are new to the fixnum library in R6RS. Implementations have
always had some kind of fixnum type but was normally not seen by the user.