Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NA / NaN detection #15

Open
DavisVaughan opened this issue Oct 16, 2019 · 0 comments
Open

NA / NaN detection #15

DavisVaughan opened this issue Oct 16, 2019 · 0 comments

Comments

@DavisVaughan
Copy link
Contributor

DavisVaughan commented Oct 16, 2019

NA and NaN functions can generally be found in two places, src/main/arithmetic.c and src/include/R_ext/Arith.h.

All of the functions / objects / macros below can be accessed by including R.h, as the ones in arithmetic.c are re-exposed in R_ext/Arith.h, which is include-ed in R.h.

The three most important are R_IsNA(), R_IsNaN() and ISNAN().


src/main/arithmetic.c contains:

Note: Both R_IsNA() and R_IsNaN() call isnan() internally, and then do additional processing if it returns TRUE to determine which type of nan value it is. You should only use these if you need to be able to tell the difference between the missing value types. If you just want to know if a value is either NA or NaN, use isnan() directly or ISNAN() (see below for more).


src/include/R_ext/Arith.h additionally contains:


A developer's decision to use C's isnan() vs R's ISNAN() is dependent on a few things. Both functions can be used to detect if a double is either NA_real_ or NaN, which is very useful as a single check for either value, rather than calls to both R_IsNA() and R_IsNaN().

  • If using C++, you will not have access to isnan() as the C++ math headers undefine it, so you must use ISNAN().

  • If using C, you can use either, but be aware that isnan() is not guaranteed to return 1 for TRUE. On an AWS Windows 2019 Server for example, it returned -1. You can therefore safely use isnan() inside of an if (isnan(x)) {} block, but if you need to return the value of isnan(), then you should use R's ISNAN() which guarantees a result of 1 or 0 by wrapping it with isnan(x) != 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant