Skip to content

Commit

Permalink
Fix #11 (#31)
Browse files Browse the repository at this point in the history
* Fix #11

* Update Readme

* Bump ver.
  • Loading branch information
chainsawriot authored Jun 7, 2024
1 parent 94156d9 commit 850c046
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: minty
Title: Minimal Type Guesser
Version: 0.0.1
Version: 0.0.2
Authors@R: c(
person("Chung-hong", "Chan", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-6232-7530")),
person("Hadley", "Wickham", , "[email protected]", role = "aut", comment = "author of the ported code from readr"),
Expand Down
11 changes: 11 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ minty::parse_logical(c("true", "fake", "IDK"), na = "IDK")
readr::parse_logical(c("true", "fake", "IDK"), na = "IDK")
```

Some features from `vroom` have been ported to `minty`, but not `readr`.

```{r}
## tidyverse/readr#1526
minty::type_convert(data.frame(a=c("NaN", "Inf", "-INF"))) |> str()
```

```{r}
readr::type_convert(data.frame(a=c("NaN", "Inf", "-INF"))) |> str()
```

## Similar packages

For parsing ambiguous date(time)
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ readr::parse_logical(c("true", "fake", "IDK"), na = "IDK")
#> 1 2 NA 1/0/T/F/TRUE/FALSE fake
```

Some features from `vroom` have been ported to `minty`, but not `readr`.

``` r
## tidyverse/readr#1526
minty::type_convert(data.frame(a=c("NaN", "Inf", "-INF"))) |> str()
#> 'data.frame': 3 obs. of 1 variable:
#> $ a: num NaN Inf -Inf
```

``` r
readr::type_convert(data.frame(a=c("NaN", "Inf", "-INF"))) |> str()
#>
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#> a = col_character()
#> )
#> 'data.frame': 3 obs. of 1 variable:
#> $ a: chr "NaN" "Inf" "-INF"
```

## Similar packages

For parsing ambiguous date(time)
Expand Down
21 changes: 17 additions & 4 deletions src/QiParsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define FASTREAD_QI_PARSERS

#include "Rinternals.h"
#include <ctype.h> // tolower

/*
An STL iterator-based string to floating point number conversion.
Expand All @@ -17,10 +18,10 @@ bsd_strtod(const char* begin, const char** endptr, const char decimal_mark) {
if (begin == *endptr) {
return NA_REAL;
}
if (*begin == 'n' || *begin == '?') {
*endptr = begin;
return NA_REAL;
}
// if (*begin == 'n' || *begin == '?') {
// *endptr = begin;
// return NA_REAL;
// }
int sign = 0, expSign = 0, i;
double fraction, dblExp;
const char* p;
Expand Down Expand Up @@ -93,6 +94,18 @@ bsd_strtod(const char* begin, const char** endptr, const char decimal_mark) {
} else if (p != *endptr && *p == '+')
++p;

// Code ported from vroom
/* NaN */
if (*endptr - p == 3 && tolower(p[0]) == 'n' && tolower(p[1]) == 'a' &&
tolower(p[2]) == 'n') {
return NAN;
}
/* Inf */
if (*endptr - p == 3 && tolower(p[0]) == 'i' && tolower(p[1]) == 'n' &&
tolower(p[2]) == 'f') {
return sign == 1 ? -HUGE_VAL : HUGE_VAL;
}

/* If we don't have a digit or decimal point something is wrong, so return an
* NA */
if (!(isdigit(*p) || *p == decimal_mark)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-parsing-numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ test_that("scientific notation is parsed properly", {
expect_equal(parse_number("-17E-5-5"), -0.00017)
expect_equal(parse_number("1.2E-3"), 0.0012)
})

## Inf NAN NA ref gesistsa/minty#11

test_that("special cases", {
## Inf
expect_equal(parse_double("Inf"), Inf)
expect_equal(parse_double("INF"), Inf)
expect_equal(parse_double("-inf"), -Inf)
expect_equal(parse_double("-Inf"), -Inf)
expect_equal(parse_double("Infa"), NA_real_)
## NAN
expect_equal(parse_double("NAN"), NaN)
expect_equal(parse_double("Nan"), NaN)
expect_equal(parse_double("-nan"), NaN)
expect_equal(parse_double("-nan"), NaN)
expect_equal(parse_double("nana"), NA_real_)
## NA
expect_equal(parse_double("NA"), NA_real_)
expect_equal(parse_double("Naan"), NA_real_) ## in fact..
## integration
expect_equal(parse_double(c("NA", "NaN", "Inf", "3.14")), c(NA_real_, NaN, Inf, 3.14))
})

0 comments on commit 850c046

Please sign in to comment.