Skip to content

Accuracy issue with summation of @viewΒ #52457

Open
@sjkelly

Description

@sjkelly

This was originally noticed in:
#28848 (comment)

I don't know if this is somewhat related to this issue, but I also found an accuracy issue which I think is more troublesome because it also affects DataFrames.jl

here is the MVP

using Distributions
using Random

# It doesn't happen always, here is a seed where this happens.
rng = MersenneTwister(630);
v = rand(rng, Normal(zero(Float32), one(Float32)), 1000)
sa = @view v[collect(1:end)]

# View (as SubArray) vs Vector
sum(sa) β‰ˆ sum(v) # false
# They are different! and worse part is that the view version is less accurate! (according to Kahan compensated summation)

IndexStyle(v) isa IndexLinear # true
IndexStyle(sa) isa IndexCartesian #true

# They are dispatched to different implementations in base/reduce.jl > _mapreduce 

Another MWE is:

julia> using Statistics, Random

julia> Random.seed!(1);

julia> x = rand(Float32, 10^6);

julia> mean(x)
0.49974534f0

julia> mean(@view x[:])
0.49974534f0

julia> mean(@view x[collect(1:end)])
0.49974778f0

@mbauman points out that the latter in this MWE hits the naive iterator: foldl(+, x)/length(x) which leads to the poor floating point accuracy.

cc @el-oso

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]foldsum, maximum, reduce, foldl, etc.mathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions