[Rcpp-commits] r3327 - pkg/int64/inst/include/int64

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 12 11:23:40 CET 2011


Author: romain
Date: 2011-11-12 11:23:39 +0100 (Sat, 12 Nov 2011)
New Revision: 3327

Modified:
   pkg/int64/inst/include/int64/math.h
Log:
abs and sign with NA handling

Modified: pkg/int64/inst/include/int64/math.h
===================================================================
--- pkg/int64/inst/include/int64/math.h	2011-11-12 10:20:52 UTC (rev 3326)
+++ pkg/int64/inst/include/int64/math.h	2011-11-12 10:23:39 UTC (rev 3327)
@@ -26,12 +26,14 @@
       
 template <typename LONG>
 SEXP abs( SEXP x ){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
     LONG tmp ;
     int64::LongVector<LONG> res(n) ;
     for( int i=0; i<n; i++){
         tmp = data.get(i) ;
+        if( tmp == na ) res.set(i, na ) ;
         res.set( i, tmp > 0 ? tmp : -tmp ) ;            
     }
     return res ;    
@@ -41,12 +43,19 @@
      
 template <typename LONG>
 SEXP sign( SEXP x){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
+    LONG tmp ;
     SEXP res = PROTECT(Rf_allocVector(REALSXP,n)) ;
     double* p_res = REAL(res) ;
     for( int i=0; i<n; i++){
-        p_res[i] = ( data.get(i) > 0 ) ? 0.0 : 1.0 ;
+        tmp = data.get(i) ;
+        if( tmp == na ) {
+            p_res[i] = NA_REAL ;
+        } else {
+            p_res[i] = ( data.get(i) > 0 ) ? 0.0 : 1.0 ;
+        }
     }
     UNPROTECT(1) ;
     return res ;    



More information about the Rcpp-commits mailing list