-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New function (as.double2), plus documentation and tests. #1
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't change it now, but I have a slight preference for the old markdown 6.0.1 which I have in an opt-in directory. It does not do the stooopid dance of forcing a recompilation and rebuild only to deal with Rd creation. And with that I often say no to markdown too. (At least in stull-small projects such as this. Exceptions can be made and are made.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also realize that it is awkward with collaborators who won't have 6.0.1 around. Ah well. RStudio lacks flexibility in invoking alternate converters, sadly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, just help remind me if I forget/don't get to it before you're looking to ship to CRAN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably keep it. I am on the fence on md-in-Rd too. It's on in some projects, I am just not too consisten. No worries right now. This all looks svelte as usual.