Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mixed argument types to div and friends #317

Merged
merged 8 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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