== operator acts differently in perl 5.005_03 and 5.8.2

Y

Yahav

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
 
C

Chris Mattern

Yahav said:
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?"
 
A

Anno Siegel

Yahav said:
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
 
T

Tad McClellan

Yahav said:
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)?
 
Y

Yahav

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



[...]
perldoc -q 9999

Or the golf version:


perldoc -q 9


;-)
 
C

Chris Mattern

Yahav said:
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?"
 
J

Jürgen Exner

Yahav said:
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
 
A

Anno Siegel

Jürgen Exner said:
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
 
T

Tad McClellan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top