# Integer overflow

**URL:** <https://discourse.julialang.org/t/integer-overflow/99230>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 22, 2023, 3:28pm UTC](https://discourse.julialang.org/t/integer-overflow/99230 "2023-05-22T15:28:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 22, 2023, 3:28pm UTC](https://discourse.julialang.org/t/integer-overflow/99230/1 "2023-05-22T15:28:20Z")

</div>

How to avoid issues like integer overflow in models based on differential equations with some states values ranging from 10^12 to 10^-12 ?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 22, 2023, 3:34pm UTC](https://discourse.julialang.org/t/integer-overflow/99230/2 "2023-05-22T15:34:13Z")

</div>

use floating point numbers instead of integers. E.G. `1e12` (or `10.0^12`)

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 22, 2023, 4:38pm UTC](https://discourse.julialang.org/t/integer-overflow/99230/3 "2023-05-22T16:38:22Z")

</div>

Perhaps this?

```julia
help?> Base.Checked
  Checked

  The Checked module provides arithmetic functions for the built-in
  signed and unsigned Integer types which throw an error when an
  overflow occurs. They are named like checked_sub, checked_div, etc.
  In addition, add_with_overflow, sub_with_overflow,
  mul_with_overflow return both the unchecked results and a boolean
  value denoting the presence of an overflow.

```

Also discussed [here](https://discourse.julialang.org/t/the-performance-of-saturating-operations-or-adding-intrinsics/48575).

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 22, 2023, 4:59pm UTC](https://discourse.julialang.org/t/integer-overflow/99230/4 "2023-05-22T16:59:14Z")

</div>

I think the simplest solution in this case is to use floating point numbers, as mentioned in the first reply. Although I don’t know the details, I guess that `Float64` is used throughout the computation anyway. Checked arithmetic for integers introduces more complication than necessary.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 22, 2023, 5:35pm UTC](https://discourse.julialang.org/t/integer-overflow/99230/5 "2023-05-22T17:35:57Z")

</div>

For values up to `10^12` an `Int64` is fine, `10^12` uses roughly 40 bits, and `Int64` has 64 bits. However, does not make any sense to worry about integer overflow if your values are not integers, and `10^-12` most certainly is not an integer, you can only be using `Float64` here anyway.
