Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check for `Inf` or `-Inf` in the `.Call` R Interface

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading