C <math.h> - lrint() Function
The C <math.h> lrint() function rounds the specified number to an integral value, using the rounding direction specified by fegetround, and returns it as long int type.
The llrint() function is similar to this function, except it returns the result as long long int type.
Syntax
long int lrint (double x); long int lrintf (float x); long int lrintl (long double x);
Parameters
x |
Specify a value to round. |
Return Value
Returns the value of x which is first rounded to nearby integral value then casted to long int type.
Example:
In the example below, lrint() 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("lrint(10.2): %ld\n", lrint(10.2)); printf("lrint(10.8): %ld\n", lrint(10.8)); printf("lrint(-5.2): %ld\n", lrint(-5.2)); printf("lrint(-5.8): %ld\n", lrint(-5.8)); return 0; }
The output of the above code will be:
Rounding using to-nearest method: lrint(10.2): 10 lrint(10.8): 11 lrint(-5.2): -5 lrint(-5.8): -6
❮ C <math.h> Library