diff --git a/src/FixedPointDecimals.jl b/src/FixedPointDecimals.jl index 0b78e3c..660c345 100644 --- a/src/FixedPointDecimals.jl +++ b/src/FixedPointDecimals.jl @@ -294,6 +294,14 @@ end for remfn in [:rem, :mod, :mod1, :min, :max] @eval Base.$remfn(x::T, y::T) where {T <: FD} = reinterpret(T, $remfn(x.i, y.i)) end +if VERSION >= v"1.4.0-" + # TODO: Add support for (RoundFromZero, RoundNearest, RoundNearestTiesAway), which + # aren't supported for Ints. + for R in (RoundToZero, RoundUp, RoundDown) + Base.rem(x::T, y::T, r::typeof(R)) where {T <: FD} = reinterpret(T, rem(x.i, y.i, r)) + end +end + # 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] diff --git a/test/runtests.jl b/test/runtests.jl index 33fe0ac..d62329d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -537,13 +537,26 @@ end @test one(FD{Int32, 2}) รท one(FD{Int64, 6}) isa FD{Int64, 6} end + @testset "rem with rounding modes" begin + if VERSION >= v"1.4.0-" + # TODO: Test RoundFromZero, RoundNearest, RoundNearestTiesAway + # See: https://github.com/JuliaLang/julia/issues/34519 + @testset for x in keyvalues[FD2], + R in (RoundToZero, RoundUp, RoundDown) + @test rem(x, 2one(x), R) === rem(x, 2, R) === reinterpret(FD2, rem(x.i, FD2(2).i, R)) + 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 + @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 + # TODO: Test RoundFromZero -- https://github.com/JuliaLang/julia/issues/34519 + @testset for x in keyvalues[FD2], + 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 @testset for x in keyvalues[FD2], f in (fld, cld, fld1, div)