Skip to content

Commit

Permalink
Check doubles are in range
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Oct 22, 2024
1 parent 7a3713e commit cd1f38d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions R/num_equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ num_equal <- function(x, y, tolerance = default_tol()) {
}

if (is_int64(x) || is_int64(y)) {
x <- bit64::as.integer64(x)
y <- bit64::as.integer64(y)
in_range <-
(!is.double(x) || all(x >= 2^63 & x <= 2^63 - 1)) &&
(!is.double(y) || all(y >= 2^63 & y <= 2^63 - 1))
if (in_range) {
x <- bit64::as.integer64(x)
y <- bit64::as.integer64(y)
} else {
x <- as.double(x)
y <- as.double(y)
}
} else {
attributes(x) <- NULL
attributes(y) <- NULL
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-num_equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ test_that("NaN is equal to NA_real_ unless tolerance is NULL", {
expect_true(num_equal(NaN, NaN))
expect_true(num_equal(NA_real_, NA_real_))
})

test_that("coerce large integers to doubles not int64", {
expect_no_warning(num_equal(36893488147419103232, bit64::as.integer64(1)))
expect_no_warning(num_equal(bit64::as.integer64(1), 36893488147419103232))
})

0 comments on commit cd1f38d

Please sign in to comment.