473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

lrint and rounding mode

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.
Sep 18 '07 #1
5 7982
On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhostwrote:
>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.
<...>
>
lrint(+1.500000)=2
lrint(+2.500000)=2

How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?
I have seen it called the "banker rounding" (in spanish, but I suppose
it should be the same in english.

If the fractional part of the number is less than 0.5, it is rounded
down. If it is more than 0.5, it is rounded up. It it is exactly 0.5,
then if the integral part is odd, it is rounded up, else it is rounded
down.

It makes the rounding smoother, and the probability of accounting and
audit results being the same, higher.

best regards,

Zara
Sep 18 '07 #2
Zara wrote:
Spoon wrote:
>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.

lrint(+1.500000)=2
lrint(+2.500000)=2

How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?

I have seen it called the "banker rounding" (in spanish, but I suppose
it should be the same in english.

If the fractional part of the number is less than 0.5, it is rounded
down. If it is more than 0.5, it is rounded up. If it is exactly 0.5,
then if the integral part is odd, it is rounded up, else it is rounded
down.

It makes the rounding smoother, and the probability of accounting and
audit results being the same, higher.
I had not read the description of "round to nearest" carefully.

"Round toward the nearest integer with the closer value, or toward the
nearest even integer if two integers are equally near."

Therefore, by definition, "round to nearest" in C and C++ is different
from the mathematicians' "round to nearest".

In math, 2.5 is rounded to 3. In C++, 2.5 is rounded to 2.

Thanks for making me aware of this fact.
Sep 18 '07 #3
On Sep 18, 11:20 am, Spoon <root@localhostwrote:
I don't understand how the lrint() function works.
long lrint(double x);
Just a technicality, but this function doesn't exist (yet) in
C++; it's part of C99. (On the other hand, it will be part of
the next C++ standard, and I would expect most implementations
of C++ to support it already.)
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?
Those are the rules for IEEE round to nearest mode. In the case
of .5, it rounds to even. The reason for this is so that there
will be no overall bias in the rounding; if you use the
"standard" rounding, with 0.5 rounding up, you introduce a bias
towards higher values. (If you add the rounded positive values
in your example, the results will be very close to the results
of adding the unrounded values if IEEE round to nearest is used;
with the classical commercial rounding, they would be
significantly greater.)

Note that you cannot always use this rounding in commercial
software; most countries have laws concerning what is good
bookkeeping practices, and impose very specific rules of
rounding, including the fact that 0.5 cents always rounds away
from 0. (Of course, since the rules are based on decimal
values, you generally can't use double directly anyway.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 19 '07 #4
On Sep 18, 12:22 pm, Zara <yoz...@terra.eswrote:
On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhostwrote:
[...]
lrint(+1.500000)=2
lrint(+2.500000)=2
How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?
I have seen it called the "banker rounding" (in spanish, but I suppose
it should be the same in english.
I think you've got it backwards. In banking, .5 always rounds
away from zero. It's the specialists in numeric analysis (the
mathematiciens) who insist on round to even; it means that the
average error will be 0, instead of being slightly greater than
zero.
If the fractional part of the number is less than 0.5, it is
rounded down. If it is more than 0.5, it is rounded up. It it
is exactly 0.5, then if the integral part is odd, it is
rounded up, else it is rounded down.
It makes the rounding smoother, and the probability of
accounting and audit results being the same, higher.
Accounting and audit results will be the same if both use the
same rounding rules (including where the rounding occurs). In
most countries, these rules are imposed by law, and require that
0.005 be rounded to 0.01. (In the EU, the law actually requires
that all calculations be done with 5 decimal digits precision,
and that the final result be rounded to 2 decimal digits.) Note
too that because these rules are normally expressed in terms of
decimal arithmetic and rounding of decimal digits, you can't use
machine floating point directly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 19 '07 #5
On 2007-09-19 04:15:26 -0400, James Kanze <ja*********@gmail.comsaid:
On Sep 18, 12:22 pm, Zara <yoz...@terra.eswrote:
>On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhostwrote:

[...]
>>lrint(+1.500000)=2
lrint(+2.500000)=2
>>How could 1.5 AND 2.5 be rounded to 2 in the same rounding mode?
>I have seen it called the "banker rounding" (in spanish, but I suppose
it should be the same in english.

I think you've got it backwards. In banking, .5 always rounds
away from zero. It's the specialists in numeric analysis (the
mathematiciens) who insist on round to even; it means that the
average error will be 0, instead of being slightly greater than
zero.
Nevertheless, I've also heard "round to even" called "banker's
rounding." And that most reliable of sources, Wikipedia, agrees:
http://en.wikipedia.org/wiki/Roundin...to-even_method.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: deanfamily11 | last post by:
Say I have the number 15.7, how can I round it up to 16?
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
11
by: The Cool Giraffe | last post by:
I have trouble making the following line compile. int a = round (2.6); I have, of course, included <cmathand i can go flooring and ceiling with no problems. However, the rounding seems...
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: kwikius | last post by:
"kwikius" <andy@servocomm.freeserve.co.ukwrote in message news:a414dd7f-9b96-40e4-820e- <..> The problem is that the mantissa is only (say) 4 bytes and Actually the last 2 para are in conflict....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.