Bug #79370 [Nab]: Comparison problem
| From: | nikic@php.net | Date: | Thu, 12 Mar 2020 09:44:59 +0000 |
| Subject: | Bug #79370 [Nab]: Comparison problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226050@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79370&edit=1
ID: 79370
Updated by: nikic@php.net
Reported by: danil at pyatnitsev dot ru
Summary: Comparison problem
Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Debian
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
If it's any consolation, on PHP 8 this is going to print:
bool(false)
float(32.77)
float(32.769999999999996)
Which should make it more obvious what is going on here :)
Previous Comments:
------------------------------------------------------------------------
[2020-03-12 09:16:28] nikic@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.
------------------------------------------------------------------------
[2020-03-12 09:13:50] danil at pyatnitsev dot ru
Description:
------------
Incorrect comparison after addition float with integer
Test script:
---------------
<?php
$foo = (float) 32.77;
$bar = (float) 31.77;
$bar += 1;
var_dump($foo == $bar); // false
var_dump($foo, $bar); // float(32.77) float(32.77)
Expected result:
----------------
var_dump($foo == $bar); should be true in this case
Actual result:
--------------
It prints false
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79370&edit=1