Inferrator
Inferrator

Reputation: 519

Error handling in Rcpp

How can I check, if a value is numeric and finite? Let's say I generate random numbers with Rf_rgamma or with my own routine. Depending on the parameters, errors could be generated. How can I check that within C and break a loop and the entire function in that event?

And how can I check if a vector, let's say an arma::vec from RcppArmadillo, contains only numeric and finite values?

I know, these are general questions. However, my specific problem takes minutes to be reproduced and I haven't been able to create a minimal example. Most of the time my function works fine, just 1 in 100.000 times it causes R to crash.

Upvotes: 2

Views: 447

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368409

Here is one way: check each element. A simple demo:

R> cppFunction('int checker(double x) { return ::R_finite(x);} ')
R> checker(2)
[1] 1
R> checker(0)
[1] 1
R> checker(NaN)
[1] 0
R> checker(Inf)
[1] 0
R> 

Upvotes: 3

Related Questions