**Common Lisp the Language, 2nd Edition**

While most arithmetic functions will operate on any kind of number, coercing types if necessary, the following functions are provided to allow specific conversions of data types to be forced when desired.

**[Function]**

`float number &optional other`

This converts any non-complex number to a floating-point number.
With no second argument, if *number* is already a floating-point
number, then *number* is returned;
otherwise a `single-float` is produced.
If the argument *other* is provided, then it must be a floating-point
number, and *number* is converted to the same format as *other*.
See also `coerce`.

**[Function]**

`rational number `

Each of these functions converts any non-complex number to a rational number. If the argument is already rational, it is returned. The two functions differ in their treatment of floating-point numbers.

`rational` assumes that the floating-point number is completely accurate
and returns a rational number mathematically equal to the precise
value of the floating-point number.

`rationalize` assumes that the
floating-point number is accurate only to the precision of the
floating-point representation and may return any rational number for
which the floating-point number is the best available approximation of
its format; in doing this it attempts to keep both numerator and
denominator small.

It is always the case that

(float (rationalx)x) ==x

and

(float (rationalizex)x) ==x

That is, rationalizing a floating-point number by either method
and then converting it back
to a floating-point number of the same format produces the original number.
What distinguishes the two functions is that `rational` typically
has a simple, inexpensive implementation, whereas `rationalize` goes
to more trouble to produce a result that is more pleasant to view and
simpler to compute with for some purposes.

**[Function]**

`numerator rational `

These functions take a rational number (an integer or ratio)
and return as an integer the numerator or denominator of the canonical
reduced form of the rational. The numerator of an integer is that integer;
the denominator of an integer is `1`. Note that

(gcd (numeratorx) (denominatorx)) => 1

The denominator will always be a strictly positive integer; the numerator may be any integer. For example:

(numerator (/ 8 -6)) => -4 (denominator (/ 8 -6)) => 3

There is no `fix` function in Common Lisp because there are several
interesting ways to convert non-integral values to integers.
These are provided by the functions below, which perform not only
type conversion but also some non-trivial calculations as well.

**[Function]**

`floor number &optional divisor `

In the simple one-argument case,
each of these functions converts its argument *number*
(which must not be complex) to an integer.
If the argument is already an integer, it is returned directly.
If the argument is a ratio or floating-point number, the functions use
different algorithms for the conversion.

`floor` converts its argument by truncating toward negative
infinity; that is, the result is the largest integer that is not larger
than the argument.

`ceiling` converts its argument by truncating toward positive
infinity; that is, the result is the smallest integer that is not smaller
than the argument.

`truncate` converts its argument by truncating toward zero;
that is, the result is the integer of the same sign as the argument
and which has the greatest integral
magnitude not greater than that of the argument.

`round` converts its argument by rounding to the nearest
integer; if *number* is exactly halfway between two integers
(that is, of the form *integer*+0.5), then it is rounded to the one that
is even (divisible by 2).

The following table shows what the four functions produce when given various arguments.

Argument floor ceiling truncate round ---------------------------------------------------------- 2.6 2 3 2 3 2.5 2 3 2 2 2.4 2 3 2 2 0.7 0 1 0 1 0.3 0 1 0 0 -0.3 -1 0 0 0 -0.7 -1 0 0 -1 -2.4 -3 -2 -2 -2 -2.5 -3 -2 -2 -2 -2.6 -3 -2 -2 -3 ----------------------------------------------------------If a second argument

This statement is not entirely accurate; one should instead say that
`(values (floor 5 2))` == `(values (floor (/ 5 2)))`,
because there is a second value to consider, as discussed below.
In other words, the first values returned by the two forms will be the same, but
in general the second values will differ. Indeed, we have

(floor 5 2) => 2 and 1 (floor (/ 5 2)) => 2 and 1/2

for this example.

The *divisor* may be any non-complex number.

It is generally accepted that it is an error for the *divisor* to be zero.

The one-argument case is exactly like the two-argument case where the second
argument is `1`.

In other words, the one-argument case returns an integer and fractional part
for the *number*: `(truncate 5.3) => 5.0 and 0.3`, for example.

Each of the functions actually returns *two* values,
whether given one or two arguments. The second
result is the remainder and may be obtained using
`multiple-value-bind` and related constructs.
If any of these functions is given two arguments ** x** and

When only one argument is given, the two results are exact; the mathematical sum of the two results is always equal to the mathematical value of the argument.

The names and definitions given here have recently been adopted by Lisp Machine Lisp, and MacLisp and NIL (New Implementation of Lisp) seem likely to follow suit.

**[Function]**

`mod number divisor `

* mod* performs the operation

`mod` and `rem` are therefore the usual modulus
and remainder functions when applied to two integer arguments.
In general, however, the arguments may be integers or floating-point
numbers.

(mod 13 4) => 1 (rem 13 4) => 1 (mod -13 4) => 3 (rem -13 4) => -1 (mod 13 -4) => -3 (rem 13 -4) => 1 (mod -13 -4) => -1 (rem -13 -4) => -1 (mod 13.4 1) => 0.4 (rem 13.4 1) => 0.4 (mod -13.4 1) => 0.6 (rem -13.4 1) => -0.4

**[Function]**

`ffloor number &optional divisor `

These functions are just like `floor`, `ceiling`, `truncate`, and
`round`, except that the result (the first result of two) is always a
floating-point number rather than an integer. It is roughly as if
`ffloor` gave its arguments to `floor`, and then applied `float` to
the first result before passing them both back. In practice, however,
`ffloor` may be implemented much more efficiently. Similar remarks
apply to the other three functions. If the first argument is a
floating-point number, and the second argument is not a floating-point
number of longer format, then the first result will be a floating-point
number of the same type as the first argument.
For example:

(ffloor -4.7) => -5.0 and 0.3 (ffloor 3.5d0) => 3.0d0 and 0.5d0

**[Function]**

`decode-float float `

The function `decode-float` takes a floating-point number
and returns three values.

The first value is a new floating-point number of the same format
representing the significand; the second value is an integer
representing the exponent; and the third value is a floating-point
number of the same format indicating the sign (-1.0 or 1.0).
Let *b* be the radix for the floating-point representation;
then `decode-float` divides the argument by an integral power of *b*
so as to bring its value between 1/*b* (inclusive) and 1 (exclusive)
and returns the quotient as the first value.
If the argument is zero, however, the result
is equal to the absolute value of the argument (that is, if there is a negative
zero, its significand is considered to be a positive zero).

The second value of `decode-float` is
the integer exponent *e* to which *b* must be raised
to produce the appropriate power for the division.
If the argument is zero, any integer value may be returned, provided
that the identity shown below for `scale-float` holds.

The third value of `decode-float` is a floating-point number,
of the same format as the argument, whose absolute value is 1
and whose sign matches that of the argument.

The function `scale-float` takes a floating-point number *f*
(not necessarily between 1/*b* and 1) and
an integer *k*, and returns `(* f (expt (float b f) k))`.
(The use of

Note that

(multiple-value-bind (signif expon sign) (decode-floatf) (scale-float signif expon)) == (absf)

and

(multiple-value-bind (signif expon sign) (decode-floatf) (* (scale-float signif expon) sign)) ==f

The function `float-radix` returns (as an integer)
the radix *b* of the floating-point argument.

The function `float-sign` returns a floating-point number ** z** such
that

The function `float-digits` returns, as a non-negative integer,
the number of radix-*b* digits
used in the representation of its argument (including any implicit
digits, such as a ``hidden bit'').
The function `float-precision`
returns, as a non-negative integer,
the number of significant radix-*b* digits present in the
argument; if the argument is (a floating-point)
zero, then the result is (an integer) zero.
For normalized floating-point numbers, the results of `float-digits`
and `float-precision`
will be the same, but the precision will be less than the
number of representation digits for a denormalized or zero number.

The function `integer-decode-float` is similar to `decode-float`
but for its first value returns,
as an `integer`, the significand scaled so as to be an integer.
For an argument *f*, this integer will be strictly less than

(exptb(float-precisionf))

but no less than

(exptb(- (float-precisionf) 1))

except that if *f* is zero, then the integer value will be zero.

The second value bears the same relationship to the first value
as for `decode-float`:

(multiple-value-bind (signif expon sign) (integer-decode-floatf) (scale-float (float signiff) expon)) == (absf)

The third value of `integer-decode-float` will be `1` or `-1`.

**[Function]**

`complex realpart &optional imagpart`

The arguments must be non-complex numbers; a number is returned
that has *realpart* as its real part and *imagpart* as its imaginary
part, possibly converted according to the rule of floating-point
contagion (thus both components will be of the same type).
If *imagpart* is not specified,
then `(coerce 0 (type-of realpart))` is
effectively used. Note that if both the

**[Function]**

`realpart number `

These return the real and imaginary parts of a complex number. If
*number* is a non-complex number, then `realpart` returns its
argument *number* and `imagpart`
returns `(* 0 number)`, which
has the effect that the imaginary part of a rational is

A clever way to multiply a complex number *z* by *i* is to write

(complex (- (imagpartz)) (realpartz))

instead of `(* z #c(0 1))`. This cleverness is not always
gratuitous; it may be of particular importance in the presence of minus
zero. For example, if we are using IEEE standard floating-point arithmetic
and

(4+0which could land on the wrong side of a branch cut, for example.i)(+0+i) = ((4)(+0)-(+0)(1))+((4)(1)+(+0)(+0)i= ((+0)-(+0))+((4)+(+0))i= +0+4i

[email protected]