Bug #76220 [Com]: Float variables comparison error
| From: | zyrlex at yandex dot ru | Date: | Sat, 14 Apr 2018 05:29:48 +0000 |
| Subject: | Bug #76220 [Com]: Float variables comparison error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214740@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76220&edit=1
ID: 76220
Comment by: zyrlex at yandex dot ru
Reported by: zyrlex at yandex dot ru
Summary: Float variables comparison error
Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Ubuntu 16.04
PHP Version: 7.0.29
Block user comment: N
Private report: N
New Comment:
I suspect that is the case, thanks for the reply.
Previous Comments:
------------------------------------------------------------------------
[2018-04-13 18:29:26] aharvey@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.
------------------------------------------------------------------------
[2018-04-13 18:26:41] zyrlex at yandex dot ru
Description:
------------
Comparison of variables does not give a positive result
2.7 !== 2.7
Test script:
---------------
$q1 = floatval(2.7);
$q2 = floatval(18*0.15); //18*0.15 = 2.7
var_dump($q1);
var_dump($q2);
if($q1 === $q2) {
echo '2.7 === 2.7 all ok';
} else {
echo '2.7 !== 2.7 BUG'; //Why?
}
if($q1 == $q2) {
echo '2.7 == 2.7 all ok';
}
Expected result:
----------------
float(2.7)
float(2.7)
2.7 === 2.7 all ok
Actual result:
--------------
float(2.7)
float(2.7)
2.7 !== 2.7 BUG
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76220&edit=1