Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > == operator acts differently in perl 5.005_03 and 5.8.2

Reply
Thread Tools

== operator acts differently in perl 5.005_03 and 5.8.2

 
 
Yahav
Guest
Posts: n/a
 
      03-21-2005
Hi,

I've noticed a behavior that i hope someone could explain:

I'm currently using perl v5.8.2 under Solaris. when trying to compare
between two numbers who should be the same using the numeric equality
operator i get that they aren't equal using v5.8.2, and equal under
5.005_03:

sample code:

perl -e 'print "\nYES\n" if (2.2e-7 == 0.22e-6);'

gives YES only when using perl 5.005_03

Is there an explanation ?

Thanks !

Yahav Bar yosef

 
Reply With Quote
 
 
 
 
Chris Mattern
Guest
Posts: n/a
 
      03-21-2005
Yahav wrote:

> Hi,
>
> I've noticed a behavior that i hope someone could explain:
>
> I'm currently using perl v5.8.2 under Solaris. when trying to compare
> between two numbers who should be the same using the numeric equality
> operator i get that they aren't equal using v5.8.2, and equal under
> 5.005_03:
>
> sample code:
>
> perl -e 'print "\nYES\n" if (2.2e-7 == 0.22e-6);'
>
> gives YES only when using perl 5.005_03
>
> Is there an explanation ?
>

Yes. Floating point is an approximation. Never compare
floating point numbers for equality, because the results
are, as you have learned, unpredictable.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      03-21-2005
Yahav <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi,
>
> I've noticed a behavior that i hope someone could explain:
>
> I'm currently using perl v5.8.2 under Solaris. when trying to compare
> between two numbers who should be the same using the numeric equality
> operator i get that they aren't equal using v5.8.2, and equal under
> 5.005_03:
>
> sample code:
>
> perl -e 'print "\nYES\n" if (2.2e-7 == 0.22e-6);'


The two numbers are different. Since division by 10 cannot be done
exactly in binary, you end up with two different approximations to
the exact value:

perl -e 'printf "%.24f %.24f\n", 2.2e-7, 0.22e-6'

prints

0.000000219999999999999985 0.000000220000000000000011

See perldoc -q "long decimals".

> gives YES only when using perl 5.005_03


I don't know why the old Perl version considers them equal. Probably
a bug that got fixed.

Anno
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-21-2005
Yahav <(E-Mail Removed)> wrote:

> I'm currently using perl v5.8.2 under Solaris. when trying to compare
> between two numbers who should be the same using the numeric equality
> operator



You cannot reliably use an equality test on floating point numbers.


> sample code:
>
> perl -e 'print "\nYES\n" if (2.2e-7 == 0.22e-6);'
>
> gives YES only when using perl 5.005_03
>
> Is there an explanation ?



perldoc -q 9999

Why am I getting long decimals (eg, 19.9499999999999) instead of the
numbers I should be getting (eg, 19.95)?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Yahav
Guest
Posts: n/a
 
      03-21-2005
Hello All,

First, i would like to thank you for your quick respond.
Second, my current solution is to multiply each of the numbers by one,
and then i get the same number...
Maybe there's other (maybe better) ways of overtaking this..

Thanks,
Yahav Bar yosef




Bernard El-Hagin wrote:
> Tad McClellan <(E-Mail Removed)> wrote:
>
> [...]
>
> > perldoc -q 9999

>
> Or the golf version:
>
>
> perldoc -q 9
>
>
>
>
>
> --
> Cheers,
> Bernard


 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      03-21-2005
Yahav wrote:

> Hello All,
>
> First, i would like to thank you for your quick respond.
> Second, my current solution is to multiply each of the numbers by one,
> and then i get the same number...
> Maybe there's other (maybe better) ways of overtaking this..
>

1) Don't top post.

2) You've thanked us for our responses, but you haven't
listened to the answers you got here. DO NOT compare
floating point numbers for equality. You found a hack
that finagles the result you wanted in this particular case.
It won't work elsewhere. There's an excellent chance that
this exact code won't work on another box. It may not work
on the box you're programming *now* if you upgrade anything.
You will get nothing but heartbreak if you continue this way.
Read the FAQ that was indicated to you. There are packages
that allow you to use numbers other than standard floating
point, if your problem absolutely *requires* that you be
able to compare for equality.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
Reply With Quote
 
Yahav
Guest
Posts: n/a
 
      03-21-2005
OK - i fixed it. Thanks !

Yahav Bar yosef

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-22-2005
Yahav wrote:
> Second, my current solution is to multiply each of the numbers by one,
> and then i get the same number...
> Maybe there's other (maybe better) ways of overtaking this..


Arrrrg, if you quoting Tad's hint for the FAQ, then why don't you actually
read _and_follow_ it?
Obviously you must have missed the first class in Introduction to Computer
Numerics, therefore here a summary:

Thou shalt not compare floating point numbers for equality!

If you must then use an epsilon distance instead.

jue



 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-22-2005
Jürgen Exner <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Yahav wrote:
> > Second, my current solution is to multiply each of the numbers by one,
> > and then i get the same number...
> > Maybe there's other (maybe better) ways of overtaking this..

>
> Arrrrg, if you quoting Tad's hint for the FAQ, then why don't you actually
> read _and_follow_ it?
> Obviously you must have missed the first class in Introduction to Computer
> Numerics, therefore here a summary:
>
> Thou shalt not compare floating point numbers for equality!
>
> If you must then use an epsilon distance instead.


A quick and dirty way is to use string compare for floats.

Anno
 
Reply With Quote
 
Yahav
Guest
Posts: n/a
 
      03-23-2005
Hi jue

Thanks for your concerns, i've found another good solution.

Yahav

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UpdatePanel acts differently inside Content area of Master Page John Kotuby ASP .Net 3 02-15-2008 01:10 AM
subprocess call acts differently than command line call? Jim Python 2 06-07-2007 07:58 PM
\Q acts differently in s/// and m// operations gargoyle Perl Misc 3 02-17-2005 10:57 PM
Web Form acts differently when using C# than VB Joe Fallon ASP .Net 1 02-15-2004 10:53 PM
"perl -MCPAN -e shell" acts unexpectedly on MacOS X 10.3.1 Art Werschulz Perl 0 12-03-2003 02:17 AM



Advertisments