Skip to content

Commit 4b2330b

Browse files
committed
close issue 159
1 parent 12d611f commit 4b2330b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Polynomials.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,20 @@ function polyder(p::Poly{T}, order::Int=1) where {T}
560560
_polyder(p, order)
561561
end
562562

563+
# _int(T) returns matching Integer type to T
564+
# to avoid overflow in `prod` usage below
565+
_int(::Type{T}) where {T <: Union{Integer, Int8, Int16, Int64, Int128, BigInt}} = T
566+
_int(::Type{Float16}) = Int16
567+
_int(::Type{Float32}) = Int32
568+
_int(::Type{Float64}) = Int64
569+
_int(::Type{BigFloat}) = BigInt
570+
_int(x) = Int
571+
563572
function _polyder(p::Poly{T}, order::Int=1) where {T}
564573
n = length(p)
565574
a2 = Vector{T}(undef, n-order)
566575
for i = order:n-1
567-
a2[i-order+1] = p[i] * prod((i-order+1):i)
576+
a2[i-order+1] = p[i] * prod((one(_int(T)) * (i-order+1)):i)
568577
end
569578

570579
return Poly(a2, p.var)

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,5 @@ xx = Real[20.0, 30.0, 40.0]
391391
yy = Real[15.7696, 21.4851, 28.2463]
392392
polyfit(xx,yy,2)
393393

394+
## Issue with overflow and polyder Issue #159
395+
@test !iszero(polyder(Poly(BigInt[0, 1])^100, 100))

0 commit comments

Comments
 (0)