From 0a748c7da8c463ad8f14056f8727e7ffd6c4ae48 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sat, 1 Apr 2023 01:04:00 +0300 Subject: [PATCH] support comparison with zero --- src/quantities.jl | 3 +++ test/runtests.jl | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/quantities.jl b/src/quantities.jl index 00277387..e7fa001b 100644 --- a/src/quantities.jl +++ b/src/quantities.jl @@ -274,6 +274,9 @@ 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) ? ($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) ? ($i)(x.val,y.val) : throw(DimensionError(x,y)) end Base.rtoldefault(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Base.rtoldefault(T) diff --git a/test/runtests.jl b/test/runtests.jl index 583c5cd3..6741273c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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