-1^(1/2) is Complex(-1^(1/2)) ==-1.0 + 0.0im?

Why Complex(-1^(1/2))== -1.0 + 0.0im ???

 Version 1.3.0-rc4.1 (2019-10-15)

julia> Complex(-1^(.5))
-1.0 + 0.0im

julia> sqrt(Complex(-1))
0.0 + 1.0im

julia> Complex(-1^(1/2))
-1.0 + 0.0im

julia> 4^(1/2)
2.0

julia> 9^(1/2)
3.0

julia> Complex(-1^(1/2))
-1.0 + 0.0im

julia> sqrt(Complex(-1))
0.0 + 1.0im

Paul

julia> sqrt(complex(-1))
0.0 + 1.0im

julia> complex(-1)^(1/2)
0.0 + 1.0im

what’s inside () is evaluated first be, before you converting it by Complex() edit: also ^ is evaluated before - as mentioned below.

^ has higher precedence than -
thus -1^(1/2)=-(1^(1/2))

2 Likes

How to use it without “sqrt” ?

1.0

julia> (-1^(1/2))
-1.0
julia> Complex((-1)^(1/2))
ERROR: DomainError with -1.0:
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] ^ at .\math.jl:769 [inlined]
 [3] ^(::Int64, ::Float64) at .\promotion.jl:345
 [4] top-level scope at none:0

?

It says right in the error message you posted

3 Likes

Is no to easy :slight_smile:

julia> Complex(-1+0im)^(1/2)
6.123233995736766e-17 + 1.0im

julia> sqrt(-1+0im)
0.0 + 1.0im

julia>

weird, it gives me the correct result in my computer, what version are you using?

On my computer, with Julia v1.3:

julia> Complex(-1+0im)^(1/2)
0.0 + 1.0im

One more thing: There is no reason to write Complex(-1+0im). Just write (-1+0im), this is already a complex number, no reason to convert a complex number to (the same) complex number.

Ok,

6.123233995736766e-17 + 1.0im on version 0.6.x on 0.7 and higher is OK
Thanks Paul |

W dniu 2019-12-29 o 16:08, Andrés Riedemann via JuliaLang pisze: