@@ -187,13 +187,13 @@ end
187
187
188
188
# these functions are needed to avoid InexactError when converting from the
189
189
# integer type
190
- Base.:* (x:: Integer , y:: FD{T, f} ) where {T, f} = reinterpret (FD{T, f}, T (x * y. i))
191
- Base.:* (x:: FD{T, f} , y:: Integer ) where {T, f} = reinterpret (FD{T, f}, T ( x. i * y ))
190
+ Base.:* (x:: Integer , y:: FD{T, f} ) where {T, f} = reinterpret (FD{T, f}, * ( promote (x, y. i) ... ))
191
+ Base.:* (x:: FD{T, f} , y:: Integer ) where {T, f} = reinterpret (FD{T, f}, * ( promote ( x. i, y) ... ))
192
192
193
193
function Base.:/ (x:: FD{T, f} , y:: FD{T, f} ) where {T, f}
194
194
powt = coefficient (FD{T, f})
195
195
quotient, remainder = fldmod (widemul (x. i, powt), y. i)
196
- reinterpret (FD{T, f}, T ( _round_to_nearest (quotient, remainder, y. i) ))
196
+ reinterpret (FD{T, f}, _round_to_nearest (quotient, remainder, y. i))
197
197
end
198
198
199
199
# These functions allow us to perform division with integers outside of the range of the
@@ -202,12 +202,12 @@ function Base.:/(x::Integer, y::FD{T, f}) where {T, f}
202
202
powt = coefficient (FD{T, f})
203
203
powtsq = widemul (powt, powt)
204
204
quotient, remainder = fldmod (widemul (x, powtsq), y. i)
205
- reinterpret (FD{T, f}, T ( _round_to_nearest (quotient, remainder, y. i) ))
205
+ reinterpret (FD{T, f}, _round_to_nearest (quotient, remainder, y. i))
206
206
end
207
207
208
208
function Base.:/ (x:: FD{T, f} , y:: Integer ) where {T, f}
209
209
quotient, remainder = fldmod (x. i, y)
210
- reinterpret (FD{T, f}, T ( _round_to_nearest (quotient, remainder, y) ))
210
+ reinterpret (FD{T, f}, _round_to_nearest (quotient, remainder, y))
211
211
end
212
212
213
213
# integerification
@@ -362,12 +362,22 @@ end
362
362
for divfn in [:div , :fld , :fld1 , :cld ]
363
363
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
364
364
# We don't need any widening logic, since we won't be multiplying by the coefficient.
365
- @eval Base.$ divfn (x:: T , y:: T ) where {T <: FD } = T ($ divfn (x. i, y. i))
365
+ # @eval Base.$divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
366
+ # @eval Base.$divfn(x::T, y::T) where {T <: FD} = $divfn(promote(x.i, y.i)...)
367
+ # TODO (PR): I'm not sure about this one...
368
+ # What should it *mean* for `typemax(FD) ÷ FD(0.5)` to overflow?
369
+ @eval function Base. $divfn (x:: T , y:: T ) where {T <: FD }
370
+ C = coefficient (T)
371
+ return reinterpret (T, C * $ divfn (promote (x. i, y. i)... ))
372
+ end
366
373
end
367
374
if VERSION >= v " 1.4.0-"
368
375
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
369
376
# We don't need any widening logic, since we won't be multiplying by the coefficient.
370
- Base. div (x:: T , y:: T , r:: RoundingMode ) where {T <: FD } = T (div (x. i, y. i, r))
377
+ @eval function Base. div (x:: T , y:: T , r:: RoundingMode ) where {T <: FD }
378
+ C = coefficient (T)
379
+ return reinterpret (T, C * div (x. i, y. i, r))
380
+ end
371
381
end
372
382
373
383
Base. convert (:: Type{AbstractFloat} , x:: FD ) = convert (floattype (typeof (x)), x)
0 commit comments