Skip to content

Commit

Permalink
Backport Fix for #8871
Browse files Browse the repository at this point in the history
Mapreduce didn't work with `Range{BigInt}`, because the length wasn't a
`Int`, because of potential overflow. This patch just converts the
length to Int with a checked conversion, because a mapreduce on larger
arrays will probably never finish anyway.

Also fix some broken test of prod

(cherry picked from commit 9493465)

Fix test for 32 bit

The test added in #8893 shouldn't use typemax(Int), but typemax(Int64)

(cherry picked from commit 49083be)
  • Loading branch information
ivarne committed Nov 7, 2014
1 parent ba119f9 commit 245b9f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mr_empty(f, op::AndFun, T) = true
mr_empty(f, op::OrFun, T) = false

function _mapreduce{T}(f, op, A::AbstractArray{T})
n = length(A)
n = Int(length(A))
if n == 0
return mr_empty(f, op, T)
elseif n == 1
Expand Down
19 changes: 11 additions & 8 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,19 @@ end

# prod

prod(Int[]) === 0
prod(Int8[]) === 0
prod(Float64[]) === 0.0
@test prod(Int[]) === 1
@test prod(Int8[]) === 1
@test prod(Float64[]) === 1.0

prod([3]) === 0
prod([int8(3)]) === 0
prod([3.0]) === 0.0
@test prod([3]) === 3
@test prod([int8(3)]) === 3
@test prod([3.0]) === 3.0

prod(z) === 120
prod(fz) === 120.0
@test prod(z) === 120
@test prod(fz) === 120.0

@test prod(1:big(16)) == big(20922789888000)
@test prod(big(typemax(Int64)):big(typemax(Int64))+16) == BigInt("25300281663413827620486300433089141956148633919452440329174083959168114253708467653081909888307573358090001734956158476311046124934597861626299416732205795533726326734482449215730132757595422510465791525610410023802664753402501982524443370512346073948799084936298007821432734720004795146875180123558814648586972474376192000")

# check type-stability
prod2(itr) = invoke(prod, (Any,), itr)
Expand Down

0 comments on commit 245b9f6

Please sign in to comment.