# Integer overflow in cumprod (cumulative product) output

**URL:** https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037
**Category:** New to Julia
**Tags:** numbers, integer-overflow
**Created:** [January 6, 2020, 6:58pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037 "2020-01-06T18:58:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [January 6, 2020, 6:58pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037/1 "2020-01-06T18:58:03Z")

</div>

The cumulative product vector of a series of linear positive numbers (show in plot(cump1) ) contains negative num and zeros(in plot(cump2)). I checked available documentations carefully and still was in dark.

```julia
twoNmu=1;ns=Int(1e3)
 cump1=Int64[]
 for i in 0:(ns-1)
  push!(cump1,twoNmu+i)
 end
 #cumpt1=hcat(cump1)
 cump2=cumprod(cump1)#default as one
 plot(cump1)
 plot(cump2)

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 6, 2020, 7:04pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037/2 "2020-01-06T19:04:02Z")

</div>

This is integer overflow. If you want to work with huge integers without overflow, use `BigInt`. (Or use floating-point calculations.)

See e.g. [the manual on overflow](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/index.html#Overflow-behavior-1) or [this discussion](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773/2) or [other similar discussions](https://discourse.julialang.org/search?q=integer%20overflow).

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [January 6, 2020, 7:34pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037/3 "2020-01-06T19:34:31Z")

</div>

Thanks, I made a trial according to big() (updated to BigInt()?) suggested among posts, but it still doesn’t work.

```julia
twoNmu=1;ns=Int(1e3)
 cump1=Int64[]
 cump2=Array{BigInt}(undef,1000,1) #overwritten to Int64 later at "cump2=..."#

 for i in 0:(ns-1)
  push!(cump1,twoNmu+i)
 end
 println(size(cump1),size(cump2))
 cump1=hcat(cump1)
 cump2=BigInt.(cumprod(cump1,dims=1)) #doesn't work
 plot(cump2)

```

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [January 6, 2020, 10:37pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037/4 "2020-01-06T22:37:19Z")

</div>

You have to convert to `BigInt` _before_ the overflow happens. E.g., `cump1 = BigInt[]`.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [January 6, 2020, 10:45pm UTC](https://discourse.julialang.org/t/integer-overflow-in-cumprod-cumulative-product-output/33037/5 "2020-01-06T22:45:14Z")

</div>

> [@1634](#):
>
> cump2=BigInt.(cumprod(cump1,dims=1))

this is like `float(Int(floor(3.14)))`, you can’t recover what is already lost, so you have to compute the `cumprod` with big integer to begin with.
