php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76220 Float variables comparison error
Submitted: 2018-04-13 18:26 UTC Modified: 2018-04-13 18:29 UTC
From: zyrlex at yandex dot ru Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.0.29 OS: Ubuntu 16.04
Private report: No CVE-ID: None
 [2018-04-13 18:26 UTC] zyrlex at yandex dot ru
Description:
------------
Comparison of variables does not give a positive result
2.7 !== 2.7

Test script:
---------------
$q1 = floatval(2.7);
$q2 = floatval(18*0.15); //18*0.15 = 2.7

var_dump($q1);
var_dump($q2);

if($q1 === $q2) {
	echo '2.7 === 2.7 all ok';	
} else {
	echo '2.7 !== 2.7 BUG'; //Why?
}

if($q1 == $q2) {
	echo '2.7 == 2.7 all ok';
}

Expected result:
----------------
float(2.7)
float(2.7)
2.7 === 2.7 all ok

Actual result:
--------------
float(2.7)
float(2.7)
2.7 !== 2.7 BUG

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-13 18:29 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2018-04-13 18:29 UTC] aharvey@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.


 [2018-04-14 05:29 UTC] zyrlex at yandex dot ru
I suspect that is the case, thanks for the reply.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC