Skip to content

Commit efc3802

Browse files
authored
Make the isapprox implementation closer to the LinearAlgebra implementation (#719)
1 parent ec51fc8 commit efc3802

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/quantities.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,17 @@ isapprox(x::Number, y::AbstractQuantity; kwargs...) = isapprox(y, x; kwargs...)
265265
function isapprox(
266266
x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
267267
y::AbstractArray{<:AbstractQuantity{T2,D,U2}};
268-
rtol::Real=Base.rtoldefault(T1,T2,0),
269268
atol=zero(Quantity{real(T1),D,U1}),
269+
rtol::Real=Base.rtoldefault(T1,T2,atol>zero(atol)),
270+
nans::Bool=false,
270271
norm::Function=norm,
271272
) where {T1,D,U1,T2,U2}
272-
273273
d = norm(x - y)
274274
if isfinite(d)
275-
return d <= atol + rtol*max(norm(x), norm(y))
275+
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
276276
else
277277
# Fall back to a component-wise approximate comparison
278-
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol), zip(x, y))
278+
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol, nans=nans), zip(x, y))
279279
end
280280
end
281281

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ end
15421542
@test isapprox([1cm, 200cm], [0.01m, 2.0m])
15431543
@test !isapprox([1.0], [1.0m])
15441544
@test !isapprox([1.0m], [1.0])
1545+
@test isapprox([1.0m, NaN*m], [nextfloat(1.0)*m, NaN*m], nans=true)
1546+
@test !isapprox([1.0m, NaN*m], [nextfloat(1.0)*m, NaN*m], nans=false)
1547+
@test !isapprox([1.0m, 2.0m], [1.1m, 2.2m], rtol=0.05, atol=0.2m)
1548+
@test !isapprox([1.0m], [nextfloat(1.0)*m], atol=eps(0.1)*m)
15451549
end
15461550
@testset ">> Unit stripping" begin
15471551
@test @inferred(ustrip([1u"m", 2u"m"])) == [1,2]

0 commit comments

Comments
 (0)