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 / Converting Floating-Point to Integer Formats


roundtol

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

long int roundtol (double_t x);
x
Any floating-point number.
DESCRIPTION
The roundtol function adds 1/2 to the magnitude of its argument and chops to integer, returning the answer in long int type.

The result is returned in an integer data type. (The return type is the difference between the roundtol function and the round function, described on page 9-10.)

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

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

  • inexact (if x is not an integer)
  • invalid (if the integer result is outside the range of the long int type)

SPECIAL CASES
Table 9-2 shows the results when the argument to the roundtol function is a zero, a NaN, or an Infinity.
Special cases for the roundtol function
OperationResultExceptions raised
roundtol(+0) +0None
roundtol(-0) -0 None
roundtol(NaN) UndefinedNone[13]
roundtol(+ ) UndefinedInvalid
roundtol(- ) UndefinedInvalid

EXAMPLES
z = roundtol(+INFINITY);   /* z = an unspecified value because 
                              +   is outside of the range of long 
                              int. */
z = roundtol(0.5);         /* z = 1 because |0.5| + 0.5 = 1.0. The 
                              inexact exception is raised. */
z = roundtol(-0.9);        /* z = -1 because |-0.9| + 0.5 = 1.4. 
                              The inexact exception is raised. */

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

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996