[Rcpp-devel] Segfaults

Romain François romain at r-enthusiasts.com
Wed Jan 4 16:00:47 CET 2012


Le 04/01/12 15:55, Hadley Wickham a écrit :
>> So your i_[0] - 1 is also nan. You are responsible for dealing with na.
>>
>> You can use traits::is_na:
>>
>> fx<- cxxfunction( signature( x_ = "numeric"), '
>>
>>     double x = as<double>(x_) ;
>>
>>     if( traits::is_na<REALSXP>(x) ){
>>         // do something
>>     } else {
>>         // do something else
>>     }
>>
>> ', plugin = "Rcpp" )
>>
>> or perhaps if you don't like the whole traits::is_na<REALSXP>  thing, you
>> could use e.g. somethin like this:
>>
>> template<typename T>
>> bool isna( T x ){
>>     return traits::is_na<traits::r_sexptype_traits<T>::rtype>  (x) ;
>> }
>>
>> so that you coud just call
>>
>> if( isna(x) ){ ... }
> What are the advantages/disadvantages of doing that over (say) x_[i]
> == NA_REAL ?
>
> Hadley
>
it works ;-)

x == NA_REAL does not work.

Try this:

fx <- cxxfunction( signature( x_ = "numeric"), '

     double x = as<double>(x_) ;

     if( x == NA_REAL) {
         return wrap( true ) ;
     } else {
         return wrap(false) ;
     }

', plugin = "Rcpp" )
fx( NA )

Romain

-- 
Romain Francois
Professional R Enthusiast
http://romainfrancois.blog.free.fr



More information about the Rcpp-devel mailing list