[Rcpp-devel] Rcpp catching a domain error

Dirk Eddelbuettel edd at debian.org
Fri Dec 12 14:05:14 CET 2014


On 12 December 2014 at 10:09, Devin wrote:
| ... but you can't set a mathematical operation equal to a string. I also tried:
| 
| int test()
| {
|     gsl_set_error_handler_off();
|     if(gsl_sf_hyperg_2F1(1,1,1.1467003,1) == NAN){
|         std::cout << "Error" << std::endl;
|     }
|     return 0;
| }
| 
| ... but the if-branch gets ignored. How can I achieve that the program runs through the if-branch?

Use a proper test for NaN.  

See eg http://gallery.rcpp.org/articles/working-with-missing-values/ or other
documentation as eg the Writing R Extensions manual, my Rcpp book -- or the
content of the Rcpp package.

A simple way, using a macro from the R headers, is

  R> cppFunction('bool isItNan(double foo) { return ISNAN(foo); }')
  R> isItNan(NaN)
  [1] TRUE
  R> isItNan(3.14)
  [1] FALSE
  R> isItNan(3L)
  [1] FALSE
  R> 

There are (better) alternatives in the Rcpp sources, but some of the
behaviour is subtle. Kevin once wrote a great answer at StackOverflow:

  http://stackoverflow.com/questions/26241085/rcpp-function-check-if-missing-value/26262984#26262984


Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list