Skip to content

Commit 0a748c7

Browse files
committed
support comparison with zero
1 parent 49623e0 commit 0a748c7

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/quantities.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ for (i,j) in zip((:<, :isless), (:_lt, :_isless))
274274
@eval @inline ($j)(x::AbstractQuantity{T,D,U}, y::AbstractQuantity{T,D,U}) where {T,D,U} = ($i)(x.val,y.val)
275275
@eval @inline ($j)(x::AbstractQuantity{T,D,U1}, y::AbstractQuantity{T,D,U2}) where {T,D,U1,U2} = ($i)(promote(x,y)...)
276276
@eval @inline ($j)(x::AbstractQuantity{T,D1,U1}, y::AbstractQuantity{T,D2,U2}) where {T,D1,D2,U1,U2} = throw(DimensionError(x,y))
277+
278+
@eval @inline ($j)(x::DimensionlessQuantity{T,FreeUnits{(),NoDims,nothing}}, y::ScalarQuantity{T,D,U}) where {T,D,U} = iszero(x) ? ($i)(x.val,y.val) : throw(DimensionError(x,y))
279+
@eval @inline ($j)(x::ScalarQuantity{T,D,U}, y::DimensionlessQuantity{T,FreeUnits{(),NoDims,nothing}}) where {T,D,U} = iszero(y) ? ($i)(x.val,y.val) : throw(DimensionError(x,y))
277280
end
278281

279282
Base.rtoldefault(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Base.rtoldefault(T)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,13 @@ Base.:*(x::MatNum, y::MatNum) = MatNum(x.mat*y.mat)
565565
@test @inferred(1 > 1μm/m)
566566
@test @inferred(1μm/m < 1mm/m)
567567
@test @inferred(1mm/m > 1μm/m)
568+
@test @inferred(0 < 2m)
569+
@test !@inferred(2mm < 0)
568570
@test_throws DimensionError 1m < 1kg
569571
@test_throws DimensionError 1m < 1
570572
@test_throws DimensionError 1 < 1m
571573
@test_throws DimensionError 1mm/m < 1m
574+
@test_throws DimensionError 0 < 100°C
572575
@test Base.rtoldefault(typeof(1.0u"m")) === Base.rtoldefault(typeof(1.0))
573576
@test Base.rtoldefault(typeof(1u"m")) === Base.rtoldefault(Int)
574577
end

0 commit comments

Comments
 (0)