php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79370 Comparison problem
Submitted: 2020-03-12 09:13 UTC Modified: 2020-03-12 09:59 UTC
From: danil at pyatnitsev dot ru Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.4.3 OS: Debian
Private report: No CVE-ID: None
 [2020-03-12 09:13 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-12 09:16 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-03-12 09:16 UTC] 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:44 UTC] 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:57 UTC] danil at pyatnitsev dot ru
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?
 [2020-03-12 09:59 UTC] nikic@php.net
It's possible, by setting precision=-1. Note however that this will affect all float printing, not just inside var_dump().

PHP 8 uses serialize_precision for var_dump(), which is -1 by default.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC