Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally treat NaNs as equal to each other in isapprox #19936

Closed
giordano opened this issue Jan 8, 2017 · 10 comments
Closed

Optionally treat NaNs as equal to each other in isapprox #19936

giordano opened this issue Jan 8, 2017 · 10 comments
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request
Milestone

Comments

@giordano
Copy link
Contributor

giordano commented Jan 8, 2017

@test_approx_eq has been deprecated by #19880, now the suggested way to compare numbers is @test isapprox(a ,b) (or some other fancy Unicode syntax). However, @test_approx_eq had a feature that isapprox is missing: @test_approx_eq NaN NaN didn't error. When performing tests, it makes sense to treat NaNs as equal (i.e., what isequal does): even if isnan is available, having isapprox dealing nicely with NaNs may be useful for cases like isapprox([1, NaN, Inf], [1, NaN, Inf]), where there is a mixture of numbers and not-numbers.

My suggestion: optionally have isapprox treat NaN values as equal to each other (maybe only in tests).

A bit of background: in a package of mines I have a test where I was using @test_approx_eq, relying on the fact that it treated NaNs as equal. When I moved to @test + isapprox I had to define a new method for a custom type in the testing suite and now I'm fine. With this ticket my aim is not to advocate for this feature, because I currently don't need it, but to suggest a possible improvement to isapprox that may be useful above all for testing.

@yuyichao
Copy link
Contributor

This also affects Images.jl.

@JeffreySarnoff
Copy link
Contributor

Make sense to me.

function isapprox(x::Number, y::Number; 
                  rtol::Real=rtoldefault(x,y), atol::Real=0, nans::Bool=false)
    x == y ||
    (isfinite(x) && isfinite(y) && abs(x-y) <= atol + rtol*max(abs(x), abs(y))) ||
    (nans && (isnan(x) & isnan(y)))
end
julia> anan = NaN
julia> isapprox(anan, anan)
false
julia> isapprox(anan, anan, nans=true)
true

@StefanKarpinski
Copy link
Member

I guess we should really do this in this release since we're deprecating @test_approx_eq.

@StefanKarpinski StefanKarpinski added this to the 0.6.0 milestone Jan 13, 2017
@StefanKarpinski StefanKarpinski added the help wanted Indicates that a maintainer wants help on an issue or pull request label Jan 13, 2017
@StefanKarpinski
Copy link
Member

StefanKarpinski commented Jan 13, 2017

What should we call this keyword? nans=true/false is a bit inscrutable but nanseq is kind of ugly.

@StefanKarpinski
Copy link
Member

I guess nans=true/false is ok in the interpretation that nans refers to the comparison NaN ≈ NaN and the value indicates whether that comparison is true or false.

@JeffreySarnoff
Copy link
Contributor

JeffreySarnoff commented Jan 13, 2017

That was my intent. Also nanseq, reads like "nan sequence", so nans or eqnans unless equivnans.

@JeffreySarnoff
Copy link
Contributor

I have prepared candidate changes, if you would like to take a look:
https://github.com/JeffreySarnoff/julia/blob/js/isapprox/base/floatfuncs.jl
https://github.com/JeffreySarnoff/julia/blob/js/isapprox/test/floatfuncs.jl
Or I could submit them as a PR.

@StefanKarpinski
Copy link
Member

A PR would be lovely!

@JeffreySarnoff
Copy link
Contributor

PR #20022

tkelman pushed a commit that referenced this issue Jan 14, 2017
* add `nans` keyword to isapprox

* test isapprox with nans

* fix typo

* isapprox with `nans` keyword

* isapprox with `nans` keyword

* add tests for isapprox([nan],[nan])

* fix bad name

* move isapprox tests into floatapprox.jl

* issue #19936

* fixes trailing whitespace?

* quash whitespace error

* isapprox description readability

* change isapprox nan test short-circuiting

* remove trailing whitespace

* vanquish trailing whitespace
@tkelman
Copy link
Contributor

tkelman commented Jan 14, 2017

Thanks @JeffreySarnoff !

@tkelman tkelman closed this as completed Jan 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants