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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 21:01:32 2025 UTC