CKTIS / C
lround — C23 mathematical function
`lround(x)` from `<math.h>` rounds a `double` to the nearest `long int` value; an exact halfway case always moves away from zero, independently of the current rounding mode. The `lroundf` and `lroundl` variants accept `float` and `long double`, respectively.
Version: C23
Syntax
long int lround(double x);lround(x) from <math.h> rounds a double to the nearest long int value; an exact halfway case always moves away from zero, independently of the current rounding mode. The lroundf and lroundl variants accept float and long double, respectively.
The example really calls lround(2.5), lround(-2.5), and lround(2.4). Output 3 -3 2 demonstrates the halfway rule on both sides of zero and an ordinary non-halfway case.
For NaN or infinity, C23 permits a domain error. If the rounded result does not fit in long, the result is an unspecified numerical value and either a domain or range error may occur; Annex F raises FE_INVALID. The function does not saturate, does not follow fesetround, and Annex F does not require FE_INEXACT for a valid non-integral argument.
Examples
#include <math.h>
#include <stdio.h>
int main(void){printf("%ld %ld %ld\n",lround(2.5),lround(-2.5),lround(2.4));return 0;}
Result
3 -3 2
The example really calls `lround(2.5)`, `lround(-2.5)`, and `lround(2.4)`. Output `3 -3 2` demonstrates the halfway rule on both sides of zero and an ordinary non-halfway case.