From 4b2330b7d07be01c5c24cb1edb01f8f3dadbb0fa Mon Sep 17 00:00:00 2001 From: jverzani Date: Thu, 17 Jan 2019 17:09:54 -0500 Subject: [PATCH 1/2] close issue 159 --- src/Polynomials.jl | 11 ++++++++++- test/runtests.jl | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Polynomials.jl b/src/Polynomials.jl index 63959711..184184bc 100644 --- a/src/Polynomials.jl +++ b/src/Polynomials.jl @@ -560,11 +560,20 @@ function polyder(p::Poly{T}, order::Int=1) where {T} _polyder(p, order) end +# _int(T) returns matching Integer type to T +# to avoid overflow in `prod` usage below +_int(::Type{T}) where {T <: Union{Integer, Int8, Int16, Int64, Int128, BigInt}} = T +_int(::Type{Float16}) = Int16 +_int(::Type{Float32}) = Int32 +_int(::Type{Float64}) = Int64 +_int(::Type{BigFloat}) = BigInt +_int(x) = Int + function _polyder(p::Poly{T}, order::Int=1) where {T} n = length(p) a2 = Vector{T}(undef, n-order) for i = order:n-1 - a2[i-order+1] = p[i] * prod((i-order+1):i) + a2[i-order+1] = p[i] * prod((one(_int(T)) * (i-order+1)):i) end return Poly(a2, p.var) diff --git a/test/runtests.jl b/test/runtests.jl index f7b209a7..0037ddf6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -391,3 +391,5 @@ xx = Real[20.0, 30.0, 40.0] yy = Real[15.7696, 21.4851, 28.2463] polyfit(xx,yy,2) +## Issue with overflow and polyder Issue #159 +@test !iszero(polyder(Poly(BigInt[0, 1])^100, 100)) From 2bb0ccffc0cffd6d3cea92694cabf287b8fa4f0c Mon Sep 17 00:00:00 2001 From: jverzani Date: Fri, 18 Jan 2019 10:59:56 -0500 Subject: [PATCH 2/2] better fix to #159 --- src/Polynomials.jl | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Polynomials.jl b/src/Polynomials.jl index 184184bc..b70b051c 100644 --- a/src/Polynomials.jl +++ b/src/Polynomials.jl @@ -560,20 +560,11 @@ function polyder(p::Poly{T}, order::Int=1) where {T} _polyder(p, order) end -# _int(T) returns matching Integer type to T -# to avoid overflow in `prod` usage below -_int(::Type{T}) where {T <: Union{Integer, Int8, Int16, Int64, Int128, BigInt}} = T -_int(::Type{Float16}) = Int16 -_int(::Type{Float32}) = Int32 -_int(::Type{Float64}) = Int64 -_int(::Type{BigFloat}) = BigInt -_int(x) = Int - function _polyder(p::Poly{T}, order::Int=1) where {T} n = length(p) a2 = Vector{T}(undef, n-order) for i = order:n-1 - a2[i-order+1] = p[i] * prod((one(_int(T)) * (i-order+1)):i) + a2[i-order+1] = reduce(*, (i-order+1):i, init=p[i]) end return Poly(a2, p.var)