[Rcpp-devel] Segfaults

Hadley Wickham hadley at rice.edu
Wed Jan 4 16:07:08 CET 2012


>> What are the advantages/disadvantages of doing that over (say) x_[i]
>> == NA_REAL ?
>>
> it works ;-)
>
> x == NA_REAL does not work.

Ok, that makes sense.  I thought it worked because the function below
did what I expected.  But now I guess that must be because < and >
must return false when comparing a double to a missing value.

fast_range <- cxxfunction(signature(x = "numeric"), plugin = "Rcpp", '
  double min = INFINITY;
  double max = -INFINITY;

  Rcpp::NumericVector x_(x);
  int n = x_.size();
  for(int i = 0; i < n; i++) {
    if (x_[i] == NA_REAL) continue;
    if (x_[i] < min) min = x_[i];
    if (x_[i] > max) max = x_[i];
  }

  return(NumericVector::create(min, max));
')

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/


More information about the Rcpp-devel mailing list