Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relax type definition of middle #28

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -733,17 +733,17 @@ 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::Real) = (x + zero(x)) / 1
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::Real, y::Real) = x/2 + y/2
middle(x::Number, y::Number) = x/2 + y/2
tlnagy marked this conversation as resolved.
Show resolved Hide resolved

"""
middle(range)
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