3

I want to check if x that is a <double> does not contain missing value. There are several options:

  • Rcpp::NumericVector::is_na(x)
  • R_IsNA(x)
  • std::isnan(x)

and others like

std::memcmp(
    (char*)(&x),
    (char*)(&NA_REAL),
    sizeof(double)
) == 0;

What are the differences between those methods? Which one is recommended assuming that x is not a vector, so we do not need Rcpp's vectorization?

Tim
  • 7,075
  • 6
  • 29
  • 58
  • 1
    I have no idea about `Rcpp`, but `std::isnan` works with C++ floating point-types which may or may not be the same as R types (and may not even be `IEEE-754`-compliant, strictly speaking). – Violet Giraffe Aug 25 '16 at 15:13
  • @Violet: R has both `NA` and `NaN`; and both are defined for several types beyond `double`. It is very much an extension beyond IEEE 754. – Dirk Eddelbuettel Aug 25 '16 at 16:05
  • @Tim: I marked this as duplicate. The other question has two good answers, and the answer by Kevin pretty thorough. – Dirk Eddelbuettel Aug 25 '16 at 16:07
  • @DirkEddelbuettel I know the other thread but I'm interested in differences between those functions. – Tim Aug 25 '16 at 16:13
  • At the top-level: The first is for Rcpp vectors, the second is an R primitive (for `SEXP` types) and the third is from the C++ library. Difference sources, different scopes. – Dirk Eddelbuettel Aug 25 '16 at 16:15
  • If you want an extended discussion, I would suggest the rcpp-devel list rather than these comments... – Dirk Eddelbuettel Aug 25 '16 at 16:16

0 Answers0