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

mean() can easily overflow for general iterables #25739

Closed
nalimilan opened this issue Jan 25, 2018 · 1 comment
Closed

mean() can easily overflow for general iterables #25739

nalimilan opened this issue Jan 25, 2018 · 1 comment
Labels
maths Mathematical functions

Comments

@nalimilan
Copy link
Member

Contrary to the mean(::AbstractArray) method, the mean fallback for general iterables computes the sum without promoting the element type, meaning that overflow can easily happen:

julia> mean([typemax(Int8), typemax(Int8)])
127.0

julia> mean(x for x in [typemax(Int8), typemax(Int8)])
-1.0

Apart from being inconvenient/risky, it's inconsistent with sum:

julia> sum([typemax(Int8), typemax(Int8)])
254

julia> sum(x for x in [typemax(Int8), typemax(Int8)])
254

I think we should use promote_sys_size_add added by #22825 to choose the accumulation type, just like sum.

@JeffreySarnoff
Copy link
Contributor

makes sense to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

3 participants