Skip to content

Commit 07b6cac

Browse files
committed
Support missing better in minimum(A; dims)
convert(T, Inf) fails for lots of T (e.g. Int).
1 parent 253a393 commit 07b6cac

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

base/reducedim.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function _reducedim_init(f, op, fv, fop, A, region)
125125
end
126126

127127
# initialization when computing minima and maxima requires a little care
128-
for (f1, f2, initval) in ((:min, :max, :Inf), (:max, :min, :(-Inf)))
128+
for (f1, f2, typeextreme) in ((:min, :max, :typemax), (:max, :min, :typemin))
129129
@eval function reducedim_init(f, op::typeof($f1), A::AbstractArray, region)
130130
# First compute the reduce indices. This will throw an ArgumentError
131131
# if any region is invalid
@@ -145,14 +145,18 @@ for (f1, f2, initval) in ((:min, :max, :Inf), (:max, :min, :(-Inf)))
145145
v0 = mapreduce(f, $f2, A1)
146146

147147
T = _realtype(f, promote_union(eltype(A)))
148+
Tr = v0 isa T ? T : typeof(v0)
148149

149150
# but NaNs and missing need to be avoided as initial values
150151
if is_poisoning(v0)
151-
# Convert handles unions containing Missing nicely.
152-
v0 = convert(T, $initval)
152+
Tnm = nonmissingtype(Tr)
153+
# TODO: Some types, like BigInt, don't support typemin/typemax.
154+
# So a Matrix{Union{BigInt, Missing}} can still error here.
155+
v0 = $typeextreme(Tnm)
153156
end
154-
157+
# v0 may have changed type.
155158
Tr = v0 isa T ? T : typeof(v0)
159+
156160
return reducedim_initarray(A, region, v0, Tr)
157161
end
158162
end

0 commit comments

Comments
 (0)