The difference is small, but I can’t figure out, why there is any difference and where this uncertainty comes from.
The following code creates a Vector of random Float64 and puts randomly NaN into it. It calculates the sum over all non-NaN entries using an indexed approach and a view over the same indices into the same Vector. The two values differ and I can’t find the reason:
function view_vs_index()
nrows=10000
a=rand(nrows)
nas=1000
naIndices=rand(1:nrows,nas)
a[naIndices].=NaN
goodIndices=.!isnan.(a)
v=view(a,goodIndices)
suma=sum(a[goodIndices])
sumv=sum(v)
return suma,sumv,suma==sumv
end
view_vs_index()
julia> view_vs_index()
(4458.65184926987, 4458.651849269862, false)
I thought about general Float precision but without deeper explanation this is unsatisfactory, but I can’t find it as e.g. the order of summing up the elements should be equal, isn’t it?
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: AMD Ryzen 9 3900X 12-Core Processor
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, znver2)