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