diff --git a/src/Statistics.jl b/src/Statistics.jl index 23eeeb60..ed892ece 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 3434bb76..3fd56eab 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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))