round

Rounding to the nearest integer

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

round(x, <n>)

Description

round rounds a number to the nearest integer.

For complex arguments, rounding is applied separately to the real and the imaginary parts.

For the call round(x, n), the result is a floating-point number with the n-th decimal digit after the decimal point being rounded. All further digits are set to zero. If the integer n is negative, the corresponding digit to the left of the decimal point is rounded. Cf. Example 2.

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.

    Note:   If the argument is a floating-point number of absolute value larger than 10DIGITS, 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.

Environment Interactions

The functions are sensitive to the environment variable DIGITS which determines the numerical working precision.

Examples

Example 1

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)

Example 2

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)

Example 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:

Example 4

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:

Example 5

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)

Parameters

x

An arithmetical expression or a floating-point interval

n

An integer. If n is positive, the n-th digit after the decimal point is rounded. If n is negative, the |n|-th digit before the decimal point is rounded.

Return Values

Arithmetical expression.

Overloaded By

x

See Also

MuPAD Functions

Was this topic helpful?