nearbyint, nearbyintf, nearbyintl

Rounds the specified floating-point value to an integer, and returns that value in a floating-point format.


double nearbyint( double x );
float nearbyintf( float x );
long double nearbyintl( long double x );
float nearbyint( float x ); //C++ only
long double nearbyint( long double x ); //C++ only


The value to round.

Return Value

If successful, returns x, rounded to the nearest integer, using the current rounding format as reported by fegetround. Otherwise, the function may return one of the following values:

Issue Return
x = ±INFINITY ±INFINITY, unmodified
x = ±0 ±0, unmodified
x = NaN NaN

Errors are not reported through _matherr; specifically, this function does not report any FE_INEXACT exceptions.


The primary difference between this function and rint is that this function does not raise the inexact floating point exception.

Because the maximum floating-point values are exact integers, this function will never overflow by itself; rather, the output may overflow the return value, depending on which version of the function you use.

C++ allows overloading, so you can call overloads of nearbyint that take and return float or long double parameters. In a C program, nearbyint always takes two double values and returns a double value.


Function C header C++ header
nearbyint, nearbyintf, nearbyintl <math.h> <cmath> or <math.h>

For additional compatibility information, see Compatibility.

See also

Alphabetical Function Reference
Math and floating-point support