Skip to content

Commit

Permalink
Merge pull request #51 from JuliaMath/nhd-julia1.4--three-arg-div-sup…
Browse files Browse the repository at this point in the history
…port

Add support for three-arg `div` to FixedDecimals in Julia 1.4+
  • Loading branch information
NHDaly authored Feb 1, 2020
2 parents c9c4686 + b32b8b9 commit 8e80d39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,17 @@ end
for remfn in [:rem, :mod, :mod1, :min, :max]
@eval $remfn(x::T, y::T) where {T <: FD} = reinterpret(T, $remfn(x.i, y.i))
end
for divfn in [:div, :fld, :fld1]
# TODO: When we upgrade to a min julia version >=1.4 (i.e Julia 2.0), this block can be
# dropped in favor of three-argument `div`, below.
for divfn in [:div, :fld, :fld1, :cld]
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
# We don't need any widening logic, since we won't be multiplying by the coefficient.
@eval $divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
@eval Base.$divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
end
if VERSION >= v"1.4.0-"
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
# We don't need any widening logic, since we won't be multiplying by the coefficient.
Base.div(x::T, y::T, r::RoundingMode) where {T <: FD} = T(div(x.i, y.i, r))
end

convert(::Type{AbstractFloat}, x::FD) = convert(floattype(typeof(x)), x)
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ end

@test one(FD{Int32, 2}) ÷ one(FD{Int64, 6}) isa FD{Int64, 6}
end

@testset "div with rounding modes" begin
if VERSION >= v"1.4.0-"
@testset for x in keyvalues[FD2]
# TODO: Test RoundFromZero -- https://github.com/JuliaLang/julia/issues/34519
for R in (RoundToZero, RoundUp, RoundDown, RoundNearest, RoundNearestTiesAway)
@test div(x, 2one(x), R) === div(x, 2, R) === FD2(div(x.i, FD2(2).i, R))
end
end
end
@testset for x in keyvalues[FD2], f in (fld, cld, fld1, div)
@test f(x, 2one(x)) === f(x, 2) === FD2(f(x.i, FD2(2).i))
end
end
end

@testset "abs, sign" begin
Expand Down

0 comments on commit 8e80d39

Please sign in to comment.