# Integer type promotion behavior

**URL:** <https://discourse.julialang.org/t/integer-type-promotion-behavior/111848>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 19, 2024, 10:54pm UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848 "2024-03-19T22:54:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byun6jae](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@byun6jae](https://discourse.julialang.org/u/byun6jae)\
**Post date:** [March 19, 2024, 10:54pm UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848/1 "2024-03-19T22:54:42Z")

</div>

I just started learning Julia. I know a bit of Python and R.

I have a (probably) simple question about integer type promotion behavior of Julia.

julia\> versioninfo()  
Julia Version 1.10.1  
Commit 7790d6f064 (2024-02-13 20:41 UTC)  
Build Info:  
Official [https://julialang.org/](https://julialang.org/) release  
Platform Info:  
OS: Windows (x86\_64-w64-mingw32)  
CPU: 20 × 12th Gen Intel(R) Core™ i7-12700K  
WORD\_SIZE: 64  
LIBM: libopenlibm  
LLVM: libLLVM-15.0.7 (ORCJIT, alderlake)  
Threads: 1 default, 0 interactive, 1 GC (on 20 virtual cores)

julia\> 2^62 # This works  
4611686018427387904

julia\> 2^63 # But this results in an overflow  
-9223372036854775808

julia\> Int128(2)^Int128(63) # This solves the problem  
9223372036854775808

julia\> 2^Int128(63) # But, this doesn’t  
-9223372036854775808

julia\> 2^big(63) # But, this works, which is strange  
9223372036854775808

Is this an expected behavior?  
Could anyone give me a link to a related documentation?

Thanks.  
byun6jae

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [March 20, 2024, 1:58am UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848/2 "2024-03-20T01:58:37Z")

</div>

Looking at the code, it looks deliberate.

The integer promotion in `power_by_squaring()` promotes the integer type using `to_power_type()`, but this _only_ takes the base into account.

`to_power_type()` in turn promotes the type to the output of the base times itself.

I imagine the intent here was ensure the output type of, for example, `Int32(2)^4` behaved as if it were written as `Int32(2) * Int32(2) * Int32(2) * Int32(2)`, irrespective of the integer type of the power. However, I think a case could be made to first apply standard integer promotion rules to both the base and power, before passing anything to `to_power_type()`.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [March 20, 2024, 2:02am UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848/3 "2024-03-20T02:02:42Z")

</div>

Note that for exponentiation where base and power aren’t bother integer types, standard type promotion rules apply.

e.g. `2^Float64(2)::Float64` and `Float32(2)^2::Float32`

---

<div class="post-metadata">

**Author:** ![byun6jae](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@byun6jae](https://discourse.julialang.org/u/byun6jae)\
**Post date:** [March 20, 2024, 4:29am UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848/4 "2024-03-20T04:29:08Z")

</div>

So, I guess the proper way of ensuring the integer type is big enough to hold the result is `Int128(2)^63` instead of `2^Int128(63)`.

And, probably `2^big(63)` works for the similar reason why `2^Float64(63)` works  
(i.e., BigInt is more than just another integer type)?

Thank you very much for your reply!

byun6jae

---

<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:** [March 20, 2024, 6:25am UTC](https://discourse.julialang.org/t/integer-type-promotion-behavior/111848/5 "2024-03-20T06:25:21Z")

</div>

I think the bigint case is possibly a bug, but I’m not 100% sure. the reason we have the behavior we do is because things like `Int8(-1)^UInt32(3)` seem like they should work
