Skip to content

NAN in array compare #18563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mvorisek opened this issue May 15, 2025 · 4 comments
Closed

NAN in array compare #18563

mvorisek opened this issue May 15, 2025 · 4 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented May 15, 2025

Description

The following code:

<?php

$a = [NAN];
$b = $a;
var_dump($a === $b);

$a = [NAN];
$b = [NAN];
var_dump($a === $b);

Resulted in this output:

bool(true)
bool(false)

But I expected this output instead:

bool(false)
bool(false)

online repro: https://3v4l.org/CVGBt

I understand the reason - in the 1st case, the zval reference is the same, thus used for the comparison, which is valid for all values excl. NAN. In the 2nd case, zval references are not the same, thus all array items are compared and because currently NAN === NAN returns false, the result is false.

Proposed fix: NANs should be counted in zval and the 1st case can then return the right result correctly and fast as well.

Alternatively, NAN, at least in arrays, can be considered to be true for weak and strict equality. This is my favorite but it might violate IEEE-754, but we already compare null === null...

PHP Version

any

@iluuu1994
Copy link
Member

I propose to fix this by changing the result of NAN === NAN

This is IEEE-754 behavior, so it's not something we can or should change. Hence, I would classify this as a wontfix. But I'll give some time for others to chime in.

@mvorisek
Copy link
Contributor Author

I have updated the description to stress the current issue and discuss the fix better.

@TimWolla
Copy link
Member

would classify this as a wontfix

I would agree.

@mvorisek
Copy link
Contributor Author

I probably agree the cost of fixing this is probably too high if [NAN] should not equals to [NAN].

@TimWolla TimWolla closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants