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

delegate all comparison methods #399

Closed
goretkin opened this issue Nov 16, 2020 · 0 comments · Fixed by #646
Closed

delegate all comparison methods #399

goretkin opened this issue Nov 16, 2020 · 0 comments · Fixed by #646

Comments

@goretkin
Copy link

As pointed out in JuliaIntervals/IntervalArithmetic.jl#237 (comment)

the assumption at

<=(x::AbstractQuantity, y::AbstractQuantity) = <(x,y) || x==y

<=(x::AbstractQuantity, y::AbstractQuantity) = <(x,y) || x==y

might not hold. Take for instance

using IntervalArithmetic: IntervalArithmetic, interval
using Unitful: Unitful, m

a = interval(0, 0)
b = interval(0, 1)
@assert a <= b
@assert a*m <= b*m  # assertion failure here

and the reason why is:

julia> a <= b
true

julia> a < b
false

julia> a == b
false

It seems worthwhile to delegate all comparison methods, not only isless and <, at

for (i,j) in zip((:<, :isless), (:_lt, :_isless))
@eval ($i)(x::AbstractQuantity, y::AbstractQuantity) = ($j)(x,y)
@eval ($i)(x::AbstractQuantity, y::Number) = ($i)(promote(x,y)...)
@eval ($i)(x::Number, y::AbstractQuantity) = ($i)(promote(x,y)...)
# promotion might not yield Quantity types
@eval @inline ($j)(x::AbstractQuantity{T1}, y::AbstractQuantity{T2}) where {T1,T2} = ($i)(promote(x,y)...)
# If it does yield Quantity types, we'll get back here,
# since at least the numeric part can be promoted.
@eval @inline ($j)(x::AbstractQuantity{T,D,U}, y::AbstractQuantity{T,D,U}) where {T,D,U} = ($i)(x.val,y.val)
@eval @inline ($j)(x::AbstractQuantity{T,D,U1}, y::AbstractQuantity{T,D,U2}) where {T,D,U1,U2} = ($i)(promote(x,y)...)
@eval @inline ($j)(x::AbstractQuantity{T,D1,U1}, y::AbstractQuantity{T,D2,U2}) where {T,D1,D2,U1,U2} = throw(DimensionError(x,y))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant