From 7fb2dfb68c63c0a77b8c859c294d8b7f03f337d6 Mon Sep 17 00:00:00 2001 From: Zachary P Christensen Date: Wed, 8 Apr 2020 19:54:26 -0400 Subject: [PATCH] Support mixed argument types to `div` and friends (#317) * Mixed arg type div and friends support * Patch bump * Convert units for div * Fix syntax * Minor version bump * Add optional `RoundingMode` to `div` * Remove unecessary `unit(x)` code * Fix errors on <1.4 --- Project.toml | 4 ++-- src/quantities.jl | 27 +++++++++++++++++++++++---- test/runtests.jl | 6 ++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 7bf2fdb9..e21786b9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Unitful" uuid = "1986cc42-f94f-5a68-af5c-568840ba703d" -version = "1.0.0" +version = "1.1.0" [deps] ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" @@ -8,8 +8,8 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] -julia = "1" ConstructionBase = "1" +julia = "1" [extras] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/quantities.jl b/src/quantities.jl index 6de585d0..31ee0cbd 100644 --- a/src/quantities.jl +++ b/src/quantities.jl @@ -56,13 +56,32 @@ end # ambiguity resolution //(x::AbstractQuantity, y::Complex) = Quantity(//(x.val, y), unit(x)) -for f in (:div, :fld, :cld) - @eval function ($f)(x::AbstractQuantity, y::AbstractQuantity) - z = uconvert(unit(y), x) # TODO: use promote? - ($f)(z.val,y.val) +for f in (:fld, :cld) + @eval begin + function ($f)(x::AbstractQuantity, y::AbstractQuantity) + z = uconvert(unit(y), x) # TODO: use promote? + ($f)(z.val,y.val) + end + + ($f)(x::Number, y::AbstractQuantity) = Quantity(($f)(x, ustrip(y)), unit(x) / unit(y)) + + ($f)(x::AbstractQuantity, y::Number) = Quantity(($f)(ustrip(x), y), unit(x)) end end +function div(x::AbstractQuantity, y::AbstractQuantity, r...) + z = uconvert(unit(y), x) # TODO: use promote? + div(z.val,y.val, r...) +end + +function div(x::Number, y::AbstractQuantity, r...) + Quantity(div(x, ustrip(y), r...), unit(x) / unit(y)) +end + +function div(x::AbstractQuantity, y::Number, r...) + Quantity(div(ustrip(x), y, r...), unit(x)) +end + for f in (:mod, :rem) @eval function ($f)(x::AbstractQuantity, y::AbstractQuantity) z = uconvert(unit(y), x) # TODO: use promote? diff --git a/test/runtests.jl b/test/runtests.jl index f227bcf5..af76f124 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -561,7 +561,13 @@ end @test m / missing === missing # Unit / missing @test missing / m === missing # Missing / Unit (// is not defined for Missing) @test @inferred(div(10m, -3cm)) === -333 + @test @inferred(div(10m, 3)) === 3m + @test @inferred(div(10, 3m)) === 3/m @test @inferred(fld(10m, -3cm)) === -334 + @test @inferred(fld(10m, 3)) === 3m + @test @inferred(fld(10, 3m)) === 3/m + @test @inferred(cld(10m, 3)) === 4m + @test @inferred(cld(10, 3m)) === 4/m @test rem(10m, -3cm) == 1.0cm @test mod(10m, -3cm) == -2.0cm @test mod(1hr+3minute+5s, 24s) == 17s