== operator acts differently in perl 5.005_03 and 5.8.2

Discussion in 'Perl Misc' started by Yahav, Mar 21, 2005.

  1. Yahav

    Yahav Guest

    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
     
    Yahav, Mar 21, 2005
    #1
    1. Advertising

  2. 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?"
     
    Chris Mattern, Mar 21, 2005
    #2
    1. Advertising

  3. Yahav

    Anno Siegel Guest

    Yahav <> 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
     
    Anno Siegel, Mar 21, 2005
    #3
  4. Yahav <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 21, 2005
    #4
  5. Yahav

    Yahav Guest

    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 <> wrote:
    >
    > [...]
    >
    > > perldoc -q 9999

    >
    > Or the golf version:
    >
    >
    > perldoc -q 9
    >
    >
    > ;-)
    >
    >
    > --
    > Cheers,
    > Bernard
     
    Yahav, Mar 21, 2005
    #5
  6. 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?"
     
    Chris Mattern, Mar 21, 2005
    #6
  7. Yahav

    Yahav Guest

    OK - i fixed it. Thanks !

    Yahav Bar yosef
     
    Yahav, Mar 21, 2005
    #7
  8. 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
     
    Jürgen Exner, Mar 22, 2005
    #8
  9. Yahav

    Anno Siegel Guest

    Jürgen Exner <> 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
     
    Anno Siegel, Mar 22, 2005
    #9
  10. Yahav

    Yahav Guest

    Hi jue

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

    Yahav
     
    Yahav, Mar 23, 2005
    #10
  11. Yahav <> wrote:


    > i've found another good solution.



    Please be a good Net Citizen and *post* the solution so that other's
    with the same problem might find out how you fixed it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 23, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Fallon
    Replies:
    1
    Views:
    427
    Peter Blum
    Feb 15, 2004
  2. Jim
    Replies:
    2
    Views:
    459
  3. John Kotuby
    Replies:
    3
    Views:
    2,610
    John Kotuby
    Feb 15, 2008
  4. Jon Reed

    Is perl 5.8 slower than 5.005_03?

    Jon Reed, Dec 4, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    208
    Ilya Zakharevich
    Dec 4, 2003
  5. gargoyle
    Replies:
    3
    Views:
    123
    Ilya Zakharevich
    Feb 17, 2005
Loading...

Share This Page