Skip to content

Commit

Permalink
Merge becc3df into 1768c58
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly authored Dec 5, 2018
2 parents 1768c58 + becc3df commit 7b5f9c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ 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]
@eval $divfn(x::T, y::T) where {T <: FD} = $divfn(x.i, y.i)
# 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))
end

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

@testset "truncating div" begin
@testset "div by 1" begin
@testset for x in keyvalues[FD2]
@test x ÷ one(x) == trunc(x)

# signed integers using two's complement have one additional negative value
if x < 0 && trunc(x) == typemin(x)
@test_throws InexactError x ÷ -one(x)
else
@test x ÷ -one(x) == -trunc(x)
end
end
end
@testset "div by 2" begin
@testset for x in keyvalues[FD2]
@test x ÷ 2one(x) == x ÷ 2 == x.i ÷ FD2(2).i
end
end
end

@testset "abs, sign" begin
@testset for T in [FD2, WFD4]
for x in keyvalues[T]
Expand Down

0 comments on commit 7b5f9c0

Please sign in to comment.