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

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

Closed
wants to merge 1 commit into from

Conversation

vyu
Copy link
Contributor

@vyu vyu commented Jun 14, 2022

Currently, mean(::AbstractRange) can overflow:

julia> using Statistics

julia> mean(typemax(Int):typemax(Int))
-1.0

julia> mean(prevfloat(Inf):prevfloat(Inf))
Inf

This PR prevents this overflow.

In addition, I've relaxed the type constraint from AbstractRange{<:Real} to AbstractRange{<:Any}, since the computation here is correct for any type that implements addition and numerical division (unlike the median, which requires an order). Currently, the mean of non-real ranges falls back to mean(::AbstractVector), which also requires addition and numerical division.

In line with this, oftype([...], NaN) is changed to zero(T)/0 so that mean(LinRange(2im, 1im, 0)) is NaN + NaN*im (consistent with mean(ComplexF64[])) instead of NaN + 0.0im.

(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.

@ararslan
Copy link
Member

Thanks for the PR! Great first contribution to the package. 🙂

@nalimilan
Copy link
Member

Do you want to finish this @vyu?

@codecov-commenter
Copy link

Codecov Report

Base: 96.93% // Head: 96.93% // No change to project coverage 👍

Coverage data is based on head (6c66b96) compared to base (576db0f).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #115   +/-   ##
=======================================
  Coverage   96.93%   96.93%           
=======================================
  Files           1        1           
  Lines         424      424           
=======================================
  Hits          411      411           
  Misses         13       13           
Impacted Files Coverage Δ
src/Statistics.jl 96.93% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Jay-sanjay
Copy link

Hello , what is the status of this issue is it solved............
I would be happy to work for it

@nalimilan
Copy link
Member

Thanks for proposing your help. I think you can start from the current state of the PR and the last round of comments. You probably won't be able to push to this branch but you can start a new PR and post a link here.

@nalimilan
Copy link
Member

Closing in favor of #150.

@nalimilan nalimilan closed this Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants