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 incorrectly computes means of ranges #120

Open
yurivish opened this issue Jun 29, 2022 · 1 comment
Open

mean incorrectly computes means of ranges #120

yurivish opened this issue Jun 29, 2022 · 1 comment

Comments

@yurivish
Copy link

For example, it incorrectly computes the mean of the single-element range containing the number 123 as -5 if the element type is Int8:

julia> using Statistics

julia> mean(Int8(123):Int8(123))
-5.0

As another example, the mean of the range 126:127 is computed as -1.5 rather than the true mean, which is 126.5:

julia> mean(Int8(126):Int8(127))
-1.5

Because median delegates to mean, the median is also wrong:

julia> median(Int8(123):Int8(123))
-5.0

This is due to a “performance-optimized” mean implementation:

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

The code mishandles integer overflow, affecting all standard integer types (Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, and UInt64):

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

julia> mean(UInt8(255):UInt8(255))
127.0

Since it also mishandles floating-point overflow, this affects all standard float types (Float16, Float32, and Float64):

julia> mean(Float16(12345):Float16(54321))
Inf16

The mean is computed incorrectly for 25% of all signed integer ranges and 50% of all unsigned integer ranges.

@jishnub
Copy link

jishnub commented Jun 30, 2022

This should be handled by #115 I think

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 a pull request may close this issue.

2 participants