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

Fix overflow in mean(::AbstractRange) #122

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Manifest.toml
5 changes: 3 additions & 2 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ function _mean(f, A::AbstractArray, dims::Dims=:) where Dims
end

function mean(r::AbstractRange{<:Real})
isempty(r) && return oftype((first(r) + last(r)) / 2, NaN)
(first(r) + last(r)) / 2
centralval = first(r) + (last(r) - first(r)) / 2
isempty(r) && return oftype(centralval, NaN)
centralval
end

median(r::AbstractRange{<:Real}) = mean(r)
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ end
end
@test mean(2:1) === NaN
@test mean(big(2):1) isa BigFloat

@testset "issue #120" begin
@test mean(Int8(123):Int8(123)) == 123
@test median(Int8(123):Int8(123)) == 123
@test mean(Int8(126):Int8(127)) == 126.5
@test mean(typemax(Int):typemax(Int)) == float(typemax(Int))
@test mean(UInt8(255):UInt8(255)) == 255
@test mean(Float16(12345):Float16(54321)) ≈ Float16(mean(Float32(12345):Float32(54321)))
end
end

@testset "var & std" begin
Expand Down