@@ -263,46 +263,44 @@ trunc(::Type{Unsigned}, x::Float128) = trunc(Int,x)
263263trunc (:: Type{Integer} , x:: Float128 ) = trunc (Int,x)
264264
265265for Ti in (Int32, Int64, Int128, UInt32, UInt64, UInt128)
266- let Tf = Float128
267- if Ti <: Unsigned || sizeof (Ti) < sizeof (Tf)
268- # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound
269- # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or
270- # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`).
271- @eval begin
272- function trunc (:: Type{$Ti} ,x:: $Tf )
273- if $ (Tf (typemin (Ti))- one (Tf)) < x < $ (Tf (typemax (Ti))+ one (Tf))
274- return unsafe_trunc ($ Ti,x)
275- else
276- throw (InexactError (:trunc , $ Ti, x))
277- end
266+ if Ti <: Unsigned || sizeof (Ti) < sizeof (Float128)
267+ # Here `Float128(typemin(Ti))-1` is exact, so we can compare the lower-bound
268+ # directly. `Float128(typemax(Ti))+1` is either always exactly representable, or
269+ # rounded to `Inf` (e.g. when `Ti==UInt128 && Float128==Float32`).
270+ @eval begin
271+ function trunc (:: Type{$Ti} ,x:: Float128 )
272+ if Float128 (typemin ($ Ti)) - one (Float128) < x < Float128 (typemax ($ Ti)) + one (Float128)
273+ return unsafe_trunc ($ Ti,x)
274+ else
275+ throw (InexactError (:trunc , $ Ti, x))
278276 end
279- function ( :: Type{$Ti} )(x :: $Tf )
280- if ( $ ( Tf ( typemin (Ti))) <= x <= $ ( Tf ( typemax (Ti)))) && ( round (x, RoundToZero) == x )
281- return unsafe_trunc ( $ Ti, x)
282- else
283- throw ( InexactError ( $ ( Expr ( :quote ,Ti . name . name)), $ Ti, x))
284- end
277+ end
278+ function ( :: Type{$Ti} )(x :: Float128 )
279+ if ( Float128 ( typemin ( $ Ti)) <= x <= Float128 ( typemax ( $ Ti))) && ( round (x, RoundToZero) == x)
280+ return unsafe_trunc ( $ Ti,x)
281+ else
282+ throw ( InexactError ( $ ( Expr ( :quote ,Ti . name . name)), $ Ti, x))
285283 end
286284 end
287- else
288- # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to
289- # `Tf( typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will
290- # be rounded up. This assumes that `Tf(typemin (Ti)) > -Inf`, which is true for
291- # these types, but not for `Float16` or larger integer types.
292- @eval begin
293- function trunc ( :: Type{$Ti} ,x :: $Tf )
294- if $ ( Tf ( typemin (Ti))) <= x < $ ( Tf ( typemax (Ti)) )
295- return unsafe_trunc ( $ Ti,x )
296- else
297- throw ( InexactError ( :trunc , $ Ti, x))
298- end
285+ end
286+ else
287+ # Here `eps(Float128( typemin(Ti))) > 1`, so the only value which can be truncated to
288+ # `Float128(typemin(Ti)` is itself. Similarly, `Float128(typemax (Ti))` is inexact and will
289+ # be rounded up. This assumes that `Float128(typemin(Ti)) > -Inf`, which is true for
290+ # these types, but not for `Float16` or larger integer types.
291+ @eval begin
292+ function trunc ( :: Type{$Ti} ,x :: Float128 )
293+ if Float128 ( typemin ( $ Ti)) <= x < Float128 ( typemax ( $ Ti) )
294+ return unsafe_trunc ( $ Ti,x)
295+ else
296+ throw ( InexactError ( :trunc , $ Ti, x))
299297 end
300- function ( :: Type{$Ti} )(x :: $Tf )
301- if ( $ ( Tf ( typemin (Ti))) <= x < $ ( Tf ( typemax (Ti)))) && ( round (x, RoundToZero) == x )
302- return unsafe_trunc ( $ Ti, x)
303- else
304- throw ( InexactError ( $ ( Expr ( :quote ,Ti . name . name)), $ Ti, x))
305- end
298+ end
299+ function ( :: Type{$Ti} )(x :: Float128 )
300+ if ( Float128 ( typemin ( $ Ti)) <= x < Float128 ( typemax ( $ Ti))) && ( round (x, RoundToZero) == x)
301+ return unsafe_trunc ( $ Ti,x)
302+ else
303+ throw ( InexactError ( $ ( Expr ( :quote ,Ti . name . name)), $ Ti, x))
306304 end
307305 end
308306 end
0 commit comments