Page 1 of 1

Bug? PHP says (7.11 == 7.11) is FALSE!

Posted: Sun Jun 23, 2002 7:43 pm
by gxpark
Hi,

Take a look at this code:

Code: Select all

$sec = $Cur_Sub2Sec;
    settype($sec, 'double');
    echo gettype($sec) . '-' . gettype($Sub2Secsї'services.email.compose']);
    echo "<br>&#1111;".$sec."]";
    echo "&#1111;".$Sub2Secs&#1111;'services.email.compose']."]";
    echo !($sec == $Sub2Secs&#1111;'services.email.compose']);
That results in:

double-double
[7.11][7.11]1

What does that say? Well, first, both variables ($sec and $Sub2Secs['services.email.compose']) are of the same type (double). Also, both variables have the same value. So why the heck does PHP return "true" to the negation of the equality? I mean, is like if PHP returned false for "7.11 == 7.11".

Maybe I'm missing something really simple... And I hope so :lol:

Thanks in advance.

Posted: Mon Jun 24, 2002 1:14 am
by protokol
this may not be the correct answer, but it's worth a try..

instead of comparing using '==', try '===' which means "equal values and of the same data type"

Posted: Mon Jun 24, 2002 9:31 am
by gxpark
Doesn't work either :)

If == doesn't work, you'd think === wouldn't work either, and it doesn't (I also tried it).

Thanks anyway.

Posted: Mon Jun 24, 2002 9:36 am
by Bill H
Your problem probably has to do with the fact that floating point numbers are not stored precisely and should not be compared directly.

For instance 7.11 might be stored as 7.1099999999 or 7.11000000001.

You should use a process of rounding the numbers, or subtract one from the other and examine the size of the difference.

Posted: Mon Jun 24, 2002 10:07 am
by gxpark
Ah, great, that was the culprit.

I solved it by converting both comparing values to 'strings'. Would this be the correct approach?

On the other hand, I don't know why in heaven's sake aren't the numbers stored the way they should. It's like when people learning math miss a solution by a decimal, and say, "But it's only a decimal!", yeah well, you're wrong.

Thanks a lot!

Posted: Mon Jun 24, 2002 2:14 pm
by Bill H
I solved it by converting both comparing values to 'strings'. Would this be the correct approach?
Yes, depending on how you create the strings. If you let the strings be of unlimited length, then it might not always work, but formatting the strings at the number of useful digits "east" of the decimal point should always work.
On the other hand, I don't know why in heaven's sake aren't the numbers stored the way they should.
Well, remember the computer only has 1's and 0's to work with. Given that limitation, I think computers do a superb job of storing floating point numbers.

Posted: Mon Jun 24, 2002 2:22 pm
by volka
you may also subtract one value from the other and check if the (absolute) result is within an 'epsilon'-range. Something like

Code: Select all

function equalFloats($a, $b, $epsilon)
&#123;
   if (abs($a - $b) <= abs($epsilon))
      return TRUE;
   else
      return FALSE;
&#125;

Posted: Mon Jun 24, 2002 5:24 pm
by gxpark
Thank you guys :)