Skip to content

Commit f7f330d

Browse files
authored
Merge pull request #16 from mkborregaard/add-extrema
Add `extrema`
2 parents cbd6063 + c2c0ee3 commit f7f330d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/NaNMath.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,35 @@ function minimum{T<:AbstractFloat}(x::Vector{T})
120120
return result
121121
end
122122

123+
"""
124+
NaNMath.extrema(A)
125+
126+
##### Args:
127+
* `A`: A one dimensional array of floating point numbers
128+
129+
##### Returns:
130+
* Returns the minimum and maximum of all elements in the array, ignoring NaN's.
131+
132+
##### Examples:
133+
```julia
134+
using NaNMath as nm
135+
nm.extrema([1., 2., NaN]) # result: 1.0, 2.0
136+
```
137+
"""
138+
function extrema{T<:AbstractFloat}(x::Vector{T})
139+
resultmin, resultmax = convert(eltype(x), NaN), convert(eltype(x), NaN)
140+
for i in x
141+
if !isnan(i)
142+
if (isnan(resultmin) || i < resultmin)
143+
resultmin = i
144+
elseif (isnan(resultmax) || i > resultmax)
145+
resultmax = i
146+
end
147+
end
148+
end
149+
return resultmin, resultmax
150+
end
151+
123152
"""
124153
NaNMath.mean(A)
125154

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using Base.Test
1212
@test NaNMath.sum([1f0, 2f0, NaN32]) === 3.0f0
1313
@test NaNMath.maximum([1., 2., NaN]) == 2.0
1414
@test NaNMath.minimum([1., 2., NaN]) == 1.0
15+
@test NaNMath.extrema([1., 2., NaN]) == (1.0, 2.0)
1516
@test NaNMath.mean([1., 2., NaN]) == 1.5
1617
@test NaNMath.var([1., 2., NaN]) == 0.5
1718
@test NaNMath.std([1., 2., NaN]) == 0.7071067811865476

0 commit comments

Comments
 (0)