Skip to content

Commit

Permalink
support comparison with zero
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Mar 31, 2023
1 parent 49623e0 commit 7acdd66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ 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))

@eval @inline ($j)(x::DimensionlessQuantity{T,FreeUnits{(),NoDims,nothing}}, 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::DimensionlessQuantity{T,FreeUnits{(),NoDims,nothing}}) where {T,D,U} =
iszero(y) && !(x isa AffineQuantity) ?
($i)(x.val,y.val) :
throw(DimensionError(x,y))
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

0 comments on commit 7acdd66

Please sign in to comment.