Skip to content

Commit

Permalink
close issue 159
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jan 17, 2019
1 parent 12d611f commit 4b2330b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Polynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 4b2330b

Please sign in to comment.