C <math.h> - rint() Function
The C <math.h> rint() function returns an integral value by rounding up the specified number, using the rounding direction specified by fegetround.
This function may raise FE_INEXACT exceptions, if the returned value differs from x. The nearbyint() function is similar to this function, except it do not raise FE_INEXACT exceptions.
Syntax
double rint (double x); float rintf (float x); long double rintl (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, rint() 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("rint(10.2): %.1f\n", rint(10.2)); printf("rint(10.8): %.1f\n", rint(10.8)); printf("rint(-5.2): %.1f\n", rint(-5.2)); printf("rint(-5.8): %.1f\n", rint(-5.8)); return 0; }
The output of the above code will be:
Rounding using to-nearest method: rint(10.2): 10 rint(10.8): 11 rint(-5.2): -5 rint(-5.8): -6
❮ C <math.h> Library