The functions listed here perform operations such as rounding and truncation of floating-point values. Some of these functions convert floating point numbers to integer values. They are all declared in math.h.
You can also convert floating-point numbers to integers simply by
casting them to int
. This discards the fractional part,
effectively rounding towards zero. However, this only works if the
result can actually be represented as an int
—for very large
numbers, this is impossible. The functions listed here return the
result as a double
instead to get around this problem.
The fromfp
functions use the following macros, from TS
18661-1:2014, to specify the direction of rounding. These correspond
to the rounding directions defined in IEEE 754-2008.
FP_INT_UPWARD
¶Round toward +∞.
FP_INT_DOWNWARD
¶Round toward -∞.
FP_INT_TOWARDZERO
¶Round toward zero.
FP_INT_TONEARESTFROMZERO
¶Round to nearest, ties round away from zero.
FP_INT_TONEAREST
¶Round to nearest, ties round to even.
double
ceil (double x)
¶float
ceilf (float x)
¶long double
ceill (long double x)
¶_FloatN
ceilfN (_FloatN x)
¶_FloatNx
ceilfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions round x upwards to the nearest integer,
returning that value as a double
. Thus, ceil (1.5)
is 2.0
.
double
floor (double x)
¶float
floorf (float x)
¶long double
floorl (long double x)
¶_FloatN
floorfN (_FloatN x)
¶_FloatNx
floorfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions round x downwards to the nearest
integer, returning that value as a double
. Thus, floor
(1.5)
is 1.0
and floor (-1.5)
is -2.0
.
double
trunc (double x)
¶float
truncf (float x)
¶long double
truncl (long double x)
¶_FloatN
truncfN (_FloatN x)
¶_FloatNx
truncfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The trunc
functions round x towards zero to the nearest
integer (returned in floating-point format). Thus, trunc (1.5)
is 1.0
and trunc (-1.5)
is -1.0
.
double
rint (double x)
¶float
rintf (float x)
¶long double
rintl (long double x)
¶_FloatN
rintfN (_FloatN x)
¶_FloatNx
rintfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions round x to an integer value according to the current rounding mode. See Floating Point Parameters, for information about the various rounding modes. The default rounding mode is to round to the nearest integer; some machines support other modes, but round-to-nearest is always used unless you explicitly select another.
If x was not initially an integer, these functions raise the inexact exception.
double
nearbyint (double x)
¶float
nearbyintf (float x)
¶long double
nearbyintl (long double x)
¶_FloatN
nearbyintfN (_FloatN x)
¶_FloatNx
nearbyintfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions return the same value as the rint
functions, but
do not raise the inexact exception if x is not an integer.
double
round (double x)
¶float
roundf (float x)
¶long double
roundl (long double x)
¶_FloatN
roundfN (_FloatN x)
¶_FloatNx
roundfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions are similar to rint
, but they round halfway
cases away from zero instead of to the nearest integer (or other
current rounding mode).
double
roundeven (double x)
¶float
roundevenf (float x)
¶long double
roundevenl (long double x)
¶_FloatN
roundevenfN (_FloatN x)
¶_FloatNx
roundevenfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions, from TS 18661-1:2014 and TS 18661-3:2015, are similar
to round
, but they round halfway cases to even instead of away
from zero.
long int
lrint (double x)
¶long int
lrintf (float x)
¶long int
lrintl (long double x)
¶long int
lrintfN (_FloatN x)
¶long int
lrintfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions are just like rint
, but they return a
long int
instead of a floating-point number.
long long int
llrint (double x)
¶long long int
llrintf (float x)
¶long long int
llrintl (long double x)
¶long long int
llrintfN (_FloatN x)
¶long long int
llrintfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions are just like rint
, but they return a
long long int
instead of a floating-point number.
long int
lround (double x)
¶long int
lroundf (float x)
¶long int
lroundl (long double x)
¶long int
lroundfN (_FloatN x)
¶long int
lroundfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions are just like round
, but they return a
long int
instead of a floating-point number.
long long int
llround (double x)
¶long long int
llroundf (float x)
¶long long int
llroundl (long double x)
¶long long int
llroundfN (_FloatN x)
¶long long int
llroundfNx (_FloatNx x)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions are just like round
, but they return a
long long int
instead of a floating-point number.
intmax_t
fromfp (double x, int round, unsigned int width)
¶intmax_t
fromfpf (float x, int round, unsigned int width)
¶intmax_t
fromfpl (long double x, int round, unsigned int width)
¶intmax_t
fromfpfN (_FloatN x, int round, unsigned int width)
¶intmax_t
fromfpfNx (_FloatNx x, int round, unsigned int width)
¶uintmax_t
ufromfp (double x, int round, unsigned int width)
¶uintmax_t
ufromfpf (float x, int round, unsigned int width)
¶uintmax_t
ufromfpl (long double x, int round, unsigned int width)
¶uintmax_t
ufromfpfN (_FloatN x, int round, unsigned int width)
¶uintmax_t
ufromfpfNx (_FloatNx x, int round, unsigned int width)
¶intmax_t
fromfpx (double x, int round, unsigned int width)
¶intmax_t
fromfpxf (float x, int round, unsigned int width)
¶intmax_t
fromfpxl (long double x, int round, unsigned int width)
¶intmax_t
fromfpxfN (_FloatN x, int round, unsigned int width)
¶intmax_t
fromfpxfNx (_FloatNx x, int round, unsigned int width)
¶uintmax_t
ufromfpx (double x, int round, unsigned int width)
¶uintmax_t
ufromfpxf (float x, int round, unsigned int width)
¶uintmax_t
ufromfpxl (long double x, int round, unsigned int width)
¶uintmax_t
ufromfpxfN (_FloatN x, int round, unsigned int width)
¶uintmax_t
ufromfpxfNx (_FloatNx x, int round, unsigned int width)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions, from TS 18661-1:2014 and TS 18661-3:2015, convert a
floating-point number to an integer according to the rounding direction
round (one of the FP_INT_*
macros). If the integer is
outside the range of a signed or unsigned (depending on the return type
of the function) type of width width bits (or outside the range of
the return type, if width is larger), or if x is infinite or
NaN, or if width is zero, a domain error occurs and an unspecified
value is returned. The functions with an ‘x’ in their names raise
the inexact exception when a domain error does not occur and the
argument is not an integer; the other functions do not raise the inexact
exception.
double
modf (double value, double *integer-part)
¶float
modff (float value, float *integer-part)
¶long double
modfl (long double value, long double *integer-part)
¶_FloatN
modffN (_FloatN value, _FloatN *integer-part)
¶_FloatNx
modffNx (_FloatNx value, _FloatNx *integer-part)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
These functions break the argument value into an integer part and a
fractional part (between -1
and 1
, exclusive). Their sum
equals value. Each of the parts has the same sign as value,
and the integer part is always rounded toward zero.
modf
stores the integer part in *integer-part
, and
returns the fractional part. For example, modf (2.5, &intpart)
returns 0.5
and stores 2.0
into intpart
.