From f80caa924c9d6aecd9796c14825df8241617c16d Mon Sep 17 00:00:00 2001 From: Andrew Keller Date: Tue, 18 Oct 2016 11:22:12 -0700 Subject: [PATCH] Fix #30 --- src/Unitful.jl | 22 ++++++++++++++++++---- test/runtests.jl | 10 ++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Unitful.jl b/src/Unitful.jl index 33985906..f7a5210a 100644 --- a/src/Unitful.jl +++ b/src/Unitful.jl @@ -683,10 +683,24 @@ round(x::Quantity) = Quantity(round(x.val), unit(x)) copysign(x::Quantity, y::Number) = Quantity(copysign(x.val,y/unit(y)), unit(x)) flipsign(x::Quantity, y::Number) = Quantity(flipsign(x.val,y/unit(y)), unit(x)) -isless{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = isless(x.val, y.val) -isless(x::Quantity, y::Quantity) = isless(uconvert(unit(y), x).val, y.val) -<{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = (x.val < y.val) -<(x::Quantity, y::Quantity) = <(uconvert(unit(y), x).val,y.val) + +@inline isless{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = _isless(x,y) +@inline _isless{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = isless(x.val, y.val) +@inline _isless{T,D1,D2,U1,U2}(x::Quantity{T,D1,U1}, y::Quantity{T,D2,U2}) = throw(DimensionError()) +@inline _isless(x,y) = isless(x,y) + +isless(x::Quantity, y::Quantity) = _isless(promote(x,y)...) +isless(x::Quantity, y::Number) = _isless(promote(x,y)...) +isless(x::Number, y::Quantity) = _isless(promote(x,y)...) + +@inline <{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = _lt(x,y) +@inline _lt{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = <(x.val,y.val) +@inline _lt{T,D1,D2,U1,U2}(x::Quantity{T,D1,U1}, y::Quantity{T,D2,U2}) = throw(DimensionError()) +@inline _lt(x,y) = <(x,y) + +<(x::Quantity, y::Quantity) = _lt(promote(x,y)...) +<(x::Quantity, y::Number) = _lt(promote(x,y)...) +<(x::Number, y::Quantity) = _lt(promote(x,y)...) isapprox{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = isapprox(x.val, y.val) isapprox(x::Quantity, y::Quantity) = isapprox(uconvert(unit(y), x).val, y.val) diff --git a/test/runtests.jl b/test/runtests.jl index 635ea800..4e07f09f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -170,6 +170,16 @@ end @test V*(3+4im) == (3V+4V*im) @test (3.0+4.0im)*V == (3+4im)*V @test im*V == Complex(0,1)*V + @test 2.0m < 3.0m + @test 2m < 3.0m + @test 1μm/m < 1 + @test 1 > 1μm/m + @test 1μm/m < 1mm/m + @test 1mm/m > 1μm/m + @test_throws Unitful.DimensionError 1m < 1kg + @test_throws Unitful.DimensionError 1m < 1 + @test_throws Unitful.DimensionError 1 < 1m + @test_throws Unitful.DimensionError 1mm/m < 1m @test @inferred(fma(2.0, 3.0m, 1.0m)) === 7.0m # llvm good @test @inferred(fma(2.0, 3.0m, 35mm)) === 6.035m # llvm good @test @inferred(fma(2.0m, 3.0, 35mm)) === 6.035m # llvm good