Skip to content

Commit

Permalink
Fix NA_real -> int on new Macs
Browse files Browse the repository at this point in the history
  • Loading branch information
privefl committed Sep 9, 2024
1 parent f17593a commit afcc03b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Encoding: UTF-8
Package: bigstatsr
Type: Package
Title: Statistical Tools for Filebacked Big Matrices
Version: 1.5.17
Date: 2024-09-03
Version: 1.6.0
Date: 2024-09-09
Authors@R: c(
person("Florian", "Privé", email = "[email protected]",
role = c("aut", "cre")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## bigstatsr 1.6.0

- Fix conversion from `NA_real` to FBM type integer on new Macs.

## bigstatsr 1.5.14

- Error when variables with a zero scaling are used in e.g. `big_randomSVD()` and `big_crossprodSelf()` (#52).
Expand Down
8 changes: 6 additions & 2 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/FBM-replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inline double int2dbl(int x) {
return (x == NA_INTEGER) ? NA_REAL : x;
}

inline int dbl2int(double x) {
return (x != x) ? NA_INTEGER : x;
}

template<typename T_IN, typename T_OUT>
inline T_OUT identity(T_IN x) {
return x;
Expand Down Expand Up @@ -79,7 +83,7 @@ case 4: \
case INTSXP: REPLACE(int, as<IntegerVector>(VEC)) \
case REALSXP: { \
NumericVector vec2 = check_conv_dbl2int(VEC); \
REPLACE(int, vec2) \
REPLACE_CONV(int, vec2, dbl2int) \
} \
default: stop("R type '%s' is not supported.", Rf_type2char(r_type)); \
} \
Expand All @@ -88,7 +92,7 @@ case 6: \
case RAWSXP: REPLACE(float, as<RawVector>(VEC)) \
case LGLSXP: REPLACE_CONV(float, as<LogicalVector>(VEC), int2flt) \
case INTSXP: { \
IntegerVector vec2 = check_conv<INTSXP, float>(VEC); \
IntegerVector vec2 = check_conv<INTSXP, float>(VEC); \
REPLACE_CONV(float, vec2, int2flt) \
} \
case REALSXP: { \
Expand Down
22 changes: 14 additions & 8 deletions tests/testthat/test-FBM-convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("Downcast warnings work", {

for (gen in c("scalar", "vector", "matrix")) {

# From double
## From double
x1 <- to_gen(runif(100))
# To raw
expect_warning(X <- FBM(10, 10, x1, type = "raw"),
Expand All @@ -79,21 +79,27 @@ test_that("Downcast warnings work", {
expect_identical(X[2], NA_integer_)
expect_warning(X[2] <- Inf,
get_text("double", "integer"), fixed = TRUE)
expect_identical(X[2], NA_integer_)
expect_warning(X[2] <- NaN,
get_text("double", "integer"), fixed = TRUE)
expect_identical(X[2], NA_integer_)
expect_FBM(without_downcast_warning( FBM(10, 10, x1, type = "integer") ))
# To float
expect_warning(X <- FBM(10, 10, x1, type = "float"))
X[2] <- NA_real_
expect_identical(X[2], NA_real_)
X[2] <- NA_real_; expect_identical(X[2], NA_real_)
X[2] <- Inf; expect_identical(X[2], Inf)
X[2] <- -Inf; expect_identical(X[2], -Inf)
X[2] <- NaN; expect_identical(X[2], NaN)
# To double
expect_FBM(X <- FBM(10, 10, x1, type = "double"))
expect_identical(X[1:5] <- x1[1:5], x1[1:5])
X[2] <- NA_real_
expect_identical(X[2], NA_real_)
X[2] <- NA_real_; expect_identical(X[2], NA_real_)
X[2] <- Inf; expect_identical(X[2], Inf)
X[2] <- -Inf; expect_identical(X[2], -Inf)
X[2] <- NaN; expect_identical(X[2], NaN)


# From integer
## From integer
x2 <- to_gen(1:100 + 1e6L)
# To raw
expect_warning(X <- FBM(10, 10, x2, type = "raw"),
Expand Down Expand Up @@ -128,7 +134,7 @@ test_that("Downcast warnings work", {
expect_identical(X[2], NA_real_)


# From logical
## From logical
x3 <- to_gen(sample(c(TRUE, FALSE), 100, TRUE))
# To raw
expect_FBM(X <- FBM(10, 10, x3, type = "raw"))
Expand Down Expand Up @@ -157,7 +163,7 @@ test_that("Downcast warnings work", {
expect_identical(X[2], NA_real_)


# From raw
## From raw
x4 <- to_gen(sample(as.raw(0:255), 100, TRUE))
expect_FBM(X <- FBM(10, 10, x4, type = "raw"))
expect_identical(X[1:5] <- x4[1:5], x4[1:5])
Expand Down

0 comments on commit afcc03b

Please sign in to comment.