[Rcpp-devel] Incorrect result of NA_REAL and NA_INTEGER

Dirk Eddelbuettel edd at debian.org
Sat Sep 5 00:15:23 CEST 2020


On 4 September 2020 at 11:24, Akhila Chowdary Kolla wrote:
| Hello Everyone,
| 
| I am trying to use NA_REAL, NA_INTEGER, and R_NaN in my CPP code(doesn't
| use R main). When I compile and run my code,* NA_REAL* it gives the value
| as *nan* and *NA_INTEGER* gives the value as *-2147483648, and R_NaN as 0*.
| 
| I used the following code(from Rcpp FAQ):
| Rcpp::IntegerVector Missing_I() {
|   Rcpp::IntegerVector v(1);
|   v[0] = NA_INTEGER; // NA
|   return v;
| }
| Rcpp::NumericVector Missing_N() {
|   Rcpp::NumericVector v(4);
|   v[0] = R_NegInf; // -Inf
|   v[1] = NA_REAL; // NA
|   v[2] = R_PosInf; // Inf
|   v[3] = R_NaN; // nan
|   return v;
| }
| 
| When I compile the functions using sourceCpp() I get the output as expected:
| > sourceCpp("~/R/RcppDeepState/inst/extdata/filesave.cpp")
| > Missing_I()
| [1] NA
| > Missing_N()
| [1] -Inf   NA  Inf  NaN
| 
| But when I compile the code using the TestHarness it gives me the following
| output:
| missing_n values: -inf nan inf 0
| missing_i values: -2147483648
| 
| I saved the above functions(Missing_I, Missing_N) in a header file and made
| a call to those functions from the testharness:
| 
| TEST(deepstate_test,datatype){
| RInside();
| Rcpp::IntegerVector missing_i = Missing_I();
| std::cout <<"missing_i values: "<< missing_i << std::endl;
| Rcpp::NumericVector missing_n = Missing_N();
| std::cout <<"missing_n values: "<< missing_n << std::endl;
| }
| 
| How can I get the results as expected? Any help is appreciated.

Am I understanding you correctly that you would like _the R behaviour_ but in
a non-R context (such as a catch or Google gtest harness) ?  You can't as
easily as R actively adds some extensions. I.e. IEEE 754 defines this for
doubles, but "nobody" besides R does it for Int. So you would have to add
your print function.

Dirk

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


More information about the Rcpp-devel mailing list