Wow. Thanks a lot for the info Jeff!
Francisco -------------------------------------------------- From: "Jeff Hobbs" <[email protected]> Sent: Friday, February 04, 2011 11:34 AM To: "Francisco Zarabozo" <[email protected]> Cc: <[email protected]> Subject: Re: Please, someone explain this behavior in Perl > This is due to not all floating point numbers being exactly representable > in IEEE floating point math. This is a problem across machines and > languages. More info at: > > http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems > > This is also why it can be dangerous to do floating point increments in > loops. Generally at the language level, it will depend on where the > rounding kicks in, which you can adjust for printing purposes, but the > math is only as good as the underlying hardware handles (which is why > integer-based math for decimal values in some situations is required). > > Jeff > > On 04/02/2011 9:25 AM, Francisco Zarabozo wrote: >> Hello All, >> >> Right now I'm using Active Perl 5.10.1. I've been working with Perl for >> more >> than 10 years. Yet, today is the first time I notice this wierd thing. >> Maybe >> I ignore something about how Perl's math works, yet it's driving me >> crazy. I >> can't believe this is the first time I notice this and makes me wonder >> how >> many applications I've written in the past that are actually doing this >> without me knowing about it. I'm talking about this: >> >> >> print 1.1 - 1; # Gives 0.1 >> print "\n"; >> >> print 2.1 - 2; # Gives 0.1 >> print "\n"; >> >> print 3.1 - 3; # Gives 0.1 >> print "\n"; >> >> print 4.1 - 4; # Gives 0.0999999999999996 ??? >> print "\n"; >> >> print 6.2 - 6; # Gives 0.2 >> print "\n"; >> >> print 7.2 - 7; # Gives 0.2 >> print "\n"; >> >> print 8.2 - 8; # Gives 0.199999999999999 ??? >> print "\n"; >> >> Why is this? Thanks in advance. >> >> >> Best regards, >> >> Francisco > _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
