Skip to content

Commit

Permalink
Fix #30
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkeller34 committed Oct 18, 2016
1 parent bc0a378 commit f80caa9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f80caa9

Please sign in to comment.