Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,23 @@ prod(a) = mapreduce(identity, mul_prod, a)

## maximum & minimum

# these propagate NaN correctly only in the second argument
_max_fast(x, y) = ifelse(x > y, x, y)
_min_fast(x, y) = ifelse(x < y, x, y)

_fast(::typeof(min)) = _min_fast
_fast(::typeof(max)) = _max_fast

function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
A::AbstractArray, first::Int, last::Int)
# locate the first non NaN number
op_fast = _fast(op)
@inbounds a1 = A[first]
v = mapreduce_first(f, op, a1)
i = first + 1
while (v == v) && (i <= last)
for i in (first+1):last
# short circuit NaN correctly
(v == v) || return v
@inbounds ai = A[i]
v = op(v, f(ai))
i += 1
v = op_fast(v, f(ai))
end
v
end
Expand Down
2 changes: 2 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
@test isequal(extrema([NaN]), (NaN, NaN))

@test isnan(maximum([NaN, 2.]))
@test isnan(maximum([2., NaN]))
@test isnan(minimum([NaN, 2.]))
@test isnan(minimum([2., NaN]))
@test isequal(extrema([NaN, 2.]), (NaN,NaN))

@test isnan(maximum([NaN, 2., 3.]))
Expand Down