Skip to content

Prevent overflow in mean(::AbstractRange) and relax type constraint #115

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

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
12 changes: 6 additions & 6 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ function _mean(f, A::AbstractArray, dims::Dims=:) where Dims
end
end

function mean(r::AbstractRange{<:Real})
isempty(r) && return oftype((first(r) + last(r)) / 2, NaN)
(first(r) + last(r)) / 2
function mean(r::AbstractRange{T}) where T
isempty(r) && return zero(T)/0
return first(r)/2 + last(r)/2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return first(r)/2 + last(r)/2
return middle(r)

This method is already defined appropriately for ranges and behaves as desired when the input is non-empty:

julia> middle(typemax(Int):typemax(Int))  typemax(Int)
true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be less performant to call middle because it currently doesn't elide bounds checks and a[end] is unfortunately slow for StepRange (it calls lastindex, which calls length, which requires a division). In contrast, first and last simply accesses fields of the range struct (except for StepRangeLen).

I've just opened a PR (#116) to have middle call mean instead. Would you mind taking a look?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that both mean here and middle at #116 check that the input is not empty, better not have one call the other and instead duplicate the (very short) computation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds good to me. I'll update #116 to not call mean.

end

median(r::AbstractRange{<:Real}) = mean(r)
Expand Down Expand Up @@ -997,9 +997,9 @@ end
require_one_based_indexing(v)

n = length(v)

@assert n > 0 # this case should never happen here

m = alpha + p * (one(alpha) - alpha - beta)
aleph = n*p + oftype(p, m)
j = clamp(trunc(Int, aleph), 1, n-1)
Expand All @@ -1012,7 +1012,7 @@ end
a = v[j]
b = v[j + 1]
end

if isfinite(a) && isfinite(b)
return a + γ*(b-a)
else
Expand Down
20 changes: 14 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,16 @@ end
@test f(2:0.1:n) ≈ f([2:0.1:n;])
end
end
@test mean(2:1) === NaN
@test mean(big(2):1) isa BigFloat
@test mean(2:0.1:4) === 3.0 # N.B. mean([2:0.1:4;]) != 3
@test mean(LinRange(2im, 4im, 21)) === 3.0im
@test mean(2:1//10:4) === 3//1
@test isnan(@inferred(mean(2:1))::Float64)
@test isnan(@inferred(mean(big(2):1))::BigFloat)
z = @inferred(mean(LinRange(2im, 1im, 0)))::ComplexF64
@test isnan(real(z)) & isnan(imag(z))
@test_throws DivideError mean(2//1:1)
@test mean(typemax(Int):typemax(Int)) === float(typemax(Int))
@test mean(prevfloat(Inf):prevfloat(Inf)) === prevfloat(Inf)
end

@testset "var & std" begin
Expand Down Expand Up @@ -547,16 +555,16 @@ end
@test cor(tmp, tmp) <= 1.0
@test cor(tmp, tmp2) <= 1.0
end

@test cor(Int[]) === 1.0
@test cor([im]) === 1.0 + 0.0im
@test_throws MethodError cor([])
@test_throws MethodError cor(Any[1.0])

@test cor([1, missing]) === 1.0
@test ismissing(cor([missing]))
@test_throws MethodError cor(Any[1.0, missing])

@test Statistics.corm([true], 1.0) === 1.0
@test_throws MethodError Statistics.corm(Any[0.0, 1.0], 0.5)
@test Statistics.corzm([true]) === 1.0
Expand Down Expand Up @@ -958,4 +966,4 @@ end
@test isequal(cor(mx, Int[]), fill(NaN, 2, 1))
@test isequal(cov(Int[], my), fill(-0.0, 1, 3))
@test isequal(cor(Int[], my), fill(NaN, 1, 3))
end
end