Skip to content

Commit

Permalink
handle regular number, NA and NaN in comparison<REALSXP>. closes tidy…
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Mar 24, 2014
1 parent 7660827 commit 609cdd3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ summarise_impl <- function(df, args, env) {
.Call('dplyr_summarise_impl', PACKAGE = 'dplyr', df, args, env)
}

test_comparisons <- function() {
.Call('dplyr_test_comparisons', PACKAGE = 'dplyr')
}

#' Cumulativate versions of any, all, and mean
#'
#' dplyr adds \code{cumall}, \code{cumany}, and \code{cummean} to complete
Expand Down
45 changes: 38 additions & 7 deletions inst/include/dplyr/comparisons.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,59 @@ struct comparisons<STRSXP> {
// taking advantage of the particularity of NA_REAL
template <>
struct comparisons<REALSXP> {

inline bool is_less(double lhs, double rhs) const {
bool res = is_na(rhs) || lhs < rhs ;
return res ;
if( is_nan(lhs) ) {
return false ;
} else if( is_na(lhs) ){
return is_nan(rhs) ;
} else {
// lhs >= rhs is false if rhs is NA or NaN
return !( lhs >= rhs) ;
}

}

inline bool is_greater(double lhs, double rhs) const {
bool res = is_na(rhs) || lhs > rhs ;
return res ;
if( is_nan(lhs) ) {
return false ;
} else if( is_na(lhs) ){
return is_nan(rhs) ;
} else {
// lhs <= rhs is false if rhs is NA or NaN
return !( lhs <= rhs) ;
}

}

inline bool is_equal(double lhs, double rhs ) const {
return lhs == rhs ;
}

inline bool equal_or_both_na( double lhs, double rhs ) const {
return lhs == rhs || ( is_na(lhs) && is_na(rhs) );
return lhs == rhs ||
( is_nan(lhs) && is_nan(rhs) ) ||
( is_na(lhs) && is_na(rhs) );
}

inline bool is_na(double x) const {
return Rcpp::traits::is_na<REALSXP>(x);
return ISNA(x);
}

inline bool is_nan(double x) const {
return Rcpp::traits::is_nan<REALSXP>(x) ;
}

inline void print(double x) const {
if( is_na(x) ) {
std::cout << "NA" ;
} else if( is_nan(x) ) {
std::cout << "NaN" ;
} else {
std::cout << x ;
}
}

} ;


Expand Down
7 changes: 7 additions & 0 deletions inst/tests/test-internals.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context("internals")

test_that("comparisons<REALSXP> works as expected (#275)", {
res <- test_comparisons()
expect_true( all(res) )
})

14 changes: 14 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,20 @@ BEGIN_RCPP
return __sexp_result;
END_RCPP
}
// test_comparisons
LogicalVector test_comparisons();
RcppExport SEXP dplyr_test_comparisons() {
BEGIN_RCPP
SEXP __sexp_result;
{
Rcpp::RNGScope __rngScope;
LogicalVector __result = test_comparisons();
PROTECT(__sexp_result = Rcpp::wrap(__result));
}
UNPROTECT(1);
return __sexp_result;
END_RCPP
}
// cumall
LogicalVector cumall(LogicalVector x);
RcppExport SEXP dplyr_cumall(SEXP xSEXP) {
Expand Down
20 changes: 20 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <dplyr.h>

using namespace Rcpp ;
using namespace dplyr ;

// [[Rcpp::export]]
LogicalVector test_comparisons(){
dplyr::comparisons<REALSXP> comp ;
return LogicalVector::create(
comp.is_less( 1.0, 2.0 ),
!comp.is_less( 2.0, 1.0 ),
comp.is_less( NA_REAL, R_NaN ),
! comp.is_less( R_NaN, NA_REAL),
! comp.is_less( NA_REAL, 1.0 ),
! comp.is_less( R_NaN, 1.0 ),
comp.is_less( 1.0, NA_REAL ),
comp.is_less( 1.0, R_NaN )
) ;
}

0 comments on commit 609cdd3

Please sign in to comment.