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
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ 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)
RType = typeof(R)
Base.rem(x::T, y::T, r::RType) where {T <: FD} = reinterpret(T, rem(x.i, y.i, r))
NHDaly marked this conversation as resolved.
Show resolved Hide resolved
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]
Expand Down
23 changes: 18 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down