In a R package using compiled code via .Call
the macro R_FINITE
can be used to check if a double
takes a special value. R_NegInf
and
R_PosInf
can be used to set it to the negative or the positive
infinite value. But how can we test that a double is equal to the
negative or the positive infinite value?
This is useful for instance when implementing a probability distribution function which should take the values zero and one at the infinite values.
>Solution :
R (and C) have you covered here, you can simply compare for equality:
> Rcpp::cppFunction("bool isPosInf(double x) { return x == R_PosInf; }")
> sapply(c(-Inf, Inf, 0), isPosInf)
[1] FALSE TRUE FALSE
>
> Rcpp::cppFunction("bool isNegInf(double x) { return x == R_NegInf; }")
> sapply(c(-Inf, Inf, 0), isNegInf)
[1] TRUE FALSE FALSE
>
Note that while I use Rcpp
for convenience there is nothing specific to Rcpp
here as the two symbols compared-to are from the R headers.