Skip to content

Commit

Permalink
updates docs and add tests for middle on non-reals
Browse files Browse the repository at this point in the history
  • Loading branch information
tlnagy committed Oct 1, 2020
1 parent 97b2777 commit 33533e2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ cor(x::AbstractVecOrMat, y::AbstractVecOrMat; dims::Int=1) =
Compute the middle of a scalar value, which is equivalent to `x` itself, but of the type of `middle(x, x)` for consistency.
"""
middle(x::Union{Bool,Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128}) = Float64(x)
# Specialized functions for real types allow for improved performance
# Specialized functions for number types allow for improved performance
middle(x::AbstractFloat) = x
middle(x::Number) = (x + zero(x)) / 1

"""
middle(x, y)
Compute the middle of two reals `x` and `y`, which is
Compute the middle of two numbers `x` and `y`, which is
equivalent in both value and type to computing their mean (`(x + y) / 2`).
"""
middle(x::Number, y::Number) = x/2 + y/2
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Random.seed!(123)
@test middle(1:8) === 4.5
@test middle([1:8;]) === 4.5

@test middle(5.0 + 2.0im, 2.0 + 3.0im) == 3.5 + 2.5im
@test middle(5.0 + 2.0im) == 5.0 + 2.0im

# 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))
Expand Down

0 comments on commit 33533e2

Please sign in to comment.