# Difference between sum over indexed array or equivalent view on the array

**URL:** <https://discourse.julialang.org/t/difference-between-sum-over-indexed-array-or-equivalent-view-on-the-array/59197>\
**Category:** Numerics\
**Created:** [April 13, 2021, 1:43pm UTC](https://discourse.julialang.org/t/difference-between-sum-over-indexed-array-or-equivalent-view-on-the-array/59197 "2021-04-13T13:43:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 13, 2021, 1:43pm UTC](https://discourse.julialang.org/t/difference-between-sum-over-indexed-array-or-equivalent-view-on-the-array/59197/1 "2021-04-13T13:43:25Z")

</div>

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:

```julia
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
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
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)

```
