@@ -421,6 +421,30 @@ function Base.checked_div(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
421
421
return reinterpret (FD{T, f}, v)
422
422
end
423
423
424
+ # We introduce a new function for this since Base.Checked only supports integers, and ints
425
+ # don't have a decimal division operation.
426
+ """
427
+ FixedPointDecimals.checked_decimal_division(x::FD, y::FD) -> FD
428
+
429
+ Calculates `x / y`, checking for overflow errors where applicable.
430
+
431
+ The overflow protection may impose a perceptible performance penalty.
432
+
433
+ See also:
434
+ - `Base.checked_div` for truncating division.
435
+ """
436
+ checked_decimal_division (x:: FD , y:: FD ) = checked_decimal_division (promote (x, y)... )
437
+ checked_decimal_division (x, y:: FD ) = checked_decimal_division (promote (x, y)... )
438
+ checked_decimal_division (x:: FD , y) = checked_decimal_division (promote (x, y)... )
439
+
440
+ function checked_decimal_division (x:: FD{T,f} , y:: FD{T,f} ) where {T<: Integer ,f}
441
+ powt = coefficient (FD{T, f})
442
+ quotient, remainder = fldmod (widemul (x. i, powt), y. i)
443
+ v = _round_to_nearest (quotient, remainder, y. i)
444
+ typemin (T) <= v <= typemax (T) || Base. Checked. throw_overflowerr_binaryop (:/ , x, y)
445
+ return reinterpret (FD{T, f}, v)
446
+ end
447
+
424
448
# --------------------------
425
449
426
450
Base. convert (:: Type{AbstractFloat} , x:: FD ) = convert (floattype (typeof (x)), x)
0 commit comments