Bug #79370 [Nab]: Comparison problem
Edit report at https://bugs.php.net/bug.php?id=79370&edit=1
ID: 79370
User updated by: danil at pyatnitsev dot ru
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:
Thanks!
On PHP 8 it seems correct. Great!
Is there any way to make var_dump shows more digits in this example on 7.4?
Previous Comments:
------------------------------------------------------------------------
[2020-03-12 09:44:59] nikic@php.net
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 :)
------------------------------------------------------------------------
[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
Thread (5 messages)