# Is it a bug for the power function?

**URL:** <https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833>\
**Category:** General Usage\
**Created:** [April 1, 2020, 8:29am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833 "2020-04-01T08:29:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 1, 2020, 8:29am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/1 "2020-04-01T08:29:13Z")

</div>

The following code seems not works

```julia
Complex(-0.5)^(0.2)

```

It throw a error.

 ![WeChat2a31ecd5955abce8eaa85bd717382934](https://global.discourse-cdn.com/julialang/original/3X/f/0/f001175b87de1c2f23cccb504e9de363cb1b9437.png)

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 1, 2020, 8:33am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/2 "2020-04-01T08:33:33Z")

</div>

Works perfectly fine for me in all Julia 1.X:

```julia
julia> Complex(-0.5)^(0.2)
0.7042902001692478 + 0.5116967824803669im

```

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 1, 2020, 8:46am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/3 "2020-04-01T08:46:58Z")

</div>

Thanks for replying. The problem is because I reload the power method as following

```julia
import Base.^
^(f::Int,g::Float32)=Complex(f)^g
^(f::Int,g::Float64)=Complex(f)^g
^(f::Float64,g::Float64)=Complex(f)^g
^(f::Float32,g::Float64)=Complex(f)^g
^(f::Float64,g::Float32)=Complex(f)^g
^(f::Float32,g::Float32)=Complex(f)^g

```

But I do not understand why these reloading method will lead errors.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 1, 2020, 8:54am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/4 "2020-04-01T08:54:43Z")

</div>

Well, don’t do that then? You probably should have included that information in the original post instead of implying it was a bug in Julia.

It breaks because you introduce an infinite loop. For example

```julia
^(f::Float64,g::Float64)=Complex(f)^g

```

will result in a call to `^(::Complex{Float64}, ::Float64)`, but that method in turn probably (didn’t verify) calls method `^(::Float64, ::Float64)`, but you have just overridden that method to call `^(::Complex{Float64}, ::Float64)`, which then calls `^(::Float64, ::Float64)` etc, so it is an infinite loop and hence an `StackOverflowError`.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 1, 2020, 8:58am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/5 "2020-04-01T08:58:35Z")

</div>

I see. Thanks. Perhaps the Type-Complex is a subType of Float64

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 1, 2020, 9:00am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/6 "2020-04-01T09:00:18Z")

</div>

It is not. Why would you think so?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 1, 2020, 10:16am UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/7 "2020-04-01T10:16:12Z")

</div>

This is called _type piracy_ and is strongly discouraged. It can break code in ways that are hard to predict and understand.

You’re actually lucky that things broke down so quickly and in obvious ways.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 1, 2020, 12:03pm UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/8 "2020-04-01T12:03:04Z")

</div>

> [@DNF](#):
>
> down

Then I confused, why the code lead an infinite loop?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 1, 2020, 12:09pm UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/9 "2020-04-01T12:09:19Z")

</div>

Computing the power function on a complex number involves computing it on the real and imaginary parts of that number, and _they_ are of type `Float64`. And then each of them get converted to a complex number by your overloading, and round you go in circles.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 1, 2020, 12:27pm UTC](https://discourse.julialang.org/t/is-it-a-bug-for-the-power-function/36833/10 "2020-04-01T12:27:23Z")

</div>

Now I understand it completely. Thanks for the explanation in detail.
