# Julia sum issue

**URL:** <https://discourse.julialang.org/t/julia-sum-issue/67566>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 2, 2021, 11:16am UTC](https://discourse.julialang.org/t/julia-sum-issue/67566 "2021-09-02T11:16:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [September 2, 2021, 11:16am UTC](https://discourse.julialang.org/t/julia-sum-issue/67566/1 "2021-09-02T11:16:48Z")

</div>

Hi In the below code I am getting wrong summation output

```julia
zz=sum((M_B_DG_400[bb,t,c,p]*C_s[c]) for p in 1:P, t in 1:T, bb in 1:b)

if the value of M_B_DG_400[bb,t,c,p] is zero then value zz should be zero. But it is not showing as zero.
any idea how to correct it.
```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 2, 2021, 11:23am UTC](https://discourse.julialang.org/t/julia-sum-issue/67566/2 "2021-09-02T11:23:45Z")

</div>

Can you produce a [minimum working example](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)?

Are you sure that `M_B_DG_400` is zero everywhere? If it isn’t then the sum wouldn’t be zero of course

```julia
julia> sum(x*y for x ∈ 0:1, y ∈ 2:3) # one non-zero value for x
5

julia> sum(x*y for x ∈ fill(0, 2), y ∈ 2:3) # x is all zeros
0

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 2, 2021, 11:48am UTC](https://discourse.julialang.org/t/julia-sum-issue/67566/3 "2021-09-02T11:48:30Z")

</div>

It’s extremely unlikely that Julia’s `sum` function should have a previously undiscovered bug where it sums all zeros to something non-zero.

I propose that you first check whether  
`all((M_B_DG_400[bb,t,c,p]*C_s[c] == 0) for p in 1:P, t in 1:T, bb in 1:b)`  
is true. If it’s not, you’re summing at least one non-zero element and shouldn’t expect the sum to be zero.

If it was true, then check whether

```julia
Base.sum((M_B_DG_400[bb,t,c,p]*C_s[c]) for p in 1:P, t in 1:T, bb in 1:b) == 0

```

If it is, you must have redefined `sum` and the problem is not in Julia’s `sum` but in your own.

If it still looks like a problem in Julia, provide a way to reproducibly reconstruct the arrays `M_B_DG_400` and `C_s`, either by code or by dumping them to file and making that available.
