-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from eddelbuettel/feature/asdouble2
New function (as.double2), plus documentation and tests.
- Loading branch information
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
library(RcppFastFloat) | ||
|
||
set.seed(8675309) | ||
input <- sample(c( | ||
paste0(" \r\n\t\f\v", c(0.0, sqrt(seq(1, 10))), " \r\n\t\f\v"), | ||
c("NaN", "-NaN", "nan", "-nan", | ||
"Inf", "-Inf", "inf", "-inf", "infinity", "-infinity", | ||
NA_character_, | ||
" 1970-01-01", "1970-01-02 ") | ||
)) | ||
|
||
expect_equal(suppressWarnings(as.double(input)), | ||
suppressWarnings(as.double2(input))) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#define STRICT_R_HEADERS | ||
#include <Rcpp.h> | ||
|
||
#include "fast_float/fast_float.h" | ||
|
||
/* essentially isBlankString(), but returns bool instead of Rboolean */ | ||
bool is_only_whitespace(const char *s) noexcept { | ||
while (*s) { | ||
if (!std::isspace(static_cast<unsigned char>(*s))) { | ||
return false; | ||
} | ||
++s; | ||
} | ||
return true; | ||
} | ||
|
||
//' Ultra efficient string-to-`double` Conversion | ||
//' | ||
//' For `character` `vector`s,`as.double2()` is a drop-in replacement for `base::as.double()`. | ||
//' | ||
//' @param x A vector of type `character`. | ||
//' | ||
//' @seealso [base::as.double()] | ||
//' | ||
//' @examples | ||
//' set.seed(8675309) | ||
//' input <- sample(c( | ||
//' paste0(" \r\n\t\f\v", c(0.0, sqrt(seq(1, 10))), " \r\n\t\f\v"), | ||
//' c("NaN", "-NaN", "nan", "-nan", | ||
//' "Inf", "-Inf", "inf", "-inf", "infinity", "-infinity", | ||
//' NA_character_, | ||
//' " 1970-01-01", "1970-01-02 ") | ||
//' )) | ||
//' input | ||
//' | ||
//' suppressWarnings(as.double2(input)) # NAs introduced by coercion | ||
//' | ||
//' comparison <- suppressWarnings( | ||
//' matrix(c(as.double(input), as.double2(input)), | ||
//' ncol = 2L, | ||
//' dimnames = list(NULL, c("as.double()", "as.double2()"))) | ||
//' ) | ||
//' comparison | ||
//' | ||
//' all.equal(comparison[, "as.double()"], comparison[, "as.double2()"]) | ||
//' | ||
//' @export | ||
// [[Rcpp::export(as.double2)]] | ||
Rcpp::DoubleVector | ||
as_dbl(const Rcpp::CharacterVector x) { | ||
const auto n{x.size()}; | ||
Rcpp::DoubleVector out(n, NA_REAL); | ||
bool any_failures{false}; | ||
|
||
for (R_xlen_t i = 0; i < n; ++i) { | ||
const char *cstring = CHAR(x[i]); | ||
double d; | ||
const auto res = fast_float::from_chars( | ||
cstring, cstring + std::char_traits<char>::length(cstring), d); | ||
|
||
if (res.ec == std::errc() && is_only_whitespace(res.ptr)) { | ||
out[i] = d; | ||
} else { | ||
any_failures = true; | ||
} | ||
} | ||
|
||
if (any_failures) { | ||
Rcpp::warning("NAs introduced by coercion"); | ||
} | ||
|
||
return out; | ||
} |