PHP Subtraction/Addition and Comparison

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timetrial
Forum Newbie
Posts: 4
Joined: Wed May 10, 2006 5:43 pm

PHP Subtraction/Addition and Comparison

Post 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 ...
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

Not 100% certain, but try changing

this

Code: Select all

==0.6
to this

Code: Select all

==.6
in the first 2
timetrial
Forum Newbie
Posts: 4
Joined: Wed May 10, 2006 5:43 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
timetrial
Forum Newbie
Posts: 4
Joined: Wed May 10, 2006 5:43 pm

Post 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?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Perhaps to solve the problem you can enclose the variables with single quotes.
timetrial
Forum Newbie
Posts: 4
Joined: Wed May 10, 2006 5:43 pm

Post 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?
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Interesting ! Never found this kind of problem .
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
Post Reply