2

I'm reading about NaN here and it says that:

A comparison with a NaN always returns an unordered result even when comparing with itself.

I'm confused about the word unordered. For practical reasons, does it mean that comparison will always evaluate to false? It seems to be the case:

// all statemens below evaluate to false

NaN === NaN
NaN > NaN
NaN < NaN

NaN > 3
NaN < 3
NaN === 3
Cœur
  • 37,241
  • 25
  • 195
  • 267
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

3

Yes. When comparing two floating point numbers a and b, there will be exactly 1 of 4 outcomes:

  • a is less than b
  • a is equal to b
  • a is greater than b
  • a and b are unordered.

The IEEE754 spec states that a and b are unordered when either a or b is a NaN (which includes the case when both a and b are NaN).

In most languages the first 3 have their own predicates (typically <, ==, >). The unordered case does not, but can be tested by checking that all the others are false.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50
  • thanks for you help. Just wondering whether you've read the spec? It's not free as I understand – Max Koretskyi Oct 12 '16 at 09:14
  • I managed to download a copy through my old university library, which had an IEEE subscription. It's quite easy to find draft versions online, which are fairly similar to the final spec. – Simon Byrne Oct 12 '16 at 10:26