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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: danil at pyatnitsev dot ru
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Apr 23 20:01:29 2024 UTC