You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Initialization of missing value objects such as R_NaInt, R_NaReal, R_NaN:
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:
Definitions of the following macros, which just use the above missing value objects.
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 eitherNA_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.
The text was updated successfully, but these errors were encountered:
NA
andNaN
functions can generally be found in two places,src/main/arithmetic.c
andsrc/include/R_ext/Arith.h
.All of the functions / objects / macros below can be accessed by including
R.h
, as the ones inarithmetic.c
are re-exposed inR_ext/Arith.h
, which isinclude
-ed inR.h
.The three most important are
R_IsNA()
,R_IsNaN()
andISNAN()
.src/main/arithmetic.c
contains:Initialization of missing value objects such as
R_NaInt
,R_NaReal
,R_NaN
:https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/main/arithmetic.c#L163-L177
R_IsNA()
to detect if a double value isNA_real_
https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/main/arithmetic.c#L120-L128
R_IsNaN()
to detect if a double value isNaN
https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/main/arithmetic.c#L130-L138
Note: Both
R_IsNA()
andR_IsNaN()
callisnan()
internally, and then do additional processing if it returnsTRUE
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 eitherNA
orNaN
, useisnan()
directly orISNAN()
(see below for more).src/include/R_ext/Arith.h
additionally contains:Definitions of the following macros, which just use the above missing value objects.
#define NA_LOGICAL R_NaInt
#define NA_INTEGER R_NaInt
#define NA_REAL R_NaReal
https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/include/R_ext/Arith.h#L55-L58
The
ISNA()
macro, which is justR_IsNA()
https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/include/R_ext/Arith.h#L64
The
ISNAN()
macro, see below for further explanation.https://github.com/wch/r-source/blob/62a4c351ef455c9233f2565ab48453818258dd51/src/include/R_ext/Arith.h#L66-L77
A developer's decision to use C's
isnan()
vs R'sISNAN()
is dependent on a few things. Both functions can be used to detect if a double is eitherNA_real_
orNaN
, which is very useful as a single check for either value, rather than calls to bothR_IsNA()
andR_IsNaN()
.If using C++, you will not have access to
isnan()
as the C++ math headers undefine it, so you must useISNAN()
.If using C, you can use either, but be aware that
isnan()
is not guaranteed to return1
forTRUE
. On an AWS Windows 2019 Server for example, it returned-1
. You can therefore safely useisnan()
inside of anif (isnan(x)) {}
block, but if you need to return the value ofisnan()
, then you should use R'sISNAN()
which guarantees a result of1
or0
by wrapping it withisnan(x) != 0
.The text was updated successfully, but these errors were encountered: