@@ -123,6 +123,12 @@ function Base.widemul(x::FD{T, f}, y::Integer) where {T, f}
123
123
reinterpret (FD{typeof (i), f}, i)
124
124
end
125
125
Base. widemul (x:: Integer , y:: FD ) = widemul (y, x)
126
+ # needed to avoid ambiguities
127
+ function Base. widemul (x:: FD{T, f} , y:: Bool ) where {T, f}
128
+ i = widemul (x. i, y)
129
+ reinterpret (FD{typeof (i), f}, i)
130
+ end
131
+ Base. widemul (x:: Bool , y:: FD ) = widemul (y, x)
126
132
127
133
"""
128
134
_round_to_even(quotient, remainder, divisor)
@@ -239,13 +245,20 @@ for fn in [:trunc, :floor, :ceil]
239
245
@eval (Base.$ fn (:: Type{TI} , x:: FD ):: TI ) where {TI <: Integer } = $ fn (x)
240
246
241
247
# round/trunc/ceil/flooring to FD; generic
242
- @eval function Base . $fn (:: Type{FD{T, f}} , x:: Real ) where {T, f}
248
+ @eval function $ ( Symbol ( :_ , fn)) (:: Type{FD{T, f}} , x:: Real ) where {T, f}
243
249
powt = coefficient (FD{T, f})
244
250
# Use machine Float64 if possible, but fall back to BigFloat if we need
245
251
# more precision. 4f bits suffices.
246
252
val = _apply_exact_float ($ (Symbol (fn, " mul" )), T, x, powt)
247
253
reinterpret (FD{T, f}, val)
248
254
end
255
+ @eval function Base. $fn (:: Type{FD{T, f}} , x:: Real ) where {T, f}
256
+ $ (Symbol (:_ , fn))(FD{T, f}, x)
257
+ end
258
+ # needed to avoid ambiguities
259
+ @eval function Base. $fn (:: Type{FD{T, f}} , x:: Rational ) where {T, f}
260
+ $ (Symbol (:_ , fn))(FD{T, f}, x)
261
+ end
249
262
end
250
263
function Base. round (:: Type{TI} , x:: FD , :: RoundingMode{:Nearest} = RoundNearest) where {TI <: Integer }
251
264
convert (TI, round (x)):: TI
@@ -254,9 +267,18 @@ function Base.round(::Type{FD{T, f}}, x::Real, ::RoundingMode{:Nearest}=RoundNea
254
267
reinterpret (FD{T, f}, round (T, x * coefficient (FD{T, f})))
255
268
end
256
269
257
- # needed to avoid ambiguity
258
- function Base. round (:: Type{FD{T, f}} , x:: Rational , :: RoundingMode{:Nearest} = RoundNearest) where {T, f}
259
- reinterpret (FD{T, f}, round (T, x * coefficient (FD{T, f})))
270
+ # needed to avoid ambiguities
271
+ @static if Base. VERSION >= v " 1.6"
272
+ function Base. round (:: Type{FD{T, f}} , x:: Rational{Tr} , :: RoundingMode{:Nearest} = RoundNearest) where {T, Tr, f}
273
+ reinterpret (FD{T, f}, round (T, x * coefficient (FD{T, f})))
274
+ end
275
+ function Base. round (:: Type{FD{T, f}} , x:: Rational{Bool} , :: RoundingMode{:Nearest} = RoundNearest) where {T, f}
276
+ reinterpret (FD{T, f}, round (T, x * coefficient (FD{T, f})))
277
+ end
278
+ else
279
+ function Base. round (:: Type{FD{T, f}} , x:: Rational , :: RoundingMode{:Nearest} = RoundNearest) where {T, f}
280
+ reinterpret (FD{T, f}, round (T, x * coefficient (FD{T, f})))
281
+ end
260
282
end
261
283
262
284
# conversions and promotions
@@ -325,6 +347,8 @@ function Base.convert(::Type{TR}, x::FD{T, f}) where {TR <: Rational, T, f}
325
347
end
326
348
327
349
(:: Type{T} )(x:: FD ) where {T<: Union{AbstractFloat,Integer,Rational} } = convert (T, x)
350
+ # needed to avoid ambiguities
351
+ Bool (x:: FD ) = x == 0 ? false : (x == 1 ? true : throw (InexactError (:Bool , Bool, x)))
328
352
329
353
Base. promote_rule (:: Type{FD{T, f}} , :: Type{<:Integer} ) where {T, f} = FD{T, f}
330
354
Base. promote_rule (:: Type{<:FD} , :: Type{TF} ) where {TF <: AbstractFloat } = TF
0 commit comments