Page 1 of 1

PHP Subtraction/Addition and Comparison

Posted: Wed May 10, 2006 5:57 pm
by timetrial
Hi PHP experts,

The following very simple piece of code and the resultung output is a mystery for me:

Code: Select all

<?php

$x=2.1 - 1.5;

if ($x==0.6)
  echo "Equal";
else
  echo "Oops!";

$y=0.2+0.4;

if ($y==0.6)
  echo "Equal";
else
  echo "Oops!";

$z=1.2+0.1;

if ($z==1.3)
  echo "Equal";
else
  echo "Oops!";

?>

On PHP 4.0 I get the resulting output:

Oops!Oops!Equal


This means the first two comparisons have the result FALSE, which I do not understand.
Can anybody explain this behaviour?


Thank you in advance ...

Posted: Wed May 10, 2006 6:33 pm
by litebearer
Not 100% certain, but try changing

this

Code: Select all

==0.6
to this

Code: Select all

==.6
in the first 2

Posted: Wed May 10, 2006 6:48 pm
by timetrial
litebearer wrote:Not 100% certain, but try changing

this

Code: Select all

==0.6
to this

Code: Select all

==.6
in the first 2

Sorry, but it does not change the result. Even when I compare two variables
the same behavoiur occurs.
If you are interested check the example on your machine.

Posted: Wed May 10, 2006 7:14 pm
by Christopher
It is interesting that following works:

Code: Select all

if ($x > 0.6)
  echo "Equal";
else
  echo "Oops!";
but not:

Code: Select all

if ($x <= 0.6)
  echo "Equal";
else
  echo "Oops!";
Send in a bug report.

Posted: Thu May 11, 2006 2:31 am
by timetrial
arborint wrote:It is interesting that following works:

Code: Select all

if ($x > 0.6)
  echo "Equal";
else
  echo "Oops!";
but not:

Code: Select all

if ($x <= 0.6)
  echo "Equal";
else
  echo "Oops!";
Send in a bug report.

I have already seen this strange behaviour when checking '<', '>' - comparisons but I could
not believe it is an PHP-error because this can (and will) happen in thousands of applications-
so I thought I made something wrong.
I wonder if noone has seen the problem before, because PHP is used in so many applications.

By the way, where do I send in a bug report?

Posted: Thu May 11, 2006 2:46 am
by Maugrim_The_Reaper
This is not a bug per se... It is (of course) but the PHP devs have never bothered to do something about it since the problem is in how floats are represented within PHP's source. Stupid bug that has become one of those oddities in PHP no one has fixed.

PHP has "issues" with floats where precision is important. A rule of thumb - never compare floats. It's a horror of a mess, but PHP just cannot handle it... :roll:

Posted: Thu May 11, 2006 3:49 am
by Benjamin
Perhaps to solve the problem you can enclose the variables with single quotes.

Posted: Thu May 11, 2006 4:16 am
by timetrial
agtlewis wrote:Perhaps to solve the problem you can enclose the variables with single quotes.
I could cast variables to strings and then compare strings which may also cause problems in some cases.

I think it is better to round the floats (up to a given precision value) and then compare them.

The following function does what I need:

Code: Select all

function fequal($x,$y,$prec)
{
   return(round($x*pow(10,$prec))==round($y*pow(10,$prec)));
}

Maybe I should avoid the pow-function to save time and use a table representation (for 10,100,1000,...) instead?

Posted: Thu May 11, 2006 5:37 am
by dibyendrah
Interesting ! Never found this kind of problem .

Posted: Thu May 11, 2006 6:13 am
by Maugrim_The_Reaper
Another possible method... Treat the integer variable as a string and strip out the decimal point for comparison - presumably removing the decimal, and casting this secondary comparative data to integer will do away with precision errors.