Skip to content

Commit 8e80d39

Browse files
authored
Merge pull request #51 from JuliaMath/nhd-julia1.4--three-arg-div-support
Add support for three-arg `div` to FixedDecimals in Julia 1.4+
2 parents c9c4686 + b32b8b9 commit 8e80d39

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/FixedPointDecimals.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,17 @@ end
300300
for remfn in [:rem, :mod, :mod1, :min, :max]
301301
@eval $remfn(x::T, y::T) where {T <: FD} = reinterpret(T, $remfn(x.i, y.i))
302302
end
303-
for divfn in [:div, :fld, :fld1]
303+
# TODO: When we upgrade to a min julia version >=1.4 (i.e Julia 2.0), this block can be
304+
# dropped in favor of three-argument `div`, below.
305+
for divfn in [:div, :fld, :fld1, :cld]
304306
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
305307
# We don't need any widening logic, since we won't be multiplying by the coefficient.
306-
@eval $divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
308+
@eval Base.$divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
309+
end
310+
if VERSION >= v"1.4.0-"
311+
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
312+
# We don't need any widening logic, since we won't be multiplying by the coefficient.
313+
Base.div(x::T, y::T, r::RoundingMode) where {T <: FD} = T(div(x.i, y.i, r))
307314
end
308315

309316
convert(::Type{AbstractFloat}, x::FD) = convert(floattype(typeof(x)), x)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,20 @@ end
536536

537537
@test one(FD{Int32, 2}) ÷ one(FD{Int64, 6}) isa FD{Int64, 6}
538538
end
539+
540+
@testset "div with rounding modes" begin
541+
if VERSION >= v"1.4.0-"
542+
@testset for x in keyvalues[FD2]
543+
# TODO: Test RoundFromZero -- https://github.com/JuliaLang/julia/issues/34519
544+
for R in (RoundToZero, RoundUp, RoundDown, RoundNearest, RoundNearestTiesAway)
545+
@test div(x, 2one(x), R) === div(x, 2, R) === FD2(div(x.i, FD2(2).i, R))
546+
end
547+
end
548+
end
549+
@testset for x in keyvalues[FD2], f in (fld, cld, fld1, div)
550+
@test f(x, 2one(x)) === f(x, 2) === FD2(f(x.i, FD2(2).i))
551+
end
552+
end
539553
end
540554

541555
@testset "abs, sign" begin

0 commit comments

Comments
 (0)