# Big exponentiation

**URL:** <https://discourse.julialang.org/t/big-exponentiation/11349>\
**Category:** New to Julia\
**Created:** [June 1, 2018, 8:48pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349 "2018-06-01T20:48:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![newcc4](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@newcc4](https://discourse.julialang.org/u/newcc4)\
**Post date:** [June 1, 2018, 8:48pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/1 "2018-06-01T20:48:01Z")

</div>

Hi all.  
I wanted to do something simple - calculate 2^122.

By default Julia returns 0 because of an overflow, so I decided to try using BigInt.

Intuitive solution - BigInt(2^122). Still returns 0 because I’m casting the BigInt after the calculation happened inside. It’s equivalent to x = 2^122; BigInt(x).

So after fiddling I found a solution.

2^(BigInt(122)) returns the correct answer 5316911983139663491615228241121378304

But I have no idea why it works.  
Can someone explain to me?  
And I wonder if using BigInts will always be troublesome like this…

Thanks!

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [June 1, 2018, 9:05pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/2 "2018-06-01T21:05:23Z")

</div>

You could instead convert the base to BigInt: `BigInt(2)^122`

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [June 1, 2018, 9:27pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/4 "2018-06-01T21:27:23Z")

</div>

> [@newcc4](#):
>
> 2^(BigInt(122)) returns the correct answer 5316911983139663491615228241121378304
> 
> But I have no idea why it works.  
> Can someone explain to me?

The usual logic when a mathematical operation receives two values of different types is to first promote them to a common type then perform the operation, e.g. `1 + 1.0` will first promote to `1.0 + 1.0`, then do floating point addition. The logic here is the same: `2^BigInt(122)` gives the same result as `BigInt(2)^BigInt(122)`, which has a `BigInt`-valued result.

(the actual methods which get called in this case are slightly different, since GMP doesn’t actually provide a method to take a `BigInt` to the power of a `BigInt`, but it gives the same result as if it did).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 1, 2018, 9:43pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/5 "2018-06-01T21:43:07Z")

</div>

Right — slightly more concretely, you can see that `2^BigInt(122)` and `2^122` _dispatch_ to different implementations:

```julia
julia> @which 2^122
^(x::T, p::T) where T<:Integer in Base at intfuncs.jl:220

julia> @which 2^big(122)
^(x::Integer, y::BigInt) in Base.GMP at gmp.jl:527

```

This is multiple dispatch in action. Julia picks which method to use based upon the types of the arguments you pass to functions. Now, the reason why we’ve chosen to give these different methods these behaviors is a different answer. 🙂

---

<div class="post-metadata">

**Author:** ![newcc4](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@newcc4](https://discourse.julialang.org/u/newcc4)\
**Post date:** [June 1, 2018, 9:43pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/6 "2018-06-01T21:43:20Z")

</div>

Thanks! It’s clear to me now.  
Could be worth adding this as an example on the BigInt docs.  
[https://docs.julialang.org/en/stable/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic-1](https://docs.julialang.org/en/stable/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic-1)

Or maybe it’s something too simple and I was the only one confused? 😛

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 1, 2018, 11:16pm UTC](https://discourse.julialang.org/t/big-exponentiation/11349/7 "2018-06-01T23:16:00Z")

</div>

You are certainly not the first or last person to have found this confusing, and this could be an excellent opportunity for a pull request. Documentation improvements are generally well-received 🙂
