Skip to content

Commit 9fb7bdf

Browse files
committed
Fix bug in extrema
Fix a bug in `extrema`, which previously did not correctly find the maximum value if it was the first in the array.
1 parent f614e0c commit 9fb7bdf

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/NaNMath.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ function extrema{T<:AbstractFloat}(x::AbstractArray{T})
141141
if !isnan(i)
142142
if (isnan(resultmin) || i < resultmin)
143143
resultmin = i
144-
elseif (isnan(resultmax) || i > resultmax)
144+
end
145+
if (isnan(resultmax) || i > resultmax)
145146
resultmax = i
146147
end
147148
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ using Base.Test
1616
@test NaNMath.minimum([1., 2., NaN]) == 1.0
1717
@test NaNMath.minimum([1. 2.; NaN 1.]) == 1.0
1818
@test NaNMath.extrema([1., 2., NaN]) == (1.0, 2.0)
19+
@test NaNMath.extrema([2., 1., NaN]) == (1.0, 2.0)
1920
@test NaNMath.extrema([1. 2.; NaN 1.]) == (1.0, 2.0)
21+
@test NaNMath.extrema([2. 1.; 1. NaN]) == (1.0, 2.0)
22+
@test NaNMath.extrema([NaN, -1., NaN]) == (-1.0, -1.0)
2023
@test NaNMath.mean([1., 2., NaN]) == 1.5
2124
@test NaNMath.mean([1. 2.; NaN 3.]) == 2.0
2225
@test NaNMath.var([1., 2., NaN]) == 0.5

0 commit comments

Comments
 (0)