Skip to content

Commit 0cf9092

Browse files
committed
Add overflow tests for fld1 and div(::RoundMode)
1 parent 4cb2d5d commit 0cf9092

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/FixedPointDecimals.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,12 @@ end
367367
if VERSION >= v"1.4.0-"
368368
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
369369
# We don't need any widening logic, since we won't be multiplying by the coefficient.
370-
@eval function Base.div(x::T, y::T, r::RoundingMode) where {T <: FD}
371-
C = coefficient(T)
372-
return reinterpret(T, C * div(x.i, y.i, r))
370+
@eval function Base.div(x::FD{T, f}, y::FD{T, f}, r::RoundingMode) where {T<:Integer, f}
371+
C = coefficient(FD{T, f})
372+
# Note: The div() will already throw for divide-by-zero and typemin(T) ÷ -1.
373+
v, b = Base.Checked.mul_with_overflow(C, div(x.i, y.i, r))
374+
b && _throw_overflowerr_op(:div, x, y)
375+
return reinterpret(FD{T, f}, v)
373376
end
374377
end
375378

test/FixedDecimal.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,27 +834,33 @@ end
834834
end
835835

836836
@testset "division" begin
837-
# TODO(PR): Is this the expected value?
838837
@test_throws OverflowError typemax(T) / T(0.5)
839838
@test_throws OverflowError typemin(T) / T(0.5)
840839
end
841840

842841
@testset "truncating division" begin
843-
# TODO(PR): Is this the expected value?
844842
@test_throws OverflowError typemax(T) ÷ T(0.5)
845843
@test_throws OverflowError typemin(T) ÷ T(0.5)
846844
@test_throws OverflowError typemax(T) ÷ eps(T)
847845
@test_throws OverflowError typemin(T) ÷ eps(T)
846+
847+
@test_throws OverflowError div(typemax(T), T(0.5), RoundUp)
848+
@test_throws OverflowError div(typemin(T), T(0.5), RoundUp)
849+
@test_throws OverflowError div(typemax(T), eps(T), RoundUp)
850+
@test_throws OverflowError div(typemin(T), eps(T), RoundUp)
848851
end
849852

850853
@testset "fld / cld" begin
851-
# TODO(PR): Is this the expected value?
852854
@test_throws OverflowError fld(typemax(T), T(0.5))
853855
@test_throws OverflowError fld(typemin(T), T(0.5))
854856
@test_throws OverflowError fld(typemax(T), eps(T))
855857
@test_throws OverflowError fld(typemin(T), eps(T))
856858

857-
# TODO(PR): Is this the expected value?
859+
@test_throws OverflowError fld1(typemax(T), T(0.5))
860+
@test_throws OverflowError fld1(typemin(T), T(0.5))
861+
@test_throws OverflowError fld1(typemax(T), eps(T))
862+
@test_throws OverflowError fld1(typemin(T), eps(T))
863+
858864
@test_throws OverflowError cld(typemax(T), T(0.5))
859865
@test_throws OverflowError cld(typemin(T), T(0.5))
860866
@test_throws OverflowError cld(typemax(T), eps(T))

0 commit comments

Comments
 (0)