[Rcpp-devel] Rcpp catching a domain error
Devin
devin.heer at gmail.com
Fri Dec 12 10:09:58 CET 2014
Thanks Qiang for your help
When I try your code, this is the output I get in Rstudio:
status = 1.#QNAN0
status = 0.310474
[1] 0
What I try to achieve is to feed this function with different input parameters and everytime the function results in "1.#QNAN0", it should return "NAN".
I tried something like:
int test()
{
gsl_set_error_handler_off();
if(gsl_sf_hyperg_2F1(1,1,1.1467003,1) == "1.#QNAN0"){
std::cout << "Error" << std::endl;
}
return 0;
}
... 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?
I appreciate your input!
Devin
-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org]
Sent: Thursday, December 11, 2014 4:36 PM
To: Qiang Kou
Cc: Devin; rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Rcpp catching a domain error
Hi there,
On 11 December 2014 at 10:27, Qiang Kou wrote:
| Hi, Devin,
|
| I don't whether this is the standard solution.
|
| As I understand, the default error handler in GSL will abort the
| program after printing error message. So you need to close it first.
Qiang beat me to it, but when I ran your example (in a standard R session, in my case inside Emacs) I get
R> sourceCpp("/tmp/rcppgslabort.cpp")
R> simpletest()
gsl: hyperg_2F1.c:667: ERROR: domain error
Default GSL error handler invoked.
Process R aborted (core dumped) at Thu Dec 11 09:30:40 2014
which unmistakenly shows __GSL__ aborting here so our try/catch can't do anything for us.
Luckily Qiang is much on the case than I was:
| In most time I don't use try-catch with GSL, I detect whether the
| result is 'nan' or not. Try the code below:
|
| Best,
|
| KK
|
| // [[Rcpp::depends(RcppGSL)]]
| #include <RcppGSL.h>
| #include <gsl/gsl_sf_hyperg.h>
|
| #include <Rcpp.h>
| #include <math.h>
| using namespace Rcpp;
|
| // [[Rcpp::export]]
| int test()
| {
| gsl_set_error_handler_off();
| printf ("status = %f\n", gsl_sf_hyperg_2F1(1,1,1.1467003,1));
| printf ("status = %f\n",
| gsl_sf_hyperg_2F1(-10.34,2.05,3.05,0.1725));
| return 0;
| }
That's a nice trick:
R> sourceCpp("/tmp/rcppgslabort.cpp")
R> fixedtest()
status = nan
status = 0.310474
[1] 0
R>
Qiang, how about wrapping ten lines of context around it and making it an Rcpp Gallery post? This ought to be documented....
Dirk
|
|
| On Thu, Dec 11, 2014 at 10:03 AM, Devin <devin.heer at gmail.com> wrote:
|
|
| Dear list
|
|
|
| My Rcpp source file contains a gsl_sf_hyperg_2F1() function, and for some
| input values to this function the return value is “NaN”, which leads in
| RStudio to an abortion error.
|
|
|
| In order to avoid abortion errors, I try to implement a try/catch exception
| handler:
|
|
| // [[Rcpp::depends(RcppGSL)]]
|
| #include <RcppGSL.h>
|
| #include <gsl/gsl_sf_hyperg.h>
|
|
|
| #include <Rcpp.h>
|
| #include <math.h>
|
| using namespace Rcpp;
|
|
|
| // [[Rcpp::export]]
|
| int test()
|
| {
|
| try{
|
| gsl_sf_hyperg_2F1(1,1,1.1467003,1); // abortion error since not
| defined
|
| }
|
| catch(std::domain_error& e){
|
| std::cout << "Domain Error occurred!" << std::endl;
|
| }
|
| return 0;
|
| }
|
|
|
| This seems to be wrong, since it still causes an abortion error in RStudio.
|
|
|
| I appreciate any input
|
|
|
| Thanks
|
| Devin
|
|
|
|
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
|
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
| l
|
|
|
|
| --
| Qiang Kou
| qkou at umail.iu.edu
| School of Informatics and Computing, Indiana University
|
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
| l
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list