|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28515 A simple comparision operation FAILS!
Submitted: 2004-05-25 06:20 UTC Modified: 2004-05-25 11:18 UTC
From: andrew dot panin at nvkz dot net Assigned:
Status: Not a bug Package: Output Control
PHP Version: Irrelevant OS: Windows 98 SE
Private report: No CVE-ID: None
 [2004-05-25 06:20 UTC] andrew dot panin at nvkz dot net
Just look into the code!

Reproduce code:
 $d = 0;
 $d = 3*abs(strlen($in)/3 - floor(strlen($in)/3));

  print $d;
//here $d=1, as it PHP print

 if ($d == 1) { print "E"; }
//The "E" is NOT prints!


Expected result:
That the "E" will be shown.

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-25 11:18 UTC]
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.
Thank you for your interest in PHP.

Look at this:
marcus@frodo php-cvs $ php -r \'var_dump(3*(7/3 - 2) == 1);\'

(7/3-2) == 1/3 but a float can never be 1/3 exact.
E.g. floats cannot handle fractions.
 [2004-05-25 11:18 UTC] lars_stegelitz at col dot wunderman dot com
This happens on 4.3.7dev too, but...

It seems to be a representation problem of 'print $d;'.
If you modify your if-clause the following way, it shows what I mean:

if ($d > 1) { print "E"; }

Now, the 'E' is printed ! It seems, that the value of $d is NOT exactly one :

if (((int)$d) == 1) { print "E"; }

Whent $d is converted to integer, the 'E' is printed also. 
Use number_format($d, 20); to see an internal value representation of $d (it reads 1.00000000000000044409). 

When working with floating point values, never ever test on equality!
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 09:01:27 2024 UTC