Juan Carlos Ramirez
Juan Carlos Ramirez

Reputation: 2129

Why does NaN!=NaN return NA instead of TRUE in R

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.

Upvotes: 1

Views: 226

Answers (1)

Robele Baker
Robele Baker

Reputation: 91

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

Upvotes: 4

Related Questions