From cd1f38dd754bd810dd6a9e57d6d7df29b7f88284 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 22 Oct 2024 17:39:52 -0500 Subject: [PATCH] Check doubles are in range --- R/num_equal.R | 12 ++++++++++-- tests/testthat/test-num_equal.R | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/R/num_equal.R b/R/num_equal.R index 8c4194e..9107019 100644 --- a/R/num_equal.R +++ b/R/num_equal.R @@ -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 diff --git a/tests/testthat/test-num_equal.R b/tests/testthat/test-num_equal.R index f29d04e..3d633d1 100644 --- a/tests/testthat/test-num_equal.R +++ b/tests/testthat/test-num_equal.R @@ -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)) +})