php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45712 $something == NaN returns true with 5.3, false with 5.2.*
Submitted: 2008-08-05 02:39 UTC Modified: 2014-02-15 23:12 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: for-bugs at hnw dot jp Assigned: tony2001 (profile)
Status: Closed Package: Variables related
PHP Version: 5.2CVS, 5.3CVS, 6CVS (2008-08-05) OS: *
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: for-bugs at hnw dot jp
New email:
PHP Version: OS:

 

 [2008-08-05 02:39 UTC] for-bugs at hnw dot jp
Description:
------------
There is == operator's problem dealing with NaN(Not a Number) in PHP 5.3.0alpha1. This behavior is different with PHP 5.0.0-5.2.6, so this is compatibility problem.

Reproduce code:
---------------
<?php

$nan = (-1e300*1e300)/(1e300*1e300); // float(NAN)

var_dump($nan=='');
var_dump($nan==0.5);
var_dump($nan==50);
var_dump($nan=='500');
var_dump($nan=='abc');
var_dump($nan==$nan);

Expected result:
----------------
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

Actual result:
--------------
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-05 12:33 UTC] jani@php.net
That $nan == $nan should be true. (like it is with 5.3)
 [2008-08-05 12:52 UTC] for-bugs at hnw dot jp
NaN is not exact number. So, NaN should not equals itself. Additionaly, NaN == NaN is false in C. You can see behavior of C as follows.

$ cat nan.c
int main() {double d;d=(-1e300*1e300)/(1e300*1e300);if (d==d) return 1; else return 2;}
$ gcc nan.c; ./a.out; echo $?
2
$
 [2008-08-07 07:37 UTC] tony2001@php.net
I'm testing a patch for it.
 [2008-08-07 08:36 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-07-28 01:28 UTC] jwvdveer at gmail dot com
Please, reopen this bug. It's not working in PHP 5.3.3 on Windows platform (released at 2010-07-21).
The behaviour of a comparison to NAN is the same as noted here.

It's not just a `me too`, but this thread shouln't have been closed.
 [2011-05-26 07:59 UTC] philip@php.net
-Status: Closed +Status: Re-Opened
 [2011-05-26 07:59 UTC] philip@php.net
Reopening because the associated test tests INF as well. However, this is about INF:

// 5.3.7-dev (and all past PHP versions, afaict)
$inf = pow(0,-2); var_dump($inf==$inf); var_dump($inf===$inf); // false true

// 5.4.0-dev
$inf = pow(0,-2); var_dump($inf==$inf); var_dump($inf===$inf); // true true

Please explain.

And @jwvdveer, it was indeed fixed (in 5.3) and made to return false.
 [2013-09-16 16:48 UTC] cmbecker69 at gmx dot de
> Please explain.

It seems there was a bug *before* PHP 5.4, because if $a === $b,
then $a == $b by definition.
 [2014-02-15 23:12 UTC] nikic@php.net
-Status: Re-Opened +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 09:01:28 2024 UTC