php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andrew dot panin at nvkz dot net
New email:
PHP Version: OS:

 

 [2004-05-25 06:20 UTC] andrew dot panin at nvkz dot net
Description:
------------
Just look into the code!

Reproduce code:
---------------
<?php
 $in="1234567";
 $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!

 die();
?>

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

Actual result:
--------------
NOTHING!!!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-25 11:18 UTC] helly@php.net
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);\'
bool(false)

(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: Fri Apr 19 08:01:28 2024 UTC