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

Conversation

Tokazama
Copy link
Contributor

This simply makes it so that when something like div(9m, 3) it produces 3m. Currently this errors instead.

@codecov-io
Copy link

codecov-io commented Mar 25, 2020

Codecov Report

Merging #317 into master will decrease coverage by 2.65%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #317      +/-   ##
==========================================
- Coverage   81.39%   78.74%   -2.66%     
==========================================
  Files          15       15              
  Lines        1129     1129              
==========================================
- Hits          919      889      -30     
- Misses        210      240      +30
Impacted Files Coverage Δ
src/quantities.jl 92.74% <100%> (-0.31%) ⬇️
src/range.jl 57.14% <0%> (-26.2%) ⬇️
src/units.jl 77.61% <0%> (-8.96%) ⬇️
src/types.jl 89.36% <0%> (-4.26%) ⬇️
src/dimensions.jl 91.89% <0%> (-2.71%) ⬇️
src/logarithm.jl 65.47% <0%> (-1.35%) ⬇️
src/user.jl 94.85% <0%> (-0.19%) ⬇️
src/display.jl 96% <0%> (-0.08%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 09e761b...85b86a9. Read the comment docs.

@Tokazama
Copy link
Contributor Author

Tokazama commented Mar 25, 2020

I've also added a version bump for a patch release. Hope that's okay

src/quantities.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
Project.toml Outdated Show resolved Hide resolved
src/quantities.jl Outdated Show resolved Hide resolved
@Wikunia
Copy link

Wikunia commented Apr 1, 2020

I just had a look at div again and it has a third parameter.

  julia> div(4, 3, RoundDown) # Matches fld(4, 3)
  1
  julia> div(4, 3, RoundUp) # Matches cld(4, 3)
  2
  julia> div(5, 2, RoundNearest)
  2
  julia> div(5, 2, RoundNearestTiesAway)
  3
  julia> div(-5, 2, RoundNearest)
  -2
  julia> div(-5, 2, RoundNearestTiesAway)
  -3
  julia> div(-5, 2, RoundNearestTiesUp)
  -2

You might want to have this supported as well.

@Tokazama
Copy link
Contributor Author

Tokazama commented Apr 1, 2020

Good, point. The other thing is that in more recent versions of Julia we are only suppose to overload div and other operators are suppose to follow. I'm not sure if I should be addressing this too.

end

function div(x::AbstractQuantity, y::Number, r::RoundingMode=RoundToZero)
Quantity(div(ustrip(x), y, r), unit(x) / unit(y))
Copy link

Choose a reason for hiding this comment

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

unit(y) is a free unit so it is just unit(x) isn't it?

Copy link
Contributor Author

@Tokazama Tokazama Apr 3, 2020

Choose a reason for hiding this comment

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

You're correct. I was just copying the / method above. I'll fix it.

@Tokazama
Copy link
Contributor Author

Tokazama commented Apr 3, 2020

Not sure why this worked for me on 1.4 but is broken on everything earlier. I'll look at it more later today.

@Wikunia
Copy link

Wikunia commented Apr 3, 2020

The feature I meant with the rounding mode is supported since 1.4 as far as I can see.

@Wikunia
Copy link

Wikunia commented Apr 3, 2020

Not sure how to deal with it maybe @giordano can help. You probably want to support Julia 1.0.
Is this something for Compat?

Comment on lines 72 to 75
function div(x::AbstractQuantity, y::AbstractQuantity, r::RoundingMode=RoundToZero)
z = uconvert(unit(y), x) # TODO: use promote?
div(z.val,y.val, r)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd do

Suggested change
function div(x::AbstractQuantity, y::AbstractQuantity, r::RoundingMode=RoundToZero)
z = uconvert(unit(y), x) # TODO: use promote?
div(z.val,y.val, r)
end
function div(x::AbstractQuantity, y::AbstractQuantity, r...)
z = uconvert(unit(y), x) # TODO: use promote?
div(z.val,y.val, r...)
end

so you don't even have to care to forcibly set a default value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds better than my idea.

@Tokazama
Copy link
Contributor Author

Tokazama commented Apr 3, 2020

Oh, then I just need to put the div code with the extra parameter inside a `if VERSION > v"1.3"`` block.

@Tokazama Tokazama requested a review from giordano April 7, 2020 11:16
@giordano giordano merged commit 7fb2dfb into PainterQubits:master Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants