Important: The information in this document is obsolete and should not be used for new development.
trunc
You can use thetrunc
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
Thetrunc
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 is exact.SPECIAL CASES
Table 9-7 shows the results when the argument to thetrunc
function is a zero, a NaN, or an Infinity.
Special cases for the trunc
functionOperation Result Exceptions raised +0 None None NaN None[18] + None 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.