# Weird result in power operator

**URL:** <https://discourse.julialang.org/t/weird-result-in-power-operator/20824>\
**Category:** New to Julia\
**Created:** [February 15, 2019, 1:43pm UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824 "2019-02-15T13:43:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hesampour](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@hesampour](https://discourse.julialang.org/u/hesampour)\
**Post date:** [February 15, 2019, 1:43pm UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/1 "2019-02-15T13:43:40Z")

</div>

hi  
there is a non stable result in power operator. this is the example. when I call the power with the variable the power not works. but when calling directly it  
julia\> a = -1.9345e6  
-1.9345e6

julia\> a^1.5  
ERROR: DomainError with -1.9345e6:  
Exponentiation yielding a complex result requires a complex argument.  
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.  
Stacktrace:  
[1] throw\_exp\_domainerror(::Float64) at .\math.jl:35  
[2] ^(::Float64, ::Float64) at .\math.jl:782  
[3] top-level scope at none:0

calling directly:  
julia\> -1.9345e6^1.5  
-2.6906245536352706e9

matlab result:  
-1.9345e+06^1.5

ans =

-2.6906e+09

calling with complex

julia\> complex(a)^1.5  
0.0 - 2.6906245536352706e9im

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 15, 2019, 1:50pm UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/2 "2019-02-15T13:50:26Z")

</div>

The `-` operator has lower precedence than the `^` operator, so the expression `-1.9345e6^1.5` means `-(1.9345e6^1.5)` rather than `(-1.9345e6)^1.5`. If you try the `a = -1.9345e6; a^1.5` version in Matlab you’ll get the same imaginary value as `complex(a)^1.5` gives in Julia.

Also, please quote code in posts:

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 13, 2019, 9:11am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/3 "2019-03-13T09:11:41Z")

</div>

While preparing an introductory talk about Julia on my University, I am looking and Chris Rackauckas’s notes why Julia. I am puzzled with his example with powers, as `5^5` should return an Int (and it does), whereas `5^-5` should fail. Nevertheless `5^-5` executes correctly in repl, but when I define function

```julia
julia> f(x, y) = x^y - 5
julia> f(5, -5)
ERROR: DomainError with -5:

```

it fails as it should. I noticed the thread [make x^-n equivalent to inv(x)^n for literal n by stevengj · Pull Request #24240 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/24240), which says that `5^-5` should be lowered to `inv(5)^5`, which more or less explains the behavior in repl, but does not explain the discrepancy. Moreover, when I ask, which version is executed

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

```

it points me to an inline function to `^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p)`, which makes perfect sense and indeed,

```julia
julia> Base.power_by_squaring(5, -5)
ERROR: DomainError with -5:

```

works as expected.

To conclude, there is something that I do not understand, and I would very much like to understand.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 13, 2019, 9:17am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/4 "2019-03-13T09:17:37Z")

</div>

Literals are treated differently:

```julia
julia> Base.Meta.@lower 5^-5
:($(Expr(:thunk, CodeInfo(
1 ─ %1 = (Core.apply_type)(Base.Val, -5)
│ %2 = (%1)()
│ %3 = (Base.literal_pow)(^, 5, %2)
└── return %3
))))

```

which is equivalent to

```julia
julia> Base.literal_pow(^, 5, Val(-5))
0.0003200000000000001

julia> f(x, y) = Base.literal_pow(^,x,y)
f (generic function with 1 method)

julia> f(5, Val(-5))
0.0003200000000000001

```

I don’t know what the reason behind this design is. Presumably to avoid having to type literal floats.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 13, 2019, 9:23am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/5 "2019-03-13T09:23:15Z")

</div>

> [@Tomas\_Pevny](#):
>
> Nevertheless `5^-5` executes correctly in repl

Because

```julia
julia> Meta.lower(Main, :(5^-5))
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Core.apply_type(Base.Val, -5)
│ %2 = (%1)()
│ %3 = Base.literal_pow(^, 5, %2)
└── return %3
))))

```

note the `literal_pow` (which indeed comes from that PR), while in the function it is not constant, so is not parsed/lowered like that, but as an application of `^`:

```julia
julia> @code_warntype f(5,-5)
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Int64
  y::Int64

Body::Int64
1 ─ %1 = (x ^ y)::Int64
│ %2 = (%1 - 5)::Int64
└── return %2

```

> [@Tomas\_Pevny](#):
>
> there is something that I do not understand, and I would very much like to understand

See the tools above to help you in general.

Is there something specific you want to achieve, eg have your code work in general, or is this just about understanding what is going on?

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 13, 2019, 9:23am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/6 "2019-03-13T09:23:23Z")

</div>

I have figure that it is hitting `literal_pow`, but I was quite confused that `@which` has not pointed me to the correct method that was used.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 13, 2019, 9:25am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/7 "2019-03-13T09:25:06Z")

</div>

Thanks @mauro3 and @Tamas_Papp. I do not want to achieve anything special, I am just interested in internals, how does it work. Your explanation is great, yet it adds another thing I do not understand well.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 13, 2019, 9:39am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/8 "2019-03-13T09:39:44Z")

</div>

> [@mauro3](#):
>
> I don’t know what the reason behind this design is. Presumably to avoid having to type literal floats.

Precisely, see [#3024](https://github.com/JuliaLang/julia/issues/3024) (from the dawn of time).

Personally, I would prefer that users type `inv(a)^n` if that’s what they want, but that adds yet another item to the list of unexpected things that surprises people who started using Julia without bothering to read the manual.

In exchange for resolving that, the confusion is amplified and backshifted in time to the point where users become interested in finer points of the language, and encounter the magic that was used in the implementation.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 13, 2019, 9:42am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/9 "2019-03-13T09:42:35Z")

</div>

I confess that the last time I have read the manual was for Julia v0.3. I think I have a homework.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 13, 2019, 9:49am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/10 "2019-03-13T09:49:15Z")

</div>

That remark was not targeted at you (but of course, re-reading the manual from time to time is great, a lot of effort goes into it and it and it is really useful), it was more about the reasons that I assume lead to the introduction of this feature.

Namely, the pivotal role of _type stability_ in Julia is very easy to miss if one just skims through a short tutorial or similar. Yet it explains the design choices that answer questions like “why doesn’t `√-2` work?”.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 13, 2019, 9:50am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/11 "2019-03-13T09:50:15Z")

</div>

I think your remark was very sound. Thanks a lot for opening me eyes.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [March 13, 2019, 11:55am UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/12 "2019-03-13T11:55:01Z")

</div>

See also [`@which` and `@code_warntype` don't show `^Val{p}` lowering · Issue #21014 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/21014) for the `@which` bug.

---

<div class="post-metadata">

**Author:** ![acasteigts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acasteigts/32/15256_2.png) [@acasteigts](https://discourse.julialang.org/u/acasteigts)\
**Post date:** [June 6, 2020, 12:46pm UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/13 "2020-06-06T12:46:00Z")

</div>

I am unsure whether this should be a new thread, the title of this thread fits well…

Typing `16^16` in the REPL gives a result of `0` (Julia v1.4).

I guess this is related to the size of Int64 (and `2^64` logically gives the same answer, so does `16^17`). But the fact that no error is raised here may be a problem, as one may take long to find out where the error comes from (in a larger calculation).

---

<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:** [June 6, 2020, 1:19pm UTC](https://discourse.julialang.org/t/weird-result-in-power-operator/20824/14 "2020-06-06T13:19:55Z")

</div>

Integer overflow is a different issue which comes up regularly. The most recent discussion about it is [here](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108), including reasons for this behaviour, ways around it and some suggestions to make it easier on newcomers.
