php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72239 float zero should be equal int 0 even with ===
Submitted: 2016-05-19 02:45 UTC Modified: 2016-05-19 14:37 UTC
From: dosergio at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
 [2016-05-19 02:45 UTC] dosergio at gmail dot com
Description:
------------
Zero is zero, I think there is no "float flavour" of zero and "int flavour" of zero since zero is nothing.

Why to check data type when both sides are absolute ZERO ?

So for the comparison 0 === 0.0, I think PHP should go beyound the memory/way of storing the number, and consider that we are comparing two absolute zero values, so the result could be TRUE as it is in other languages.

The data type check only makes sense to enter in scene when one of the compared values differ from zero!
 
What makes a factual float? One number like 0.0000000000001 ! I mean, different from zero.

The problem: 
A programmer knows that a value in some condition is zero.
But when comparing  if ($myValue === 0) causes failure, since PHP obliges you to write if ($myValue === 0.0) 


 



Test script:
---------------
$x = floatVal(0);

if( $x === 0){ // why to check data type when both sides are ZERO ?
  echo 'Will not enter here, php expects 0.0';
} else {
  echo '0 and 0.0 is the same "VALUE"';
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-19 02:55 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2016-05-19 02:55 UTC] requinix@php.net
Why stop at 0? How about making 1 === 1.0? And what about strings? 0 should === "0" too. Of course since 0 === 0.0 then that means 0 === "0.0" too.

=== means identical. If you don't want an identical comparison then don't use it.
 [2016-05-19 06:14 UTC] pajoye@php.net
-Status: Wont fix +Status: Not a bug
 [2016-05-19 06:14 UTC] pajoye@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.

Better explanation.
 [2016-05-19 13:54 UTC] dosergio at gmail dot com
You asked why zero?
The answer is: because zero is a special value. Means nothing, in any types of numbers, int, float, double, real, etc.
Envolving strings in the comparison was an exageration. What I proposed would apply only in NUMERIC comparisons with both sides ZERO. because zero is absolute. A number only starts being float when it is different from zero.
But ok
Lets write === 0.0 because 0 is not 0.0 so, I am talking about numbers and the intrinsic value.
 [2016-05-19 14:23 UTC] dosergio at gmail dot com
OP1          OP2                    TYPE CHECK          RESULT
--------------------------------------------------------------
number      "not number"            On                  false
--------------------------------------------------------------
float       int    if(0.0 and 0 )   Off                 true
--------------------------------------------------------------
float       int    if(any != 0.0)   On                  false
 [2016-05-19 14:37 UTC] dosergio at gmail dot com
I know that this idea will not be implemented.
This is more philosophy than computing:
Concepts of numbers, and specially, the definition of zero.
Anyway thanks
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 11:00:02 2026 UTC