Name

nearbyint

Synopsis

Rounds a floating-point number to an integer value

#include <math.h>
doublenearbyint( double x );
float nearbyintf( float x );
long double nearbyintl( long double x );

The nearbyint() functions round the value of the argument to the next integer value in the current rounding direction. The current rounding direction is an attribute of the floating-point environment that you can read and modify using the fegetround() and fesetround() functions. They are similar to the rint() functions, except that the nearbyint() functions do not raise the FE_INEXACT exception when the result of the rounding is different from the argument.

Example

if ( fesetround( FE_TOWARDZERO) == 0)
  printf("The current rounding mode is \"round toward 0.\"\n");
else
  printf("The rounding mode is unchanged.\n");

printf("nearbyint(1.9) = %4.1f    nearbyint(-1.9) = %4.1f\n",nearbyint(1.9), nearbyint(-1.9) );

printf("round(1.9) = %4.1f        round(-1.9) = %4.1f\n",
        round(1.9), round(-1.9) );

This code produces the following output:

The current rounding mode is "round toward 0."
nearbyint(1.9) =  1.0    nearbyint(-1.9) = -1.0
round(1.9) =  2.0        round(-1.9) = -2.0

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.