Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 20, 2014
2 parents 3d460c3 + 05ebd55 commit 943e8d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ end

##### median & quantiles #####

middle(x::Real) = float(x)
middle(x::Union(Bool,Int8,Int16,Int32,Int64,Int128,Uint8,Uint16,Uint32,Uint64,Uint128)) = float64(x)
middle(x::FloatingPoint) = x
middle(x::Float16) = float32(x)
middle(x::Real) = (x + zero(x)) / 1
middle(x::Real, y::Real) = (x + y) / 2
middle(a::Range) = middle(a[1], a[end])
middle(a::AbstractArray) = ((v1, v2) = extrema(a); middle(v1, v2))
Expand Down
14 changes: 14 additions & 0 deletions test/statistics.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# middle

@test middle(3) === 3.0
@test middle(2, 3) === 2.5
@test middle(1:8) === 4.5
@test middle([1:8]) === 4.5

# ensure type-correctness
for T in [Bool,Int8,Int16,Int32,Int64,Int128,Uint8,Uint16,Uint32,Uint64,Uint128,Float16,Float32,Float64]
@test middle(one(T)) === middle(one(T), one(T))
end


# median
@test median([1.]) === 1.
@test median([1.,3]) === 2.
@test median([1.,3,2]) === 2.
Expand Down

0 comments on commit 943e8d3

Please sign in to comment.