Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ for (i,j) in zip((:<, :isless), (:_lt, :_isless))
@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))

# comparison with dimensionless zero:
NodimsQuantity{T} = DimensionlessQuantity{T,FreeUnits{(),NoDims,nothing}}
@eval @inline ($j)(x::$NodimsQuantity{T}, y::ScalarQuantity{T,D,U}) where {T,D,U} =
iszero(x) && !(y isa AffineQuantity) ?
($i)(x.val,y.val) :
throw(DimensionError(x,y))
@eval @inline ($j)(x::ScalarQuantity{T,D,U}, y::$NodimsQuantity{T}) where {T,D,U} =
iszero(y) && !(x isa AffineQuantity) ?
($i)(x.val,y.val) :
throw(DimensionError(x,y))
# disambiguation:
@eval @inline ($j)(x::$NodimsQuantity{T}, y::$NodimsQuantity{T}) where {T} = ($i)(x.val,y.val)
end

Base.rtoldefault(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Base.rtoldefault(T)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,13 @@ Base.:*(x::MatNum, y::MatNum) = MatNum(x.mat*y.mat)
@test @inferred(1 > 1μm/m)
@test @inferred(1μm/m < 1mm/m)
@test @inferred(1mm/m > 1μm/m)
@test @inferred(0 < 2m)
@test !@inferred(2mm < 0)
@test_throws DimensionError 1m < 1kg
@test_throws DimensionError 1m < 1
@test_throws DimensionError 1 < 1m
@test_throws DimensionError 1mm/m < 1m
@test_throws DimensionError 0 < 100°C
@test Base.rtoldefault(typeof(1.0u"m")) === Base.rtoldefault(typeof(1.0))
@test Base.rtoldefault(typeof(1u"m")) === Base.rtoldefault(Int)
end
Expand Down