Skip to content

Commit f19c147

Browse files
committed
Fix dispatch for checked_mul and checked_div. Fix checked_div
implementation
1 parent 9da03cd commit f19c147

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/FixedPointDecimals.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,20 +406,19 @@ function Base.checked_sub(x::T, y::T) where {T<:FD}
406406
b && Base.Checked.throw_overflowerr_binaryop(:-, x, y)
407407
return reinterpret(T, z)
408408
end
409-
function Base.checked_mul(x::FD{T,f}, y::FD{T,f}) where {T, f}
409+
function Base.checked_mul(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
410410
powt = coefficient(FD{T, f})
411411
quotient, remainder = fldmodinline(widemul(x.i, y.i), powt)
412412
v = _round_to_nearest(quotient, remainder, powt)
413413
typemin(T) <= v <= typemax(T) || Base.Checked.throw_overflowerr_binaryop(:*, x, y)
414414
return reinterpret(FD{T, f}, T(v))
415415
end
416-
function Base.checked_div(x::FD{T,f}, y::FD{T,f}) where {T,f}
416+
function Base.checked_div(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
417417
C = coefficient(FD{T, f})
418-
v1 = div(promote(x.i, y.i)...)
419-
v2, b = Base.Checked.mul_with_overflow(C, v1)
418+
# Note: The div() will already throw for divide-by-zero and typemin(T) ÷ -1.
419+
v, b = Base.Checked.mul_with_overflow(C, div(x.i, y.i))
420420
b && Base.Checked.throw_overflowerr_binaryop(:÷, x, y)
421-
typemin(T) <= v2 <= typemax(T) || Base.Checked.throw_overflowerr_binaryop(:÷, x, y)
422-
return reinterpret(FD{T, f}, T(v2))
421+
return reinterpret(FD{T, f}, v)
423422
end
424423

425424
# --------------------------

test/FixedDecimal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ end
628628
@test_throws OverflowError Base.checked_add(FD{Int8,2}(1), FD{Int8,2}(1))
629629
@test_throws OverflowError Base.checked_add(FD{Int8,2}(1), FD{Int8,2}(0.4))
630630

631-
@test_throws OverflowError Base.checked_div(Int8(1), FD{Int8,2}(0.4))
631+
@test_throws OverflowError Base.checked_div(Int8(1), FD{Int8,2}(0.5))
632632
@test_throws OverflowError Base.checked_div(FD{Int8,2}(1), FD{Int8,2}(0.4))
633633
end
634634

0 commit comments

Comments
 (0)