# Large sum returns negative numbers in julia

**URL:** <https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032>\
**Category:** General Usage\
**Tags:** integer-overflow\
**Created:** [October 26, 2020, 10:12am UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032 "2020-10-26T10:12:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dashanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dashanan/32/18909_2.png) [@dashanan](https://discourse.julialang.org/u/dashanan)\
**Post date:** [October 26, 2020, 10:12am UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/1 "2020-10-26T10:12:16Z")

</div>

hello ,  
i am trying to do a counter for some problem I am having, but while I was testing I see that Julia was returning negative numbers after some time,  
I have no idea why this is happening,

now to check that, i have added print with condition that 'weird limit has hit,  
is this a bug ,

adding the counter code  
‘’‘function counter(x)  
y = 0  
for i in 0:x  
y=y+x  
if y\<0  
print(i)  
return “weird limit hit”  
#rint(i)  
end  
end  
print(y)  
end  
‘’’  
execution @time counter(10000000000)  
outputs of weird limits  
922337203 0.000041 seconds (17 allocations: 368 bytes)  
weird limit hit"

execution @time counter(10000000000000)  
922337 0.000040 seconds (17 allocations: 368 bytes)

Out[16]:

“weird limit hit”

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 26, 2020, 10:16am UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/2 "2020-10-26T10:16:42Z")

</div>

This is integer overflow. Once the maximum integer number is hit, it will jump to the smallest:

```julia
julia> typemax(Int)
9223372036854775807

julia> typemax(Int)+1
-9223372036854775808

```

You can avoid this by using floats or `BigInt`.

There have been quite a few posts on this on this forum, search them for more details.

PS: please quote your code, see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 26, 2020, 10:18am UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/3 "2020-10-26T10:18:14Z")

</div>

Also see  
[https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic)

---

<div class="post-metadata">

**Author:** ![dashanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dashanan/32/18909_2.png) [@dashanan](https://discourse.julialang.org/u/dashanan)\
**Post date:** [October 26, 2020, 1:50pm UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/4 "2020-10-26T13:50:04Z")

</div>

thanks for the info , i will try with bigint and thanks for teh psa one as well

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 26, 2020, 2:27pm UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/5 "2020-10-26T14:27:55Z")

</div>

Before going to `BigInt` you could try `Int128`, which are _much_ faster, and see if they are enough.

---

<div class="post-metadata">

**Author:** ![dashanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dashanan/32/18909_2.png) [@dashanan](https://discourse.julialang.org/u/dashanan)\
**Post date:** [October 26, 2020, 6:55pm UTC](https://discourse.julialang.org/t/large-sum-returns-negative-numbers-in-julia/49032/6 "2020-10-26T18:55:22Z")

</div>

thanks for teh advise nd thanks for this this had really solved my problem
