@@ -302,10 +302,7 @@ function Base.convert(::Type{FD{T, f}}, x::Integer) where {T, f}
302
302
xT = convert (T, x) # This won't throw, since we already checked, above.
303
303
# Perform x * C, and check for overflow. This is cheaper than a widemul, especially for
304
304
# 128-bit T, since those widen into a BigInt.
305
- v, overflow = Base. mul_with_overflow (xT, C)
306
- if overflow
307
- throw_inexact ()
308
- end
305
+ v = _mul_checked_overflow (throw_inexact, xT, C)
309
306
reinterpret (FD{T, f}, v)
310
307
end
311
308
function Base. convert (:: Type{FD{BigInt, f}} , x:: Integer ) where {f}
@@ -321,6 +318,15 @@ function Base.convert(::Type{FD{BigInt, f}}, x::Integer) where {f}
321
318
reinterpret (FD{BigInt, f}, v)
322
319
end
323
320
321
+ # x * y - if overflow, report an InexactError(FDT, )
322
+ function _mul_checked_overflow (overflow_callback, x, y)
323
+ v, overflow = Base. mul_with_overflow (x, y)
324
+ if overflow
325
+ overflow_callback ()
326
+ end
327
+ return v
328
+ end
329
+
324
330
Base. convert (:: Type{T} , x:: AbstractFloat ) where {T <: FD } = round (T, x)
325
331
326
332
function Base. convert (:: Type{FD{T, f}} , x:: Rational ) where {T, f}
@@ -332,7 +338,10 @@ function Base.convert(::Type{FD{T, f}}, x::FD{U, g}) where {T, f, U, g}
332
338
if f ≥ g
333
339
# Compute `10^(f - g)` without overflow
334
340
powt = div (coefficient (FD{T, f}), coefficient (FD{U, g}))
335
- reinterpret (FD{T, f}, T (widemul (x. i, powt)))
341
+ v = _mul_checked_overflow (promote (x. i, powt)... ) do
342
+ throw (InexactError (:convert , FD{T, f}, x))
343
+ end
344
+ reinterpret (FD{T, f}, T (v))
336
345
else
337
346
# Compute `10^(g - f)` without overflow
338
347
powt = div (coefficient (FD{U, g}), coefficient (FD{T, f}))
0 commit comments