# Integer power of large Integer returning very wrong results

**URL:** <https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747>\
**Category:** General Usage\
**Tags:** base\
**Created:** [May 12, 2023, 3:06pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747 "2023-05-12T15:06:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [May 12, 2023, 3:06pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/1 "2023-05-12T15:06:01Z")

</div>

I am with Julia 1.9 on a fresh session (windows). (Maybe it happens on previous version already)  
Is it normal to get stuff like

```julia
julia> 5^2000
-7794321243379011391

julia> 2^200
0

```

With floating power `5^2000.`, it is `Inf`.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [May 12, 2023, 3:07pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/2 "2023-05-12T15:07:02Z")

</div>

That is just integer overflow. Normal julia Ints aren’t BigInts.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [May 12, 2023, 3:07pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/3 "2023-05-12T15:07:41Z")

</div>

Why not `Inf` everywhere?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [May 12, 2023, 3:09pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/4 "2023-05-12T15:09:39Z")

</div>

Inf is a floating point thing. One could use checked integer arithmetic that would throw an exception on overflow like [GitHub - JeffreySarnoff/SaferIntegers.jl: These integer types use checked arithmetic, otherwise they are as system types.](https://github.com/JeffreySarnoff/SaferIntegers.jl). But they are slow, so julia doesn’t do that by default

---

<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:** [May 12, 2023, 3:11pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/5 "2023-05-12T15:11:55Z")

</div>

For the second time this week I can dig out this classic thread:

> [@Discussion about integer overflow](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627):
>
> Yes, it is a limitation but I would not call it big since we could use BigInt if Int64 is not enough and an overflow is something we can catch. I find more worrying this: \> julia\> 1 / 10^20000 \> Inf I’d rather have an overflow here than Inf, or at least an Infinitesimal value… Maybe something we could ask for future versions of Julia.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 12, 2023, 3:40pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/6 "2023-05-12T15:40:51Z")

</div>

And I’ll plug OverflowContexts.jl ([GitHub - BioTurboNick/OverflowContexts.jl: Fine control of integer overflow checking.](https://github.com/BioTurboNick/OverflowContexts.jl)) if you’d like control over it.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [May 12, 2023, 10:16pm UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/7 "2023-05-12T22:16:05Z")

</div>

> Why not `Inf` everywhere?

Because Inf is **NOT** a part of Int64

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [May 15, 2023, 10:49am UTC](https://discourse.julialang.org/t/integer-power-of-large-integer-returning-very-wrong-results/98747/8 "2023-05-15T10:49:46Z")

</div>

Thanks for all your answers, I missed these link in my pre-question search.  
Plus I missed [Julia docs on that](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Overflow-behavior).

I missed the “overflow” keyword.
