trunc, truncf, truncl
Determines the nearest integer that is less than or equal to the specified floating-point value.
Syntax
double trunc( double x );
long double truncl( long double x );
#define trunc(X) // Requires C11 or higher
long double trunc( long double x ); //C++ only
float trunc( float x ); //C++ only
Parameters
x
The value to truncate.
Return Value
If successful, returns an integer value of x, rounded towards zero.
Otherwise, may return one of the following:
| Issue | Return |
|---|---|
| x = ±INFINITY | x |
| x = ±0 | x |
| x = NaN | NaN |
Errors are reported as specified in _matherr.
Remarks
Because C++ allows overloading, you can call overloads of trunc that take and return float and long double types. In a C program, unless you're using the <tgmath.h> macro to call this function, trunc always takes and returns a double.
If you use the <tgmath.h> trunc() macro, the type of the argument determines which version of the function is selected. See Type-generic math for details.
Because the largest floating-point values are exact integers, this function will not overflow on its own. However, you may cause the function to overflow by returning a value into an integer type.
You can also round down by implicitly converting from floating-point to integral; however, doing so is limited to the values that can be stored in the target type.
Requirements
| Function | C header | C++ header |
|---|---|---|
| trunc, truncf, truncl | <math.h> | <cmath> |
| trunc macro | <tgmath.h> |
For additional compatibility information, see Compatibility.
See also
Alphabetical Function Reference
floor, floorf, floorl
ceil, ceilf, ceill
round, roundf, roundl
Povratne informacije
Pošalјite i prikažite povratne informacije za