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


trunc

You can use the trunc function to truncate the fractional part of a real number so that just the integer part remains.

double_t trunc (double_t x);
x
Any floating-point number.
DESCRIPTION
The trunc function chops off the fractional part of its argument. This is an ANSI standard C library function.

This function is the same as performing the following code sequence:

r = fegetround();          /* save current rounding direction */
fesetround(FE_TOWARDZERO); /* round toward zero */
rint(x);                   /* round to integer */
fesetround(r);             /* restore rounding direction */
EXCEPTIONS
When x is finite and nonzero, the result of trunc(x) is exact.

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

EXAMPLES
z = trunc(+INFINITY);   /* z = +INFINITY because +   is already an 
                           integer value by definition. */
z = trunc(300.1);       /* z = 300.0 */
z = trunc(-300.1);      /* z = -300.0 */

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

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996