Skip to content

Commit

Permalink
Make the isapprox implementation closer to the LinearAlgebra implem…
Browse files Browse the repository at this point in the history
…entation (#719)
  • Loading branch information
eliascarv authored May 17, 2024
1 parent ec51fc8 commit efc3802
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ isapprox(x::Number, y::AbstractQuantity; kwargs...) = isapprox(y, x; kwargs...)
function isapprox(
x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
y::AbstractArray{<:AbstractQuantity{T2,D,U2}};
rtol::Real=Base.rtoldefault(T1,T2,0),
atol=zero(Quantity{real(T1),D,U1}),
rtol::Real=Base.rtoldefault(T1,T2,atol>zero(atol)),
nans::Bool=false,
norm::Function=norm,
) where {T1,D,U1,T2,U2}

d = norm(x - y)
if isfinite(d)
return d <= atol + rtol*max(norm(x), norm(y))
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
else
# Fall back to a component-wise approximate comparison
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol), zip(x, y))
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol, nans=nans), zip(x, y))
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,10 @@ end
@test isapprox([1cm, 200cm], [0.01m, 2.0m])
@test !isapprox([1.0], [1.0m])
@test !isapprox([1.0m], [1.0])
@test isapprox([1.0m, NaN*m], [nextfloat(1.0)*m, NaN*m], nans=true)
@test !isapprox([1.0m, NaN*m], [nextfloat(1.0)*m, NaN*m], nans=false)
@test !isapprox([1.0m, 2.0m], [1.1m, 2.2m], rtol=0.05, atol=0.2m)
@test !isapprox([1.0m], [nextfloat(1.0)*m], atol=eps(0.1)*m)
end
@testset ">> Unit stripping" begin
@test @inferred(ustrip([1u"m", 2u"m"])) == [1,2]
Expand Down

1 comment on commit efc3802

@juliohm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sostock appreciate if you can trigger a release with this fix included.

Please sign in to comment.