Name
lrint
Synopsis
Rounds a floating-point number to an integer
#include <math.h> longlrint
( doublex
); longlrintf
( floatx
); longlrintl
( long doublex
);
The lrint()
functions round
a floating-point number to the next integer value in the current
rounding direction. If the result is outside the range of long
, a range error may occur, depending
on the implementation, and the return value is unspecified.
Example
double t_ambient; // Ambient temperature in Celsius.
int t_display; // Display permits integer values.
char tempstring[128];
int saverounding = fegetround();
/* ... Read t_ambient from some thermometer somewhere ... */
fesetround( FE_TONEAREST ); // Round toward nearest integer, up or down.
t_display = (int)lrint
( t_ambient );
snprintf( tempstring, 128, "Current temperature: %d° C\n", t_display );
fesetround( saverounding ); // Restore rounding direction.
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.