<div dir="ltr"><div>Hi, Devin,</div><div><br></div><div>You may misunderstand NaN. NaN is a special floating number. It doesn't equal to anything, even itself. If x is NaN, x == x will return false. Actually this is a trick to detect whether it is NaN or not.</div><div><br></div><div>However, there is a standard way to handle it, using isnan [1]. Try the code below:</div><div><br></div><div><div>// [[Rcpp::depends(RcppGSL)]]</div><div>#include <RcppGSL.h></div><div>#include <gsl/gsl_sf_hyperg.h></div><div> </div><div>#include <Rcpp.h></div><div>#include <math.h></div><div>using namespace Rcpp;</div><div> </div><div>// [[Rcpp::export]]</div><div>SEXP test()</div><div>{</div><div>    gsl_set_error_handler_off();</div><div>    double res = gsl_sf_hyperg_2F1(1,1,1.1467003,1);</div><div>    if (res != res){</div><div><span class="" style="white-space:pre">             </span>Rcout << "NaN found!" << std::endl;</div><div><span class="" style="white-space:pre">  </span>}</div><div><span class="" style="white-space:pre">  </span></div><div><span class="" style="white-space:pre">   </span>if (isnan(res)){</div><div><span class="" style="white-space:pre">           </span>Rcout << "NaN found using isnan!" << std::endl;</div><div><span class="" style="white-space:pre">      </span>}</div><div><span class="" style="white-space:pre">  </span></div><div>    return R_NilValue;</div><div>}</div></div><br><div class="gmail_extra">R result:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">> sourceCpp("testgsl.cpp")</div><div class="gmail_extra">> test()</div><div class="gmail_extra">NaN found!</div><div class="gmail_extra">NaN found using isnan!</div><div class="gmail_extra">NULL</div><div class="gmail_extra">> </div><div><br></div></div><div class="gmail_extra">If you are dealing with something more than a NaN floating number, Dirk's solution is your best choice.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Best,</div><div class="gmail_extra"><br></div><div class="gmail_extra">KK</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">[1] <a href="http://www.gnu.org/software/libc/manual/html_node/Floating-Point-Classes.html#Floating-Point-Classes">http://www.gnu.org/software/libc/manual/html_node/Floating-Point-Classes.html#Floating-Point-Classes</a></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 12, 2014 at 8:05 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br>
On 12 December 2014 at 10:09, Devin wrote:<br>
| ... but you can't set a mathematical operation equal to a string. I also tried:<br>
|<br>
| int test()<br>
| {<br>
|     gsl_set_error_handler_off();<br>
|     if(gsl_sf_hyperg_2F1(1,1,1.1467003,1) == NAN){<br>
|         std::cout << "Error" << std::endl;<br>
|     }<br>
|     return 0;<br>
| }<br>
|<br>
| ... but the if-branch gets ignored. How can I achieve that the program runs through the if-branch?<br>
<br>
</span>Use a proper test for NaN.<br>
<br>
See eg <a href="http://gallery.rcpp.org/articles/working-with-missing-values/" target="_blank">http://gallery.rcpp.org/articles/working-with-missing-values/</a> or other<br>
documentation as eg the Writing R Extensions manual, my Rcpp book -- or the<br>
content of the Rcpp package.<br>
<br>
A simple way, using a macro from the R headers, is<br>
<br>
  R> cppFunction('bool isItNan(double foo) { return ISNAN(foo); }')<br>
  R> isItNan(NaN)<br>
  [1] TRUE<br>
  R> isItNan(3.14)<br>
  [1] FALSE<br>
  R> isItNan(3L)<br>
  [1] FALSE<br>
  R><br>
<br>
There are (better) alternatives in the Rcpp sources, but some of the<br>
behaviour is subtle. Kevin once wrote a great answer at StackOverflow:<br>
<br>
  <a href="http://stackoverflow.com/questions/26241085/rcpp-function-check-if-missing-value/26262984#26262984" target="_blank">http://stackoverflow.com/questions/26241085/rcpp-function-check-if-missing-value/26262984#26262984</a><br>
<span class=""><font color="#888888"><br>
<br>
Dirk<br>
</font></span><div class=""><div class="h5"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</div></div></blockquote></div><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Qiang Kou<div><a href="mailto:qkou@umail.iu.edu" target="_blank">qkou@umail.iu.edu</a><br><div>School of Informatics and Computing, Indiana University</div><div><br></div></div></div></div>
</div></div>