# REPL domain error for exponentiation

**URL:** <https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 23, 2019, 2:29am UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408 "2019-11-23T02:29:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [November 23, 2019, 2:29am UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408/1 "2019-11-23T02:29:39Z")

</div>

Why

```nohighlight
julia> 2^-10
0.0009765625

julia> t = -10
-10

julia> 2^t
ERROR: DomainError with -10:
Cannot raise an integer x to a negative power -10.
Make x or -10 a float by adding a zero decimal (e.g., 2.0^-10 or 2^-10.0 instead of 2^-10), or write 1/x^10, float(x)^-10, x^float(-10) or (x//1)^-10
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, ::Int64) at ./intfuncs.jl:176
 [2] power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:197
 [3] ^(::Int64, ::Int64) at ./intfuncs.jl:221
 [4] top-level scope at REPL[40]:1

julia> 2.0^t
0.0009765625

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [November 23, 2019, 4:24am UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408/2 "2019-11-23T04:24:25Z")

</div>

Happens in windows, julia 1.3 rc5.1

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 23, 2019, 4:24am UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408/3 "2019-11-23T04:24:38Z")

</div>

I think the literal expression takes a different path, possibly going thru this line

```julia
        :(literal_pow(^, inv(x), $(Val{-p}())))

```

at line 255 of `intfuncs.jl`. There are comments at line 224:

```julia
# x^p for any literal integer p is lowered to Base.literal_pow(^, x, Val(p))
# to enable compile-time optimizations specialized to p.

```

but I don’t understand how code gets dispatched on literals as opposed to Ints. Note that `2^-1` succeeds but `2^Int(-1)` does not.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 23, 2019, 4:38am UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408/4 "2019-11-23T04:38:57Z")

</div>

And this

> Experimental feature: `x^n` for integer literals `n` (e.g. `x^3` or `x^-3` ) is now lowered to `Base.literal_pow(^, x, Val{n})` , to enable compile-time specialization for literal integer exponents ([#20530](https://github.com/JuliaLang/julia/issues/20530), [#20889](https://github.com/JuliaLang/julia/issues/20889)).

from the Julia v0.6.0 release notes.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 23, 2019, 1:05pm UTC](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408/5 "2019-11-23T13:05:19Z")

</div>

Sorry for the multiple posts, you’re seeing me work this out in my slow fashion.

The answer is type safety.

In order for the power function to be type safe with integers, the exponent must be limited to non-negative values. On the other hand, literal expressions, being fixed in value, don’t threaten type safety. As a matter of convenience a literal with a negative exponent is allowed (because it is always a float).
