From 8f6e842f8552658e4dafc6827e1d4050a146b1a5 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 24 Feb 2020 16:31:27 -0800 Subject: [PATCH] remove compile-time interpolation this should allow it to load (but not run) on machines where libquadmath is not present. partially fixes #46. --- src/Quadmath.jl | 70 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/Quadmath.jl b/src/Quadmath.jl index 4dc8940..e5d9d97 100644 --- a/src/Quadmath.jl +++ b/src/Quadmath.jl @@ -263,46 +263,44 @@ trunc(::Type{Unsigned}, x::Float128) = trunc(Int,x) trunc(::Type{Integer}, x::Float128) = trunc(Int,x) for Ti in (Int32, Int64, Int128, UInt32, UInt64, UInt128) - let Tf = Float128 - if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf) - # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound - # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or - # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`). - @eval begin - function trunc(::Type{$Ti},x::$Tf) - if $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf)) - return unsafe_trunc($Ti,x) - else - throw(InexactError(:trunc, $Ti, x)) - end + if Ti <: Unsigned || sizeof(Ti) < sizeof(Float128) + # Here `Float128(typemin(Ti))-1` is exact, so we can compare the lower-bound + # directly. `Float128(typemax(Ti))+1` is either always exactly representable, or + # rounded to `Inf` (e.g. when `Ti==UInt128 && Float128==Float32`). + @eval begin + function trunc(::Type{$Ti},x::Float128) + if Float128(typemin($Ti)) - one(Float128) < x < Float128(typemax($Ti)) + one(Float128) + return unsafe_trunc($Ti,x) + else + throw(InexactError(:trunc, $Ti, x)) end - function (::Type{$Ti})(x::$Tf) - if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && (round(x, RoundToZero) == x) - return unsafe_trunc($Ti,x) - else - throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) - end + end + function (::Type{$Ti})(x::Float128) + if (Float128(typemin($Ti)) <= x <= Float128(typemax($Ti))) && (round(x, RoundToZero) == x) + return unsafe_trunc($Ti,x) + else + throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) end end - else - # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to - # `Tf(typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will - # be rounded up. This assumes that `Tf(typemin(Ti)) > -Inf`, which is true for - # these types, but not for `Float16` or larger integer types. - @eval begin - function trunc(::Type{$Ti},x::$Tf) - if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))) - return unsafe_trunc($Ti,x) - else - throw(InexactError(:trunc, $Ti, x)) - end + end + else + # Here `eps(Float128(typemin(Ti))) > 1`, so the only value which can be truncated to + # `Float128(typemin(Ti)` is itself. Similarly, `Float128(typemax(Ti))` is inexact and will + # be rounded up. This assumes that `Float128(typemin(Ti)) > -Inf`, which is true for + # these types, but not for `Float16` or larger integer types. + @eval begin + function trunc(::Type{$Ti},x::Float128) + if Float128(typemin($Ti)) <= x < Float128(typemax($Ti)) + return unsafe_trunc($Ti,x) + else + throw(InexactError(:trunc, $Ti, x)) end - function (::Type{$Ti})(x::$Tf) - if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && (round(x, RoundToZero) == x) - return unsafe_trunc($Ti,x) - else - throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) - end + end + function (::Type{$Ti})(x::Float128) + if (Float128(typemin($Ti)) <= x < Float128(typemax($Ti))) && (round(x, RoundToZero) == x) + return unsafe_trunc($Ti,x) + else + throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) end end end