(r5rs.info.gz) Numerical operations
Info Catalog
(r5rs.info.gz) Syntax of numerical constants
(r5rs.info.gz) Numbers
(r5rs.info.gz) Numerical input and output
6.2.5 Numerical operations
--------------------------
The reader is referred to 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.
-- procedure: number? obj
-- procedure: complex? obj
-- procedure: real? obj
-- procedure: rational? obj
-- procedure: integer? obj
These numerical type predicates can be applied to any kind of
argument, including non-numbers. They return #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.
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.
_Note:_ In many implementations the `rational?' procedure
will be the same as `real?', and the `complex?' procedure
will be the same as `number?', but unusual implementations
may be able to represent some irrational numbers exactly or
may extend the number system to support some kind of
non-complex numbers.
-- procedure: exact? Z
-- procedure: inexact? Z
These numerical predicates provide tests for the exactness of a
quantity. For any Scheme number, precisely one of these predicates
is true.
-- procedure: = z1 z2 z3 ...,
-- procedure: < x1 x2 x3 ...,
-- procedure: > x1 x2 x3 ...,
-- procedure: <= x1 x2 x3 ...,
-- procedure: >= x1 x2 x3 ...,
These procedures return #t if their arguments are (respectively):
equal, monotonically increasing, monotonically decreasing,
monotonically nondecreasing, or monotonically nonincreasing.
These predicates are required to be transitive.
_Note:_ 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.
-- library procedure: zero? Z
-- library procedure: positive? X
-- library procedure: negative? X
-- library procedure: odd? N
-- library procedure: even? N
These numerical predicates test a number for a particular property,
returning #t or #f. See note above.
-- library procedure: max x1 x2 ...,
-- library procedure: min x1 x2 ...,
These procedures return the maximum or minimum of their arguments.
(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.
-- procedure: + z1 ...,
-- procedure: * z1 ...,
These procedures return the sum or product of their arguments.
(+ 3 4) ==> 7
(+ 3) ==> 3
(+) ==> 0
(* 4) ==> 4
(*) ==> 1
-- procedure: - z1 z2
-- procedure: - Z
-- optional procedure: - z1 z2 ...,
-- procedure: / z1 z2
-- procedure: / Z
-- optional procedure: / z1 z2 ...,
With two or more arguments, these procedures return the difference
or quotient of their arguments, associating to the left. With one
argument, however, they return the additive or multiplicative
inverse of their argument.
(- 3 4) ==> -1
(- 3 4 5) ==> -6
(- 3) ==> -3
(/ 3 4 5) ==> 3/20
(/ 3) ==> 1/3
-- library procedure: abs x
`Abs' returns the absolute value of its argument.
(abs -7) ==> 7
-- procedure: quotient n1 n2
-- procedure: remainder n1 n2
-- procedure: modulo n1 n2
These procedures implement number-theoretic (integer) division.
N2 should be non-zero. All three procedures return integers. If
N1/N2 is an integer:
(quotient N1 N2) ==> N1/N2
(remainder N1 N2) ==> 0
(modulo N1 N2) ==> 0
If N1/N2 is not an integer:
(quotient N1 N2) ==> N_Q
(remainder N1 N2) ==> N_R
(modulo N1 N2) ==> N_M
where N_Q is N1/N2 rounded towards zero, 0 < |N_R| < |N2|, 0 <
|N_M| < |N2|, N_R and N_M differ from N1 by a multiple of N2, N_R
has the same sign as N1, and N_M has the same sign as N2.
From this we can conclude that 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.
(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
-- library procedure: gcd n1 ...,
-- library procedure: lcm n1 ...,
These procedures return the greatest common divisor or least common
multiple of their arguments. The result is always non-negative.
(gcd 32 -36) ==> 4
(gcd) ==> 0
(lcm 32 -36) ==> 288
(lcm 32.0 -36) ==> 288.0 ; inexact
(lcm) ==> 1
-- procedure: numerator Q
-- procedure: denominator Q
These procedures return the numerator or denominator of their
argument; the result is computed as if the argument was
represented as a fraction in lowest terms. The denominator is
always positive. The denominator of 0 is defined to be 1.
(numerator (/ 6 4)) ==> 3
(denominator (/ 6 4)) ==> 2
(denominator
(exact->inexact (/ 6 4))) ==> 2.0
-- procedure: floor x
-- procedure: ceiling x
-- procedure: truncate x
-- procedure: round x
These procedures return integers. `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
default rounding mode specified 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.
(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
-- library procedure: rationalize x y
`Rationalize' returns the _simplest_ rational number differing
from X by no more than Y. A rational number r_1 is _simpler_
than another rational number r_2 if r_1 = p_1/q_1 and r_2 =
p_2/q_2 (in lowest terms) and |p_1|<= |p_2| and |q_1| <= |q_2|.
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
-- procedure: exp Z
-- procedure: log Z
-- procedure: sin Z
-- procedure: cos Z
-- procedure: tan Z
-- procedure: asin Z
-- procedure: acos Z
-- procedure: atan Z
-- procedure: atan Y X
These procedures are part of every implementation that supports
general real numbers; they compute the usual transcendental
functions. `Log' computes the natural logarithm of Z (not the
base ten logarithm). `Asin', `acos', and `atan' compute arcsine
(sin^-1), arccosine (cos^-1), and arctangent (tan^-1),
respectively. The two-argument variant of `atan' computes (angle
(make-rectangular X Y)) (see below), even in implementations that
don't support general complex numbers.
In general, the mathematical functions log, arcsine, arccosine, and
arctangent are multiply defined. The value of log z is defined to
be the one whose imaginary part lies in the range from -pi
(exclusive) to pi (inclusive). log 0 is undefined. With log
defined this way, the values of sin^-1 z, cos^-1 z, and tan^-1 z
are according to the following formulae:
sin^-1 z = -i log (i z + sqrt1 - z^2)
cos^-1 z = pi / 2 - sin^-1 z
tan^-1 z = (log (1 + i z) - log (1 - i z)) / (2 i)
The above specification follows [CLtL], which in turn cites
[Penfield81]; 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.
-- procedure: sqrt Z
Returns the principal square root of Z. The result will have
either positive real part, or zero real part and non-negative
imaginary part.
-- procedure: expt z1 z2
Returns Z1 raised to the power Z2. For z_1 ~= 0
z_1^z_2 = e^z_2 log z_1
0^z is 1 if z = 0 and 0 otherwise.
-- procedure: make-rectangular x1 x2
-- procedure: make-polar x3 x4
-- procedure: real-part Z
-- procedure: imag-part Z
-- procedure: magnitude Z
-- procedure: angle Z
These procedures are part of every implementation that supports
general complex numbers. Suppose X1, X2, X3, and X4 are real
numbers and Z is a complex number such that
Z = X1 + X2i = X3 . e^i X4
Then
(make-rectangular X1 X2) ==> Z
(make-polar X3 X4) ==> Z
(real-part Z) ==> X1
(imag-part Z) ==> X2
(magnitude Z) ==> |X3|
(angle Z) ==> x_angle
where -pi < x_angle <= pi with x_angle = X4 + 2pi n for some
integer n.
_Rationale:_ `Magnitude' is the same as `abs' for a real
argument, but `abs' must be present in all implementations,
whereas `magnitude' need only be present in implementations
that support general complex numbers.
-- procedure: exact->inexact Z
-- procedure: inexact->exact 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.
`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.
These procedures implement the natural one-to-one correspondence
between exact and inexact integers throughout an
implementation-dependent range. See section Implementation
restrictions.
Info Catalog
(r5rs.info.gz) Syntax of numerical constants
(r5rs.info.gz) Numbers
(r5rs.info.gz) Numerical input and output
automatically generated byinfo2html