Is it a bug for the power function?

The following code seems not works

Complex(-0.5)^(0.2)

It throw a error.

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

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

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

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.

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

^(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.

2 Likes

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

It is not. Why would you think so?

1 Like

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.

2 Likes

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

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.

3 Likes

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