php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67501 strange operation of comparison of same value
Submitted: 2014-06-23 11:15 UTC Modified: 2014-06-24 06:51 UTC
From: blazexerit at yahoo dot co dot jp Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.13 OS: Linux
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: blazexerit at yahoo dot co dot jp
New email:
PHP Version: OS:

 

 [2014-06-23 11:15 UTC] blazexerit at yahoo dot co dot jp
Description:
------------
after Test script

php > var_dump($now,$next);
int(1060)
double(1060)
php > if($now == $next) echo 1,PHP_EOL; else echo 0,PHP_EOL;
0


but,

php > if((int)1060 == (double)1060) echo 1,PHP_EOL; else echo 0,PHP_EOL;
1



Test script:
---------------
$r = 1060;
$now = 0; $next = 0;
$per = $r/50;

$i=0;
while($i!=$r){
	$now++;
	while($now >= $next){
		$next += $per;
	}
	$i++;
}

var_dump($now,$next);
if($now == $next) echo 1,PHP_EOL; else echo 0,PHP_EOL;


Expected result:
----------------
$now is 1060.
$next is 1060.
So, "if($now==$next)" will return true.

Actual result:
--------------
"if($now==$next)" returns false.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-23 17:26 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-06-23 17:26 UTC] requinix@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.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.


 [2014-06-24 04:29 UTC] blazexerit at yahoo dot co dot jp
Thank you for answer.
I understand cause of this issue.

But, in JavaScript(V8), when I dump `next', 1060.000000000001 is printed.
So, I request that var_dump of php prints 1060.000000000001 instead of 1060, as well.
 [2014-06-24 04:54 UTC] requinix@php.net
That's controlled by your "precision" php.ini setting.

php > var_dump(1060 + 1/50);
float(1060.02)
php > ini_set("precision", 20);
php > var_dump(1060 + 1/50);
float(1060.0199999999999818)
 [2014-06-24 06:51 UTC] blazexerit at yahoo dot co dot jp
Thank you for answer.
The problem has been solved.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 04:00:01 2026 UTC