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

Add custom overloads for rem(x,y,::RoundingMode) for FixedDecimals #53

Open
wants to merge 2 commits into
base: nhd-explicit-base-extending
Choose a base branch
from

Conversation

NHDaly
Copy link
Member

@NHDaly NHDaly commented Feb 1, 2020

However, the simple overload does not seem to work, since it is
ambiguous with rem(x,y,::RoundingMode{Up}), for example, so for rem,
we're manually overloading all RoundingModes.

However, the simple overload does not seem to work, since it is
ambiguous with `rem(x,y,::RoundingMode{Up})`, for example, so for `rem`,
we're manually overloading all RoundingModes.
@NHDaly
Copy link
Member Author

NHDaly commented Feb 1, 2020

@TotalVerb I originally added the straightforward method, below, but it gave a MethodAmbiguity error:

julia> Base.rem(x::T, y::T, r::RoundingMode) where {T <: FD} = reinterpret(T, rem(x.i, y.i, r))
julia> rem(x, x, RoundUp)
ERROR: MethodError: rem(::FixedDecimal{Int64,2}, ::FixedDecimal{Int64,2}, ::RoundingMode{:Up}) is ambiguous. Candidates:
  rem(x::T, y::T, r::RoundingMode) where T<:FixedDecimal in Main at REPL[20]:1
  rem(x, y, ::RoundingMode{:Up}) in Base at div.jl:69
Possible fix, define
  rem(::T, ::T, ::RoundingMode{:Up}) where T<:FixedDecimal

So i implemented all the more specific methods instead.

This actually leads me to wonder whether we should be doing the div() methods the same way...


And in fact, the generic definition of rem provided in Base, rem(x, y, ::RoundingMode{:Up}) = mod(x,-y) means that we actually already support the 3-arg rem out of the box without doing anything more in this package... I now remember that i knew this already, which is why i didn't implement it last time -- i wanted to ask someone about this.

Those defs are here:
https://github.com/JuliaLang/julia/blob/fbc2c0aec19a7c415011c1abd4f05622ac9c41e7/base/div.jl#L67-L69

I'm wondering if we're not supposed to be overloading three-argument div and rem for generic ::RoundingModes? The API isn't super clear or well-documented.

@coveralls
Copy link

coveralls commented Feb 1, 2020

Coverage Status

Coverage increased (+0.02%) to 97.531% when pulling b585127 on nhd-rem-rounding-mode into 317c82b on nhd-explicit-base-extending.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.531% when pulling c660eb5 on nhd-rem-rounding-mode into 317c82b on nhd-explicit-base-extending.

@NHDaly
Copy link
Member Author

NHDaly commented Feb 1, 2020

Additionally, one other concern:

The default 3-arg Base.rem implementation provided above doesn't call promote like the 2-arg versions do, which means that our newly added methods aren't being triggered for non-FixedDecimal types. I could probably add the overloads to call promote, but this might be worth filing with julia itself?

julia> @which rem(FD2(0.5), FD2(2), RoundToZero)
rem(x::T, y::T, r::RoundingMode{:ToZero}) where T<:FixedDecimal in FixedPointDecimals at /Users/daly/julia-dev/FixedPointDecimals/src/FixedPointDecimals.jl:302

julia> @which rem(FD2(0.5), 2, RoundToZero)
rem(x, y, ::RoundingMode{:ToZero}) in Base at div.jl:67

@NHDaly NHDaly changed the title Add support for rem(x,y,::RoundingMode) for FixedDecimals Add custom overloads for rem(x,y,::RoundingMode) for FixedDecimals Feb 1, 2020
@TotalVerb
Copy link
Collaborator

This seems to be an unfortunate inconsistency within Julia itself (where the div 3-arg is being made primitive, with fld, etc. deferring to it, while the rem 3-arg is currently non-primitive and calls rem/mod). If the # TODO resolves in Base, then we will need to implement something like this PR, but until then I suppose it is not needed.

src/FixedPointDecimals.jl Outdated Show resolved Hide resolved
Apply PR Review suggestion from @omus.
@NHDaly
Copy link
Member Author

NHDaly commented Apr 19, 2020

If the # TODO resolves in Base, then we will need to implement something like this PR, but until then I suppose it is not needed.

Okay yeah, makes sense. Then i'm going to put this PR on hold until such time as it's resolved in Base?

@TotalVerb
Copy link
Collaborator

TotalVerb commented Apr 19, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants