Bug #67501 [Nab]: strange operation of comparison of same value
| From: | requinix@php.net | Date: | Tue, 24 Jun 2014 04:54:22 +0000 |
| Subject: | Bug #67501 [Nab]: strange operation of comparison of same value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186316@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67501&edit=1
ID: 67501
Updated by: requinix@php.net
Reported by: blazexerit at yahoo dot co dot jp
Summary: strange operation of comparison of same value
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 5.5.13
Block user comment: N
Private report: N
New Comment:
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)
Previous Comments:
------------------------------------------------------------------------
[2014-06-24 04:29:24] 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-23 17:26:35] 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-23 11:15:02] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67501&edit=1