Skip to content

Commit 3ef866b

Browse files
committed
fix min/maximum for inputs containing both missing and NaN
1 parent ffed6b8 commit 3ef866b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

base/reduce.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,13 @@ isgoodzero(::typeof(min), x) = isbadzero(max, x)
627627

628628
function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
629629
A::AbstractArrayOrBroadcasted, first::Int, last::Int)
630+
# 1. This optimization gives different result from general fallback, if the inputs `f.(A)`
631+
# contains both 'missing' and 'Nan'.
632+
# 2. For Integer cases, general fallback seems faster.
633+
# Based the above reasons, only use this for AbstractFloat cases.
634+
Eltype = _return_type(i -> f(A[i]), Tuple{Int})
635+
Eltype <: AbstractFloat ||
636+
return invoke(mapreduce_impl,Tuple{Any,Any,AbstractArrayOrBroadcasted,Int,Int},f,op,A,first,last)
630637
a1 = @inbounds A[first]
631638
v1 = mapreduce_first(f, op, a1)
632639
v2 = v3 = v4 = v1
@@ -635,10 +642,10 @@ function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
635642
simdstop = start + chunk_len - 4
636643
while simdstop <= last - 3
637644
# short circuit in case of NaN or missing
638-
(v1 == v1) === true || return v1
639-
(v2 == v2) === true || return v2
640-
(v3 == v3) === true || return v3
641-
(v4 == v4) === true || return v4
645+
v1 == v1 || return v1
646+
v2 == v2 || return v2
647+
v3 == v3 || return v3
648+
v4 == v4 || return v4
642649
@inbounds for i in start:4:simdstop
643650
v1 = _fast(op, v1, f(A[i+0]))
644651
v2 = _fast(op, v2, f(A[i+1]))

test/reduce.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
388388
@test maximum(x) === minimum(x) === missing
389389
@test extrema(x) === (missing, missing)
390390
end
391+
# inputs containing both missing and NaN
392+
minimum([NaN;zeros(255);missing]) === missing
393+
maximum([NaN;zeros(255);missing]) === missing
391394
end
392395

393396
# findmin, findmax, argmin, argmax

0 commit comments

Comments
 (0)