S
Spoon
Hello everyone,
I don't understand how the lrint() function works.
long lrint(double x);
The function returns the nearest long integer to x, consistent with the
current rounding mode. It raises an invalid floating-point exception if
the magnitude of the rounded value is too large to represent. And it
raises an inexact floating-point exception if the return value does not
equal x.
The output of the following program does not make sense to me.
#include <cstdio>
#include <cmath>
int main()
{
for (int i=-10; i < 10; ++i)
{
double x = i + 0.5;
printf("lrint(%+f)=%ld\n", x, lrint(x));
}
return 0;
}
$ g++ -std=c++98 -Wall -Wextra -O2 -march=pentium3 foo.cxx -lm
$ ./a.out
lrint(-9.500000)=-10
lrint(-8.500000)=-8
lrint(-7.500000)=-8
lrint(-6.500000)=-6
lrint(-5.500000)=-6
lrint(-4.500000)=-4
lrint(-3.500000)=-4
lrint(-2.500000)=-2
lrint(-1.500000)=-2
lrint(-0.500000)=0
lrint(+0.500000)=0
lrint(+1.500000)=2
lrint(+2.500000)=2
lrint(+3.500000)=4
lrint(+4.500000)=4
lrint(+5.500000)=6
lrint(+6.500000)=6
lrint(+7.500000)=8
lrint(+8.500000)=8
lrint(+9.500000)=10
How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?
Regards.
I don't understand how the lrint() function works.
long lrint(double x);
The function returns the nearest long integer to x, consistent with the
current rounding mode. It raises an invalid floating-point exception if
the magnitude of the rounded value is too large to represent. And it
raises an inexact floating-point exception if the return value does not
equal x.
The output of the following program does not make sense to me.
#include <cstdio>
#include <cmath>
int main()
{
for (int i=-10; i < 10; ++i)
{
double x = i + 0.5;
printf("lrint(%+f)=%ld\n", x, lrint(x));
}
return 0;
}
$ g++ -std=c++98 -Wall -Wextra -O2 -march=pentium3 foo.cxx -lm
$ ./a.out
lrint(-9.500000)=-10
lrint(-8.500000)=-8
lrint(-7.500000)=-8
lrint(-6.500000)=-6
lrint(-5.500000)=-6
lrint(-4.500000)=-4
lrint(-3.500000)=-4
lrint(-2.500000)=-2
lrint(-1.500000)=-2
lrint(-0.500000)=0
lrint(+0.500000)=0
lrint(+1.500000)=2
lrint(+2.500000)=2
lrint(+3.500000)=4
lrint(+4.500000)=4
lrint(+5.500000)=6
lrint(+6.500000)=6
lrint(+7.500000)=8
lrint(+8.500000)=8
lrint(+9.500000)=10
How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?
Regards.