(This section is largely taken from the Revised^4 Report on the Algorithmic Language Scheme.)
Numerical computation has traditionally been neglected by the Lisp community. Until Common Lisp there was no carefully thought out strategy for organizing numerical computation, and with the exception of the MacLisp system little effort was made to execute numerical code efficiently. This report recognizes the excellent work of the Common Lisp committee and accepts many of their recommendations. In some ways this report simplifies and generalizes their proposals in a manner consistent with the purposes of Scheme.
It is important to distinguish between the mathematical numbers, the Scheme numbers that attempt to model them, the machine representations used to implement the Scheme numbers, and notations used to write numbers. This report uses the types number, complex, real, rational, and integer to refer to both mathematical numbers and Scheme numbers. Machine representations such as fixed point and floating point are referred to by names such as fixnum and flonum.
Mathematically, numbers may be arranged into a tower of subtypes in which each level is a subset of the level above it:
number complex real rational integer
For example, 3 is an integer. Therefore 3 is also a rational, a real,
and a complex. The same is true of the Scheme numbers that model 3.
For Scheme numbers, these types are defined by the predicates
number?
, complex?
, real?
, rational?
, and
integer?
.
There is no simple relationship between a number's type and its representation inside a computer. Although most implementations of Scheme will offer at least two different representations of 3, these different representations denote the same integer.
Scheme's numerical operations treat numbers as abstract data, as independent of their representation as possible. Although an implementation of Scheme may use fixnum, flonum, and perhaps other representations for numbers, this should not be apparent to a casual programmer writing simple programs.
It is necessary, however, to distinguish between numbers that are represented exactly and those that may not be. For example, indexes into data structures must be known exactly, as must some polynomial coefficients in a symbolic algebra system. On the other hand, the results of measurements are inherently inexact, and irrational numbers may be approximated by rational and therefore inexact approximations. In order to catch uses of inexact numbers where exact numbers are required, Scheme explicitly distinguishes exact from inexact numbers. This distinction is orthogonal to the dimension of type.
Scheme numbers are either exact or inexact. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations. A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number.
If two implementations produce exact results for a computation that did not involve inexact intermediate results, the two ultimate results will be mathematically equivalent. This is generally not true of computations involving inexact numbers since approximate methods such as floating point arithmetic may be used, but it is the duty of each implementation to make the result as close as practical to the mathematically ideal result.
Rational operations such as +
should always produce exact results
when given exact arguments. If the operation is unable to produce an
exact result, then it may either report the violation of an
implementation restriction or it may silently coerce its result to an
inexact value. See section Implementation restrictions.
With the exception of inexact->exact
, the operations described in
this section must generally return inexact results when given any
inexact arguments. An operation may, however, return an exact result if
it can prove that the value of the result is unaffected by the
inexactness of its arguments. For example, multiplication of any number
by an exact zero may produce an exact zero result, even if the other
argument is inexact.
Implementations of Scheme are not required to implement the whole tower of subtypes (see section Numerical types), but they must implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language. For example, an implementation in which all numbers are real may still be quite useful.(1)
Implementations may also support only a limited range of numbers of any type, subject to the requirements of this section. The supported range for exact numbers of any type may be different from the supported range for inexact numbers of that type. For example, an implementation that uses flonums to represent all its inexact real numbers may support a practically unbounded range of exact integers and rationals while limiting the range of inexact reals (and therefore the range of inexact integers and rationals) to the dynamic range of the flonum format. Furthermore the gaps between the representable inexact integers and rationals are likely to be very large in such an implementation as the limits of this range are approached.
An implementation of Scheme must support exact integers throughout the
range of numbers that may be used for indexes of lists, vectors, and
strings or that may result from computing the length of a list, vector,
or string. The length
, vector-length
, and
string-length
procedures must return an exact integer, and it is
an error to use anything but an exact integer as an index. Furthermore
any integer constant within the index range, if expressed by an exact
integer syntax, will indeed be read as an exact integer, regardless of
any implementation restrictions that may apply outside this range.
Finally, the procedures listed below will always return an exact integer
result provided all their arguments are exact integers and the
mathematically expected result is representable as an exact integer
within the implementation:
* gcd modulo + imag-part numerator - inexact->exact quotient abs lcm rationalize angle magnitude real-part ceiling make-polar remainder denominator make-rectangular round expt max truncate floor min
Implementations are encouraged, but not required, to support exact
integers and exact rationals of practically unlimited size and
precision, and to implement the above procedures and the /
procedure in such a way that they always return exact results when given
exact arguments. If one of these procedures is unable to deliver an
exact result when given exact arguments, then it may either report a
violation of an implementation restriction or it may silently coerce its
result to an inexact number. Such a coercion may cause an error
later.
An implementation may use floating point and other approximate representation strategies for inexact numbers. This report recommends, but does not require, that the IEEE 32-bit and 64-bit floating point standards be followed by implementations that use flonum representations, and that implementations using other representations should match or exceed the precision achievable using these floating point standards.
In particular, implementations that use flonum representations must
follow these rules: A flonum result must be represented with at least as
much precision as is used to express any of the inexact arguments to
that operation. It is desirable (but not required) for potentially
inexact operations such as sqrt
, when applied to exact arguments,
to produce exact answers whenever possible (for example the square root
of an exact 4 ought to be an exact 2). If, however, an exact number is
operated upon so as to produce an inexact result (as by sqrt
),
and if the result is represented as a flonum, then the most precise
flonum format available must be used; but if the result is represented
in some other way then the representation must have at least as much
precision as the most precise flonum format available.
Although Scheme allows a variety of written notations for numbers, any particular implementation may support only some of them.(2) For example, an implementation in which all numbers are real need not support the rectangular and polar notations for complex numbers. If an implementation encounters an exact numerical constant that it cannot represent as an exact number, then it may either report a violation of an implementation restriction or it may silently represent the constant by an inexact number.
A number may be written in binary, octal, decimal, or hexadecimal by the
use of a radix prefix. The radix prefixes are #b
(binary),
#o
(octal), #d
(decimal), and #x
(hexadecimal).
With no radix prefix, a number is assumed to be expressed in
decimal.
A numerical constant may be specified to be either exact or inexact by a
prefix. The prefixes are #e
for exact, and #i
for
inexact. An exactness prefix may appear before or after any radix
prefix that is used. If the written representation of a number has no
exactness prefix, the constant may be either inexact or exact. It is
inexact if it contains a decimal point, an exponent, or a #
character in the place of a digit, otherwise it is exact.
In systems with inexact numbers of varying precisions it may be useful
to specify the precision of a constant. For this purpose, numerical
constants may be written with an exponent marker that indicates
the desired precision of the inexact representation. The letters
s
, f
, d
, and l
specify the use of
short, single, double, and long precision,
respectively. (When fewer than four internal inexact representations
exist, the four size specifications are mapped onto those available.
For example, an implementation with two internal representations may map
short and single together and long and double together.) In addition,
the exponent marker e
specifies the default precision for the
implementation. The default precision has at least as much precision as
double, but implementations may wish to allow this default to be
set by the user.
3.14159265358979F0 Round to single --- 3.141593 0.6L0 Extend to long --- .600000000000000
See section Entry Format for a summary of the naming conventions used to specify restrictions on the types of arguments to numerical routines. The examples used in this section assume that any numerical constant written using an exact notation is indeed represented as an exact number. Some examples also assume that certain numerical constants written using an inexact notation can be represented without loss of accuracy; the inexact constants were chosen so that this is likely to be true in implementations that use flonums to represent inexact numbers.
#t
if the object is of the
named type, and otherwise they return #f
. In general, if a type
predicate is true of a number then all higher type predicates are also
true of that number. Consequently, if a type predicate is false of a
number, then all lower type predicates are also false of that
number.(3)
If z is an inexact complex number, then (real? z)
is
true if and only if (zero? (imag-part z))
is true. If
x is an inexact real number, then (integer? x)
is
true if and only if (= x (round x))
.
(complex? 3+4i) => #t (complex? 3) => #t (real? 3) => #t (real? -2.5+0.0i) => #t (real? #e1e10) => #t (rational? 6/10) => #t (rational? 6/3) => #t (integer? 3+0i) => #t (integer? 3.0) => #t (integer? 8/4) => #t
Note: The behavior of these type predicates on inexact numbers is unreliable, since any inaccuracy may affect the result.
#t
if their arguments are (respectively):
equal, monotonically increasing, monotonically decreasing, monotonically
nondecreasing, or monotonically nonincreasing.
These predicates are transitive. Note that the traditional implementations of these predicates in Lisp-like languages are not transitive.
Note: While it is not an error to compare inexact numbers using these
predicates, the results may be unreliable because a small inaccuracy may
affect the result; this is especially true of =
and zero?
.
When in doubt, consult a numerical analyst.
#t
or #f
. See note above regarding inexact
numbers.
(max 3 4) => 4 ; exact (max 3.9 4) => 4.0 ; inexact
Note: If any argument is inexact, then the result will also be inexact
(unless the procedure can prove that the inaccuracy is not large enough
to affect the result, which is possible only in unusual
implementations). If min
or max
is used to compare
numbers of mixed exactness, and the numerical value of the result cannot
be represented as an inexact number without loss of accuracy, then the
procedure may report a violation of an implementation
restriction.(4)
(+ 3 4) => 7 (+ 3) => 3 (+) => 0 (* 4) => 4 (*) => 1
(- 3 4) => -1 (- 3 4 5) => -6 (- 3) => -3 (/ 3 4 5) => 3/20 (/ 3) => 1/3
(1+ z)
is equivalent to (+ z 1)
; (-1+ z)
is
equivalent to (- z 1)
.
abs
returns the magnitude of its argument.
(abs -7) => 7
(quotient n1 n2) => n3 (remainder n1 n2) => n4 (modulo n1 n2) => n4
For integers n1 and n2 with n2 not equal to 0,
(= n1 (+ (* n2 (quotient n1 n2)) (remainder n1 n2))) => #t
provided all numbers involved in that computation are exact.
The value returned by quotient
always has the sign of the product
of its arguments. remainder
and modulo
differ on negative
arguments -- the remainder
always has the sign of the dividend,
the modulo
always has the sign of the divisor:
(modulo 13 4) => 1 (remainder 13 4) => 1 (modulo -13 4) => 3 (remainder -13 4) => -1 (modulo 13 -4) => -3 (remainder 13 -4) => 1 (modulo -13 -4) => -1 (remainder -13 -4) => -1 (remainder -13 -4.0) => -1.0 ; inexact
(integer-floor n1 n2) (floor (/ n1 n2))
However, the former is faster and does not produce an intermediate result.
integer-divide
is equivalent to performing both quotient
and remainder
at once. The result of integer-divide
is an
object with two components; the procedures
integer-divide-quotient
and integer-divide-remainder
select those components. These procedures are useful when both the
quotient and remainder are needed; often computing both of these numbers
simultaneously is much faster than computing them separately.
For example, the following are equivalent:
(lambda (n d) (cons (quotient n d) (remainder n d))) (lambda (n d) (let ((qr (integer-divide n d))) (cons (integer-divide-quotient qr) (integer-divide-remainder qr))))
(gcd 32 -36) => 4 (gcd) => 0 (lcm 32 -36) => 288 (lcm 32.0 -36) => 288.0 ; inexact (lcm) => 1
(numerator (/ 6 4)) => 3 (denominator (/ 6 4)) => 2 (denominator (exact->inexact (/ 6 4))) => 2.0
floor
returns the largest
integer not larger than x. ceiling
returns the smallest
integer not smaller than x. truncate
returns the integer
closest to x whose absolute value is not larger than the absolute
value of x. round
returns the closest integer to x,
rounding to even when x is halfway between two integers.
Rationale: round
rounds to even for consistency with the rounding
modes required by the IEEE floating point standard.
Note: If the argument to one of these procedures is inexact, then the
result will also be inexact. If an exact value is needed, the result
should be passed to the inexact->exact
procedure (or use one of
the procedures below).
(floor -4.3) => -5.0 (ceiling -4.3) => -4.0 (truncate -4.3) => -4.0 (round -4.3) => -4.0 (floor 3.5) => 3.0 (ceiling 3.5) => 4.0 (truncate 3.5) => 3.0 (round 3.5) => 4.0 ; inexact (round 7/2) => 4 ; exact (round 7) => 7
(floor->exact x) (inexact->exact (floor x))
except that the former is faster and has fewer range restrictions.
rationalize
returns the simplest rational number differing
from x by no more than y. A rational number r1 is
simpler than another rational number r2 if
r1=p1/q1 and r2=p2/q2 (both
in lowest terms) and |p1|<=|p2| and
|q1|<=|q2|. Thus 3/5 is simpler than 4/7.
Although not all rationals are comparable in this ordering (consider
2/7 and 3/5) any interval contains a rational number that is
simpler than every other rational number in that interval (the simpler
2/5 lies between 2/7 and 3/5). Note that 0=0/1 is the
simplest rational of all.
(rationalize (inexact->exact .3) 1/10) => 1/3 ; exact (rationalize .3 1/10) => #i1/3 ; inexact
rationalize->exact
is similar to rationalize
except that
it always returns an exact result.
simplest-rational
returns the simplest rational number between
x and y inclusive; simplest-exact-rational
is similar
except that it always returns an exact result.
These procedures implement the same functionality as rationalize
and rationalize->exact
, except that they specify the input range
by its endpoints; rationalize
specifies the range by its center
point and its (half-) width.
log
computes the natural logarithm of z (not the base ten logarithm).
asin
, acos
, and atan
compute arcsine, arccosine,
and arctangent, respectively. The two-argument variant of atan
computes (angle (make-rectangular x y))
(see
below).
In general, the mathematical functions log, arcsine, arccosine, and arctangent are multiply defined. For nonzero real x, the value of log x is defined to be the one whose imaginary part lies in the range minus pi (exclusive) to pi (inclusive). log 0 is undefined. The value of log z when z is complex is defined according to the formula With log defined this way, the values of arcsine, arccosine, and arctangent are according to the following formulae: The above specification follows Common Lisp: the Language, which in turn cites Principal Values and Branch Cuts in Complex APL; refer to these sources for more detailed discussion of branch cuts, boundary conditions, and implementation of these functions. When it is possible these procedures produce a real result from a real argument.
make-rectangular
and make-polar
return z,
real-part
returns x1, imag-part
returns x2,
magnitude
returns x3, and angle
returns x4.
In the case of angle
, whose value is not uniquely determined by
the preceding rule, the value returned will be the one in the range
minus pi (exclusive) to pi (inclusive).
conjugate
returns the complex conjugate of z.
exact->inexact
returns an inexact representation of z. The
value returned is the inexact number that is numerically closest to the
argument. If an exact argument has no reasonably close inexact
equivalent, then a violation of an implementation restriction may be
reported; MIT Scheme signals an error of type
condition-type:bad-range-argument
in this case.
inexact->exact
returns an exact representation of z. The
value returned is the exact number that is numerically closest to the
argument. If an inexact argument has no reasonably close exact
equivalent, then a violation of an implementation restriction may be
reported; in MIT Scheme this case does not occur because all inexact
numbers are representable as exact numbers.
These procedures implement the natural one-to-one correspondence between exact and inexact integers throughout an implementation-dependent range. See section Implementation restrictions.
number->string
takes a number and a radix and returns as a string
an external representation of the given number in the given radix such
that
(let ((number number) (radix radix)) (eqv? number (string->number (number->string number radix) radix)))
is true. It is an error if no possible result makes this expression true.
If number is inexact, the radix is 10, and the above expression can be satisfied by a result that contains a decimal point, then the result contains a decimal point and is expressed using the minimum number of digits (exclusive of exponent and trailing zeroes) needed to make the above expression true; otherwise the format of the result is unspecified.
The result returned by number->string
never contains an explicit
radix prefix.
Note: The error case can occur only when number is not a complex number or is a complex number with an non-rational real or imaginary part.
Rationale: If number is an inexact number represented using flonums, and the radix is 10, then the above expression is normally satisfied by a result containing a decimal point. The unspecified case allows for infinities, NaNs, and non-flonum representations.
number->string
when
number is a flonum (and consequently controls all printing of
flonums); it can have the following values:
normal
, which is the initial value of this variable,
causes flonums to be printed with full precision. number->string
uses as many digits as are necessary to display all of the information
in the flonum. This value may also be specified as a list (normal
n)
where n is an exact integer; n is ignored.
(relative n)
, where n is an exact positive
integer, constrains number->string
to represent the flonum using
at most n significant digits. number->string
rounds the
result so that it is as accurate as possible. For example:
(number->string (* 4 (atan 1 1))) => "3.141592653589793" (fluid-let ((flonum-unparser-cutoff '(relative 5))) (number->string (* 4 (atan 1 1)))) => "3.1416" (fluid-let ((flonum-unparser-cutoff '(relative 5))) (number->string (* 4000 (atan 1 1)))) => "3141.6"
(absolute n)
, where n is an exact integer,
constrains number->string
to represent the flonum by rounding it
at the nth digit to the right of the decimal point; if n is
negative, the number is rounded (- n)
digits to the left of
the decimal point. For example:
(fluid-let ((flonum-unparser-cutoff '(absolute 5))) (number->string (* 4 (atan 1 1)))) => "3.14159" (fluid-let ((flonum-unparser-cutoff '(absolute 5))) (number->string (* 4000 (atan 1 1)))) => "3141.59265" (fluid-let ((flonum-unparser-cutoff '(absolute -4))) (number->string (* 4e10 (atan 1 1)))) => "31415930000." (fluid-let ((flonum-unparser-cutoff '(absolute -5))) (number->string (* 4e10 (atan 1 1)))) => "31415900000."
If flonum-unparser-cutoff
is bound to a value different from
those described here, number->string
issues a warning and acts as
though it had been bound to normal
.
"#o177"
). If radix is not supplied, then the default radix
is 10. If string is not a syntactically valid notation for a
number, then string->number
returns #f
.
(string->number "100") => 100 (string->number "100" 16) => 256 (string->number "1e2") => 100.0 (string->number "15##") => 1500.0
Note that a numeric representation using a decimal point or an exponent
marker is not recognized unless radix is 10
.
This section describes numerical operations that are restricted forms of the operations described above. These operations are useful because they compile very efficiently. However, care should be exercised: if used improperly, these operations can return incorrect answers, or even malformed objects that confuse the garbage collector.
A fixnum is an exact integer that is small enough to fit in a machine word. In MIT Scheme, fixnums are typically 24 or 26 bits, depending on the machine; it is reasonable to assume that fixnums are at least 24 bits. Fixnums are signed; they are encoded using 2's complement.
All exact integers that are small enough to be encoded as fixnums are
always encoded as fixnums -- in other words, any exact integer that is
not a fixnum is too big to be encoded as such. For this reason, small
constants such as 0
or 1
are guaranteed to be fixnums.
#t
if object is a fixnum; otherwise returns
#f
.
Here is an expression that determines the largest fixnum:
(let loop ((n 0)) (let ((m (+ n 1))) (if (fix:fixnum? m) (loop m) n)))
A similar expression determines the smallest fixnum.
(fix:zero? fixnum) (fix:= fixnum 0)
Similarly, fix:positive?
and fix:negative?
produce code
identical to equivalent expressions using fix:>
and fix:<
.
integer-divide
, except that its arguments
and its results must be fixnums. It should be used in conjunction with
integer-divide-quotient
and integer-divide-remainder
.
The following are bitwise-logical operations on fixnums.
(fix:not 0) => -1 (fix:not -1) => 0 (fix:not 1) => -2 (fix:not -34) => 33
(fix:and #x43 #x0f) => 3 (fix:and #x43 #xf0) => #x40
(fix:andc #x43 #x0f) => #x40 (fix:andc #x43 #xf0) => 3
(fix:or #x40 3) => #x43 (fix:or #x41 3) => #x43
(fix:xor #x40 3) => #x43 (fix:xor #x41 3) => #x42
(fix:lsh 1 10) => #x400 (fix:lsh #x432 -10) => 1 (fix:lsh -1 3) => -8 (fix:lsh -128 -4) => #x3FFFF8
A flonum is an inexact real number that is implemented as a
floating-point number. In MIT Scheme, all inexact real numbers are
flonums. For this reason, constants such as 0.
and 2.3
are guaranteed to be flonums.
#t
if object is a flonum; otherwise returns #f
.
(flo:- 0.
flonum)
.
atan
with two arguments. When
compiled, it does not check the types of its arguments.
MIT Scheme provides a facility for generating pseudo-random numbers. The current implementation is a "subtract-with-carry" random-number generator, based on the algorithm from A New Class of Random Number Generators, George Marsaglia and Arif Zaman, The Annals of Applied Probability, Vol. 1, No. 3, 1991. At the time it was implemented, this was a good algorithm for general purposes, but the state of the art in random-number generation is constantly changing. If necessary, the implementation will be updated to use a new algorithm while retaining the same interface.
The interface described here is essentially identical to that of Common Lisp.
random
returns a
pseudo-random number between zero (inclusive) and modulus
(exclusive). The exactness of the returned number is the same as the
exactness of modulus. Additionally, if modulus is an exact
integer, the returned number will be also. Usually, modulus is
either an exact integer or an inexact real; the current implementation
has been tuned to make these two cases fast.
If state is given and not #f
, it must be a random-state
object; otherwise, it defaults to the value of the variable
*random-state*
. This object is used to maintain the state of the
pseudo-random-number generator and is altered as a side effect of the
random
procedure.
(random 1.0) => .32744744667719056 (random 1.0) => .01668326768172354 (random 10) => 3 (random 10) => 8 (random 100) => 38 (random 100) => 63 (random 100/3) => 130501475769920525/6755399441055744 (random 100/3) => 170571694016427575/13510798882111488
random
uses by default. A call to random
will perform a
side effect on this data structure. This variable may be changed, using
set!
or fluid-let
, to hold a new random-state object.
*random-state*
, or as the state
argument to random
. If state is not given or #f
,
make-random-state
returns a copy of the current
random-number state object (the value of the variable
*random-state*
). If state is a random-state object, a copy
of that object is returned. If state is #t
, then a new
random-state object is returned that has been "randomly" initialized
by some means (such as by a time-of-day clock).
#t
if object is a random-state object, otherwise
returns #f
.