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. 5 8015
On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhost wrote:
>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.50000 0)=2 lrint(+2.50000 0)=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
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.50000 0)=2 lrint(+2.50000 0)=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.
On Sep 18, 11:20 am, Spoon <root@localhost wrote:
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Sep 18, 12:22 pm, Zara <yoz...@terra.e swrote:
On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhost wrote:
[...]
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On 2007-09-19 04:15:26 -0400, James Kanze <ja*********@gm ail.comsaid:
On Sep 18, 12:22 pm, Zara <yoz...@terra.e swrote:
>On Tue, 18 Sep 2007 11:20:45 +0200, Spoon <root@localhost wrote:
[...]
>>lrint(+1.5000 00)=2 lrint(+2.5000 00)=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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: deanfamily11 |
last post by:
Say I have the number 15.7, how can I round it up to 16?
|
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
---------------------
2004-05-27 09:00:01
|
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 seems to be done like this
3.435 = 3.44
3.445 = 3.44
Dim decNbr1 As Decimal
|
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
------------------
b += (float)(mode *val);
On 32 bit(intel , vs 2003, C++), some watch variables are
|
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 not to
| |
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 an item is 41.87 i want the application to bring it up to 41.89
if an item is 41.84 i want the application to round down to 41.79
does anyone know how i could do this?
|
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)
It then updates the value with numberOfPrecisions after the decimal
|
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>
|
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. In reality what happens to any
underflow is implementation defined, but there are various rounding schemes
that deal with it.
regards
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |