New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is this a bug? #6569
Comments
From giswty@nus.edu.sgHi there, I'm new to perl and was writing a small program. Found something funny. When I try to add this particular sequence of numbers (0.4+0.3+0.2+0.1), it does not add up to one. (falls to the else loop). If I change these numbers to like 0.25+0.5+0.25 OR 0.9+0.1 etc then no problem. Here is the code snippet: ************************* my $tmp = 0.4+0.3+0.2+0.1; thanks! |
From Robin.Barker@npl.co.ukThis is not a bug, see C<perldoc -f decimal> for a full explanation. If you add a line after 'print "Not equal to one", "\n";' Not equal to one Robin # New Ticket Created by "Marie Vivian Wong Tzu Yenn" Hi there, I'm new to perl and was writing a small program. Found something funny. If I change these numbers to like 0.25+0.5+0.25 OR 0.9+0.1 etc then no Here is the code snippet: ************************* my $tmp = 0.4+0.3+0.2+0.1; thanks! This e-mail and any attachments may contain confidential and/or NPL Management Ltd cannot guarantee that the e-mail or any NPL Management Ltd. Registered in England and Wales. No: 2937881 |
From @demerphq
At first I thought this was a classic example of rounding problems. Assuming D:\Development>perl -e "printf '%22.20f',0.4+0.3+0.2+0.1" Whereas the binary representation of the other examples (0.9,0.1 and D:\Development>perl -e "printf '%22.20f',0.25+0.5+0.25" But the below makes me think that this is indeed a bug. D:\Development>perl -e "printf '%22.20f',0.4 When I take these results and sum them in my handy dandy desk calculator I 1.00000000000000003 So now im not sure that this isnt a bug. But i dont know where it lies, or However, the GOOD news is that your problem can be worked around by simply D:\Development>perl -e "print 0.4+0.3+0.2+0.1 == 1 ? 'Equals' : 'NotEquals'" Ill leave it up to somebody else to determine if it is in fact a bug, and/or Yves |
From Robin.Barker@npl.co.ukI wrote:
I meant: Robin This e-mail and any attachments may contain confidential and/or NPL Management Ltd cannot guarantee that the e-mail or any NPL Management Ltd. Registered in England and Wales. No: 2937881 |
From @mhx
Nope, no bug here. Just the classic rounding problem: E:\>type add.c int main( int argc, char **argv ) E:\>.\add 0.4 0.3 0.2 0.1 E:\>perl -e"printf qq'%22.20f\n', 0.4+0.3+0.2+0.1" Remember, the precision of a "double" mantissa is only 0.00000 00000 00000 1 So the error is quite ok after 3 floating point operations. However, the easiest workaround is obviously to just write E:\>perl -e"printf qq'%22.20f\n', 0.4+0.2+0.1+0.3" ;-) -- Marcus |
From @mhxRE: [perl #22725] is this a bug?> > Remember, the precision of a "double" mantissa is only
Yes. E:\>type float.pl As you can see, initializing $b to zero and adding 1e-16 for 100000 That's what the second example shows: initializing $b to one and
Although I'm not an expert (so don't blame me if I'm wrong ;-) I'd -- Marcus
|
From @pjcjOrton, Yves said:
Here's a patch for the bug: *** pp_hot.c.org Thu Jul 18 23:32:07 2002 *** pp.c.org Tue Jun 18 22:26:49 2002 ./perl -we "print 0.4+0.3+0.2+0.1 == 1 ? 'Equals' : 'NotEquals'" -- |
From perl5-porters@ton.iguana.beIn article <18286.193.134.254.145.1055950103.squirrel@wesley.pjcj.net>,
No. That would be a cure worse than the disease. Many floating point perl -wle '$a=2.; print "yes" if $a+3. == 5.' |
From @demerphq
Nice! Yves |
@rspier - Status changed from 'new' to 'resolved' |
From @pjcjOn Wed, Jun 18, 2003 at 04:25:45PM +0000, Ton Hospel wrote:
In case anyone was unsure, the patch was not meant to be applied.
However, this case would not have triggered the warning: ./perl -wle '$a=2.; print "yes" if $a+3. == 5.' ./perl -wle '$a=2.1; print "yes" if $a+3.1 == 5.2' -- |
@rgs - Status changed from 'open' to 'resolved' |
From nick.ing-simmons@elixent.comMarie Vivian Wong Tzu Yenn <perl5-porters@perl.org> writes:
This isn't a perl problem it is a generic floating point problem common The problem is that just as you cannot represent 1/6 or 1/3 as an exact
0.25 and 0.5 are exact in binary. 0.9 and 0.1 are like 0.667 + 0.333 -- |
From acme@astray.comVery amusing discussion but not a bug. |
acme@astray.com - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#22725 (status was 'resolved')
Searchable as RT22725$
The text was updated successfully, but these errors were encountered: