1

As far as i know, in the IEEE 754 standard x==NaN should be false for all values of x, including Nan, but when i test NaN==NaN in https://rextester.com/l/r_online_compiler it returns NA instead.

Does this violate the standard? Is there a reasoning behind this design choice? I ask mostly out of curiosity, since this situation doesn't come up a lot.

Juan Carlos Ramirez
  • 2,054
  • 1
  • 7
  • 22
  • 3
    From ``?`==` ``: "Missing values (NA) and NaN values are regarded as non-comparable even to themselves, so comparisons involving them will always result in NA". So, whatever the reasoning, it seems to be deliberate – divibisan Feb 15 '19 at 18:45
  • @divibisan Do you happen to know why NA is the return instead of NaN? – Juan Carlos Ramirez Feb 15 '19 at 18:58
  • This is just guessing, but `NA` is more normal failure condition. Since they've decided that `NaN` can't be compared to anything, it makes sense that they'd return `NA` since the answer is Not Available, rather than `NaN` which suggests that some operation was done. – divibisan Feb 15 '19 at 19:02
  • https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values – Ben Bolker Feb 15 '19 at 19:26
  • I think that `0/0` explains a lot. If you think of what is the root of equation `0 * x = 0`, you realize that it could be any real number, apparently - all of them. That's why division by zero is *undefined*. As far as the `0/0` may be assigned to any chosen number comparison `0/0 == 0/0` doesn't have sense, because you can't say what are you comparing 1 to 3.1415, or 0 to 1e99. And probably the best answer you can get is 'maybe', which is not a number, or `NaN` in R. At least I explain it to myself this way. P.S.: and `class(NaN)` is `numeric` not `logical` as it mentioned below. – utubun Feb 15 '19 at 19:46
  • @utubun I agree that it doesn't make sense from a mathematical perspective. However, it seems that it was historically chosen to evaluate to false so that x!=x could be used as an efficient detection method for nans across different machines, according to https://stackoverflow.com/a/1573715/10548514 . Of course nowadays detection can be done with some isnan routine. – Juan Carlos Ramirez Feb 15 '19 at 20:00

1 Answers1

4

I think this answer can go down two paths. You can talk about IEEE 754 and the various types of NaN (signaling NaN and "quiet" NaN) and if that is where your curiosity is leading towards. It may be worth researching.

However in R, NaN is a "special value" and reserved word. It will be treated as a numerical (float, real, imaginary, complex etc.)

It is a general rule in R that any computations involving NaN will return either NaN or NA. "Which may depend on the R platform". (See link below). Essentially, depending how R chooses to interpret your code, it will normally typically always throw NaN or NA.

The only sure-fire way of testing if an NaN exists is to use the predicate is.nan()

https://stat.ethz.ch/R-manual/R-devel/library/base/html/is.finite.html

  • "It is a logical vector with a length of 1" so it isn't represented in a floating point format, if I understand you correctly. If I assign (0/0) to a variable x then x would store this vector? – Juan Carlos Ramirez Feb 15 '19 at 19:22
  • You're absolutely right, had a brain fart and was thinking about what is.nan returns. I will amend. NaN will be treated as one any of the numeric data types (real, imaginary, complex, etc..) – Robele Baker Feb 15 '19 at 21:17