r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
888 stars 318 forks source link

`expect_equal` fails unless we set `tolerance` #1897

Closed stla closed 10 months ago

stla commented 10 months ago

Hello,

library(Carlson)

nome <- function(m) {
  if(m == 0) {
    return(as.complex(0))
  }
  if(m == 1) {
    return(as.complex(1))
  }
  exp(-pi * elliptic_F(pi/2, 1-m) / elliptic_F(pi/2, m))
}

nome(-2)
# -0.06823783+0i

testthat::expect_equal(nome(-2), as.complex(-0.06823783))
# Erreur : nome(-2) (`actual`) not equal to as.complex(-0.06823783) (`expected`).
# 
# Re(actual) != Re(expected) but don't know how to show the difference

This works if we specifiy tolerance.

Latest versions of 'testthat', R and RStudio, Windows 10.

hadley commented 10 months ago

Minimal reprex:

testthat::local_edition(3)
x <- -6.82378277453383e-02-8e-17i
y <- as.complex(-0.06823783)

testthat::expect_equal(x, y)
#> Error: `x` (`actual`) not equal to `y` (`expected`).
#> 
#> Re(actual) != Re(expected) but don't know how to show the difference
waldo::compare(x, y)
#> `Re(old)`: -0.068237828
#> `Re(new)`: -0.068237830
#> 
#> `Im(old)`: -0.00000000000000008
#> `Im(new)`:  0.00000000000000000

Created on 2023-11-28 with reprex v2.0.2.9000

The waldo output looks fine so this does indeed appear to be a testthat problem.

hadley commented 10 months ago

Oops, no, it is a waldo problem with the default testthat tolerance:

x <- -6.82378277453383e-02-8e-17i
y <- as.complex(-0.06823783)

waldo::compare(x, y, tolerance = testthat::testthat_tolerance())
#> Re(old) != Re(new) but don't know how to show the difference

Created on 2023-11-29 with reprex v2.0.2.9000

hadley commented 10 months ago

Now tracked in https://github.com/r-lib/waldo/issues/187