[Rcpp-commits] r4157 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/unitTests inst/unitTests/cpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 14 00:45:45 CET 2012


Author: romain
Date: 2012-12-14 00:45:44 +0100 (Fri, 14 Dec 2012)
New Revision: 4157

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/clamp.h
   pkg/Rcpp/inst/unitTests/cpp/sugar.cpp
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
clamp was wrong

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-12-13 23:34:22 UTC (rev 4156)
+++ pkg/Rcpp/ChangeLog	2012-12-13 23:45:44 UTC (rev 4157)
@@ -1,3 +1,9 @@
+2012-12-14 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/sugar/functions/clamp.h: clamp was pretty wrong
+        * unitTests/runit.sugar.R: new unit test for clamp
+        * unitTests/cpp/sugar.cpp: new unit test for clamp
+
 2012-12-12  JJ Allaire <jj at rstudio.org>
 
         * src/Attributes.R: always generate new dynlib file for rebuild=TRUE

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/clamp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/clamp.h	2012-12-13 23:34:22 UTC (rev 4156)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/clamp.h	2012-12-13 23:45:44 UTC (rev 4157)
@@ -32,7 +32,7 @@
     clamp_operator(STORAGE lhs_, STORAGE rhs_ ) : lhs(lhs_), rhs(rhs_){}
     
     inline STORAGE operator()(STORAGE x) const {
-        return lhs < x ? lhs : (x < rhs ? x : rhs ) ;
+        return x < lhs ? lhs : (x > rhs ? rhs : x ) ;
     }
     STORAGE lhs, rhs ;    
 } ;
@@ -43,7 +43,7 @@
     
     inline double operator()(double x) const {
         if( Rcpp::traits::is_na<REALSXP>(x) )  return x ;
-        return lhs < x ? lhs : (x < rhs ? x : rhs ) ;
+        return x < lhs ? lhs : (x > rhs ? rhs : x ) ;
     }
     double lhs, rhs ;    
 } ;

Modified: pkg/Rcpp/inst/unitTests/cpp/sugar.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2012-12-13 23:34:22 UTC (rev 4156)
+++ pkg/Rcpp/inst/unitTests/cpp/sugar.cpp	2012-12-13 23:45:44 UTC (rev 4157)
@@ -574,4 +574,8 @@
     return intersect( x, y ) ;
 }
 
+// [[Rcpp::export]]
+NumericVector runit_clamp( double a, NumericVector x, double b){
+    return clamp( a, x, b ) ;
+}
 

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2012-12-13 23:34:22 UTC (rev 4156)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2012-12-13 23:45:44 UTC (rev 4157)
@@ -696,4 +696,12 @@
     checkEquals( runit_intersect( 1:10, 1:5 ), intersect( 1:10, 1:5 ) )
 }
 
+test.clamp <- function(){
+    r_clamp <- function(a, x, b) pmax(a, pmin(x, b) )
+    checkEquals( 
+        runit_clamp( -1, seq(-3,3, length=100), 1 ), 
+        r_clamp( -1, seq(-3,3, length=100), 1 )
+    )
 }
+
+}



More information about the Rcpp-commits mailing list