C <math.h> - nearbyint() Function
The C <math.h> nearbyint() function returns an integral value by rounding up the specified number, using the rounding direction specified by fegetround.
This function does not raise FE_INEXACT exceptions. The rint() function is similar to this function, except it may raise FE_INEXACT exceptions if the returned value differs from x.
Syntax
double nearbyint (double x); float nearbyintf (float x); long double nearbyintl (long double x);
Parameters
x |
Specify a value to round. |
Return Value
Returns an integral value by rounding up the x, using the rounding direction specified by fegetround.
Example:
In the example below, nearbyint() function is used to round the given number.
#include <stdio.h> #include <fenv.h> #include <math.h> void Rounding_Direction_Message(void) { printf("Rounding using "); switch(fegetround()) { case FE_DOWNWARD: printf("downward"); break; case FE_TONEAREST: printf("to-nearest"); break; case FE_TOWARDZERO: printf("toward-zero"); break; case FE_UPWARD: printf("upward"); break; default: printf("unknown"); } printf(" method:\n"); } int main (){ Rounding_Direction_Message(); printf("nearbyint(10.2): %.1f\n", nearbyint(10.2)); printf("nearbyint(10.8): %.1f\n", nearbyint(10.8)); printf("nearbyint(-5.2): %.1f\n", nearbyint(-5.2)); printf("nearbyint(-5.8): %.1f\n", nearbyint(-5.8)); return 0; }
The output of the above code will be:
Rounding using to-nearest method: nearbyint(10.2): 10.0 nearbyint(10.8): 11.0 nearbyint(-5.2): -5.0 nearbyint(-5.8): -6.0
❮ C <math.h> Library