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.
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.
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
and
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.
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
The denominator will always be a strictly positive integer; the numerator may be any integer. For example:
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 divisorIn 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 divisor is supplied, then the result is the appropriate type of rounding or truncation applied to the result of dividing the number by the divisor. For example, (floor 5 2) ≡ (floor (/ 5 2)) but is potentially more efficient.
for this example.
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.
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.
mod performs the operation floor on its two arguments and returns the second result of floor as its only result. Similarly, rem performs the operation truncate on its arguments and returns the second result of truncate as its only result.
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.
[Function]
ffloor number &optional divisorThese 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:
[Function]
decode-float floatThe 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 scale-float may be much more efficient than using exponentiation and multiplication and avoids intermediate overflow and underflow if the final result is representable.)
Note that
and
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 z and float1 have the same sign and also such that z and float2 have the same absolute value. The argument float2 defaults to the value of (float 1