# Negative integer exponent

**URL:** https://discourse.julialang.org/t/negative-integer-exponent/120848
**Category:** General Usage
**Tags:** question
**Created:** [October 3, 2024, 9:40am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848 "2024-10-03T09:40:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)
#### Post date: [October 3, 2024, 9:40am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848/1 "2024-10-03T09:40:53Z")

</div>

There is something strange that I do not understand:

```julia
julia> 2^-3
0.125

```

works ok. But the following minor generalization does not.

```julia
julia> g() = -3; 2^g()
ERROR: DomainError with -3:
Cannot raise an integer x to a negative power -3.
Make x or -3 a float by adding a zero decimal (e.g., 2.0^-3 or 2^-3.0 instead of 2^-3)or write 1/x^3, float(x)^-3, x^float(-3) or (x//1)^-3.
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, p::Int64)
   @ Base ./intfuncs.jl:265
 [2] power_by_squaring(x_::Int64, p::Int64)
   @ Base ./intfuncs.jl:286
 [3] ^(x::Int64, p::Int64)
   @ Base ./intfuncs.jl:310
 [4] top-level scope
   @ REPL[2]:1

```

of course

```julia
julia> 2.0^g()
0.125

```

works. I tried to `descend` into the call but did not find where `2^g()` misses the promotion to `2.0^g()`. I do not think it is a bug, but I do not see the rationale behind this behavior.

Thanks

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [October 3, 2024, 9:50am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848/2 "2024-10-03T09:50:45Z")

</div>

From documentation on `^`:

> If y is an Int literal (e.g. 2 in x^2 or -3 in x^-3), the Julia code x^y is transformed by the compiler to Base.literal\_pow(^, x, Val(y)), to enable compile-time specialization on the value of the exponent. (As a default fallback we have Base.literal\_pow(^, x, Val(y)) = ^(x,y), where usually ^ == Base.^ unless ^ has been defined in the calling namespace.) If y is a negative integer literal, then Base.literal\_pow transforms the operation to inv(x)^-y by default, where -y  
> is positive.

```julia-repl
julia> Base.literal_pow(^, 3, Val(-3))
0.037037037037037035

```

`g()` in `2^g()` is not a literal, so this transformation does not happen.

---

<div class="post-metadata">

### Author: ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)
#### Post date: [October 3, 2024, 9:58am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848/3 "2024-10-03T09:58:13Z")

</div>

Thanks @barucden … I overlooked the docs. Marking your reply as a solution.

Yet, it is not completely clear why `2^g()` where g() is inferred to be an `Int` should behave differently. g() is not a literal true, but there should be a code path where literal\_pow and pow should behave the same for provably integer exponents.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 3, 2024, 10:03am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848/4 "2024-10-03T10:03:11Z")

</div>

This is not just about the exponent being an integer, but being a _specific_ integer. Cf. [Confusing difference: ^literal vs ^variable - #6 by StefanKarpinski](https://discourse.julialang.org/t/confusing-difference-literal-vs-variable/13515/6) for some rationale.

---

<div class="post-metadata">

### Author: ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)
#### Post date: [October 3, 2024, 10:49am UTC](https://discourse.julialang.org/t/negative-integer-exponent/120848/5 "2024-10-03T10:49:35Z")

</div>

Thanks @GunnarFarneback.

[Confusing difference: ^literal vs ^variable - #6 by StefanKarpinski](https://discourse.julialang.org/t/confusing-difference-literal-vs-variable/13515/6) indeed answers precisely my doubts.
