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

Fix #465 for isapprox with complex arrays #468

Merged
merged 13 commits into from
Jan 2, 2022
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Unitful"
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.10.0"
version = "1.10.1"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
26 changes: 21 additions & 5 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,31 @@ for (i,j) in zip((:<, :isless), (:_lt, :_isless))
end

Base.rtoldefault(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Base.rtoldefault(T)
isapprox(x::AbstractQuantity{T,D,U}, y::AbstractQuantity{T,D,U}; atol=zero(Quantity{real(T),D,U}), kwargs...) where {T,D,U} =
isapprox(x.val, y.val; atol=uconvert(unit(y), atol).val, kwargs...)

function isapprox(
x::AbstractQuantity{T,D,U},
y::AbstractQuantity{T,D,U};
atol = zero(Quantity{real(T),D,U}),
kwargs...,
) where {T,D,U}
return isapprox(x.val, y.val; atol=ustrip(unit(y), atol), kwargs...)
end

function isapprox(x::AbstractQuantity, y::AbstractQuantity; kwargs...)
dimension(x) != dimension(y) && return false
return isapprox(promote(x,y)...; kwargs...)
end

isapprox(x::AbstractQuantity, y::Number; kwargs...) = isapprox(promote(x,y)...; kwargs...)
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{T1,D,U1}), norm::Function=norm) where {T1,D,U1,T2,U2}
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}),
norm::Function=norm,
) where {T1,D,U1,T2,U2}

d = norm(x - y)
if isfinite(d)
Expand All @@ -291,8 +304,10 @@ function isapprox(x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol), zip(x, y))
end
end

isapprox(x::AbstractArray{S}, y::AbstractArray{T};
kwargs...) where {S <: AbstractQuantity,T <: AbstractQuantity} = false

function isapprox(x::AbstractArray{S}, y::AbstractArray{N};
kwargs...) where {S <: AbstractQuantity,N <: Number}
if dimension(N) == dimension(S)
Expand All @@ -301,6 +316,7 @@ function isapprox(x::AbstractArray{S}, y::AbstractArray{N};
false
end
end

isapprox(y::AbstractArray{N}, x::AbstractArray{S};
kwargs...) where {S <: AbstractQuantity,N <: Number} = isapprox(x,y; kwargs...)

Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ end
@test isapprox(1.0u"m", 1.1u"m"; rtol=0.2)
@test !isapprox(1.0u"m", 1.1u"m"; rtol=0.05)

# Issue 465:
z = fill((1+im)m, 2, 3)
@test !isapprox(z, 2z)
JeffFessler marked this conversation as resolved.
Show resolved Hide resolved
@test isapprox(z, z * (1 + 1e-15))

# Test eps
@test eps(1.0u"s") == eps(1.0)u"s"
@test eps(typeof(1.0u"s")) == eps(Float64)
Expand Down