Skip to content

Instantly share code, notes, and snippets.

@hadley
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save hadley/66feb570d93d5edc54f7 to your computer and use it in GitHub Desktop.

Select an option

Save hadley/66feb570d93d5edc54f7 to your computer and use it in GitHub Desktop.
# http://stackoverflow.com/a/3407254/16632
Rcpp::cppFunction("double ceil_any(double x, double prec) {
if (fabs(prec / x) < DBL_MIN)
return x;
double r = fmod(fabs(x), prec);
if (r == 0)
return x;
return (x > 0) ? x + prec - r : x + r;
}")
ceil_any(9.9, 2)
ceil_any(10, 2)
ceil_any(10.1, 2)
ceil_any(-9.9, 2)
ceil_any(-10, 2)
ceil_any(-10.1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment