Skip to content

Array isapprox behavior when the norm is NaN #17650

Closed

Description

When using isapprox to compare arrays, the current definition, introduced in #12472, seems to have some (IMHO) unideal behavior in the presence of NaNs:

julia> [0.01311489462160816,Inf]  [0.013114894621608135,Inf]
false

julia> all(map(isapprox, [0.01311489462160816,Inf], [0.013114894621608135,Inf]))
true

This is how isapprox is currently defined for arrays:

function isapprox{T<:Number,S<:Number}(x::AbstractArray{T}, y::AbstractArray{S}; rtol::Real=Base.rtoldefault(T,S), atol::Real=0, norm::Function=vecnorm)
    d = norm(x - y)
    return isfinite(d) ? d <= atol + rtol*max(norm(x), norm(y)) : x == y
end

Whenever norm evaluates to NaN, which happens for example when x and y both have Inf in a corresponding index, the comparison falls back to checking exact equality using ==. That will be false if there are finite, approximately equal floating point values in other corresponding indices in x and y. This is causing a test failure in HypothesisTests.jl.

@stevengj was against checking approximate equality element-wise for arrays but I'm not entirely clear on why. Is there anything that can or should be changed in the current implementation, or is there some other way I can get around the test failure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    mathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions