Documentation |
Rounding down to the next integer
This functionality does not run in MATLAB.
floor(x)
floor rounds a number to the next smaller integer.
For complex arguments, rounding is applied separately to the real and the imaginary parts.
Integers are returned for real numbers and exact expressions representing real numbers.
Unevaluated function calls are returned for arguments that contain symbolic identifiers.
For floating-point intervals, the result will be a floating-point interval containing all the results of applying the rounding function to the real or complex numbers inside the interval.
If you think of x as a floating-point number, then trunc(x) truncates the digits after the decimal point. Thus, trunc coincides with floor for real positive arguments and with ceil for real negative arguments, respectively.
Note: If the argument is a floating-point number of absolute value larger than 10^{DIGITS}, the resulting integer is affected by internal non-significant digits! Cf. Example 3. |
Note: Internally, exact numerical expressions that are neither integers nor rational numbers are approximated by floating-point numbers before rounding. Thus, the resulting integer may depend on the present value of DIGITS! Cf. Example 4. |
The functions are sensitive to the environment variable DIGITS which determines the numerical working precision.
We demonstrate the rounding of real and complex numbers:
ceil(3.5), floor(3.5), round(3.5), trunc(3.5)
ceil(-7/2), floor(-7/2), round(-7/2), trunc(-7/2)
ceil(3 + 5/2*I), floor(4.3 + 7*I), round(I/2), trunc(I/2)
Also symbolic expressions representing numbers can be rounded:
x := PI*I + 7*sin(exp(2)): ceil(x), floor(x), round(x), trunc(x)
Rounding of expressions with symbolic identifiers produces unevaluated function calls:
delete x: ceil(x), floor(x - 1), round(x + 1), trunc(x^2 + 3)
The call round(x, n) serves for rounding the n-th decimal digit of the floating-point representation of x:
round(123.456, 1), round(123.456, 2), round(123.456, 3), round(123.456, 4), round(123.456, 5)
float(exp(5)*PI), round(exp(5)*PI, 3)
The second argument may also be negative, leading to rounding of the digits to the left of the decimal point:
round(123.45, 1), round(123.45, 0), round(123.45, -1), round(123.45, -2), round(123.45, -3)
Care should be taken when rounding floating-point numbers of large absolute value:
x := 10^30/3.0
Note that only the first 10 decimal digits are "significant". Further digits are subject to round-off effects caused by the internal binary representation. These "insignificant" digits are part of the integer produced by rounding:
floor(x), ceil(x)
delete x:
Exact numerical expressions are internally converted to floating point numbers before rounding. Consequently, the present setting of DIGITS can affect the result:
x := 10^30 - exp(30)^ln(10)
Note that the exact value of this number is 0. Floating point evaluation is subject to severe cancellations:
DIGITS := 10: float(x), floor(x), ceil(x)
The floating-point result is more accurate when a higher precision is used. The rounded values change accordingly:
DIGITS := 20: float(x), floor(x), ceil(x)
DIGITS := 30: float(x), floor(x), ceil(x)
delete x, DIGITS:
On floating-point intervals, ceil and floor behave as expected:
ceil(3.5...6.7); floor(3.5...6.7)
These intervals, as easily seen, contain the results of ceil(x) and floor(x) for all , respectively.
Because there are finite numbers represented as RD_INF and RD_NINF, respectively, ceil and floor return very small or large representable numbers in certain cases:
ceil(RD_NINF...RD_NINF)