Skip to content

Commit

Permalink
Support mixed argument types to div and friends (#317)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Tokazama authored Apr 8, 2020
1 parent 09e761b commit 7fb2dfb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name = "Unitful"
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.0.0"
version = "1.1.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
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"
Expand Down
27 changes: 23 additions & 4 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 7fb2dfb

@giordano
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/12608

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 7fb2dfb68c63c0a77b8c859c294d8b7f03f337d6
git push origin v1.1.0

Please sign in to comment.