Bug #75568 [Opn->Nab]: a strange behavior of float and string comparison
| From: | nikic@php.net | Date: | Fri, 24 Nov 2017 16:13:03 +0000 |
| Subject: | Bug #75568 [Opn->Nab]: a strange behavior of float and string comparison | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212714@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75568&edit=1
ID: 75568
Updated by: nikic@php.net
Reported by: 605231181 at qq dot com
Summary: a strange behavior of float and string comparison
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: not related
PHP Version: 7.1.12
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2017-11-24 16:10:10] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75568&edit=1