Please, someone explain this behavior in Perl

Please, someone explain this behavior in Perl

am 04.02.2011 18:25:57 von fzarabozo

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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Please, someone explain this behavior in Perl

am 04.02.2011 18:34:08 von Jeff Hobbs

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_problem s

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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Please, someone explain this behavior in Perl

am 04.02.2011 18:40:33 von Brian Raven

-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Francisco Zarabozo
Sent: 04 February 2011 17:26
To: activeperl@listserv.activestate.com
Subject: Please, someone explain this behavior in Perl

> 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.
>

Sounds like a FAQ. See 'perldoc -q numbers'.

HTH


--
Brian Raven

Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Please, someone explain this behavior in Perl

am 04.02.2011 18:47:25 von fzarabozo

Wow.

Thanks a lot for the info Jeff!

Francisco

--------------------------------------------------
From: "Jeff Hobbs"
Sent: Friday, February 04, 2011 11:34 AM
To: "Francisco Zarabozo"
Cc:
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_problem s
>
> 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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Please, someone explain this behavior in Perl

am 04.02.2011 22:31:14 von David Dick

On 05/02/11 04:34, Jeff Hobbs wrote:
> 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_problem s
>
> 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).

For precision math, try the Math::BigFloat library. Very handy.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs