Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: PowerPC Numerics / Part 2 - The PowerPC Numerics C Implementation
Chapter 9 - Conversion Functions / Rounding Floating-Point Numbers to Integers


round

You can use the round function to round a real number to the integer value obtained by adding 1/2 to the magnitude and truncating.

double_t round (double_t x);
x
Any floating-point number.
DESCRIPTION
The round function adds 1/2 to the magnitude of its argument and chops to integer. The result is returned in a floating-point data type.

This function is not affected by the current rounding direction. Notice that the round function rounds halfway cases (1.5, 2.5, and so on) away from 0. With the default rounding direction, rint (described on page 6-13) rounds halfway cases to the even integer.

EXCEPTIONS
When x is finite and nonzero, either the result of round(x) is exact or it raises the following exception:

  • inexact (if x is not an integer value)

SPECIAL CASES
Table 9-6 shows the results when the argument to the round function is a zero, a NaN, or an Infinity.
Special cases for the round function
OperationResultExceptions raised
round(+0) +0None
round(-0) -0 None
round(NaN) NaNNone[17]
round(+ ) + None
round(- ) - None

EXAMPLES
z = round(+INFINITY);   /* z = +INFINITY because +   is already an 
                           integer value by definition. */
z = round(0.5);         /* z = 1.0 because |0.5| + 0.5 = 1.0. The 
                           inexact exception is raised. */
z = round(-0.9);        /* z = -1.0 because |-0.9| + 0.5 = 1.4. 
                           The inexact exception is raised. */

[17] If the NaN is a signaling NaN, the invalid exception is raised.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996