php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75568 a strange behavior of float and string comparison
Submitted: 2017-11-24 16:10 UTC Modified: 2017-11-24 16:13 UTC
From: 605231181 at qq dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.12 OS: not related
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: 605231181 at qq dot com
New email:
PHP Version: OS:

 

 [2017-11-24 16:10 UTC] 605231181 at qq dot com
Description:
------------
I've recently noticed a strange behavior of php comparison on float/double and string, i searched online but found nothing related, I'm not sure if it's a real bug or not, so I've posted it here. I understand there might be some type conversion problem, but i don't quite understand what's really going on.

I understand the problem is about float number, but I'm not using === to compare, and the output(var_dump) of two value is same, so I expect same results here.
As a note, not every case result wrong, some case is working.

Test script:
---------------
code is running in Psy Shell v0.8.2, but the issue can be reproduced anywhere.

>>> $a=986365.1;$b='986365.1';var_dump($a,$b,$a==$b);
float(986365.1)
string(8) "986365.1"
bool(true)

>>> $a='73.01'*13510;$b='986365.1';var_dump($a,$b,$a==$b);
float(986365.1)
string(8) "986365.1"
bool(false)

>>> $a='5.01'*10.1;$b='50.601';var_dump($a,$b,$a==$b);
float(50.601)
string(6) "50.601"
bool(true)

Expected result:
----------------
i expect same result bool(true) in all test scripts.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-24 16:13 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-11-24 16:13 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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 16:01:29 2024 UTC