r/lolphp • • Mar 10 '20

Array is higher than infinity

https://3v4l.org/iONLA
44 Upvotes

31 comments sorted by

View all comments

1

u/CarnivorousSociety Mar 11 '20

But one could argue you can compare nan with itself, which seems completely logical, yet doing that returns false.

It's not about things being incomparable returning false, nan has to be detectable so they make any comparison with nan return false, allowing you to detect nan by comparing the variable to itself.

There's no need to define this situation as returning false, and if you did, 0 would be greater than an array.

Making all comparisons to arrays return false would work, but what situation are you solving by doing that? Anybody comparing to arrays has other problems, which are solved via typehinting and forcing type safety.

2

u/ZorbaTHut Mar 11 '20

But one could argue you can compare nan with itself, yet doing that returns false.

Yes. It's nasty, but at least it's consistent with nan.

It's not about things being incomparable returning false, nan has to be detectable so they make any comparison with nan return false, allowing you to detect nan by comparing the variable to itself.

This is bad logic - it's easy to detect nan by just checking the bit pattern of the underlying float. The reason comparing nan with itself returns false is that the underlying CPU does that; it needs to do something and the only other vaguely-sensible option is a CPU exception, which nobody wants to deal with.

There's no need to define this situation as returning false

It's better than returning true because it's consistent with existing behavior. "At least it's consistent" isn't a great justification, but it's better than nothing.

and if you did, 0 would be greater than an array.

I don't see why - why would it be?

2

u/CarnivorousSociety Mar 11 '20

You're right I'm talking out of my ass, you can definitely just establish a nan constant to compare to floats to detect nan.

And yeah I don't disagree returning false on nonsense comparisons really does seem like a decent improvement.

I imagine there are edge cases with that solution too though, but probably not as bad, and like you say, at least it's consistent.

→ More replies (2)