Skip to content

Commit

Permalink
complex: define unary plus and add tests (JuliaLang#31237)
Browse files Browse the repository at this point in the history
  • Loading branch information
eulerkochy authored and fredrikekre committed Mar 6, 2019
1 parent 00d6750 commit 6552521
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ abs2(z::Complex) = real(z)*real(z) + imag(z)*imag(z)
inv(z::Complex) = conj(z)/abs2(z)
inv(z::Complex{<:Integer}) = inv(float(z))

+(z::Complex) = Complex(+real(z), +imag(z))
-(z::Complex) = Complex(-real(z), -imag(z))
+(z::Complex, w::Complex) = Complex(real(z) + real(w), imag(z) + imag(w))
-(z::Complex, w::Complex) = Complex(real(z) - real(w), imag(z) - imag(w))
Expand Down
11 changes: 11 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ end
@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im"
@test sprint(show, complex(true, true)) == "Complex(true,true)"

@testset "unary operator on complex boolean" begin
@test +Complex(true, true) === Complex(1, 1)
@test +Complex(true, false) === Complex(1, 0)
@test +Complex(false, true) === Complex(0, 1)
@test +Complex(false, false) === Complex(0, 0)
@test -Complex(true, true) === Complex(-1, -1)
@test -Complex(true, false) === Complex(-1, 0)
@test -Complex(false, true) === Complex(0, -1)
@test -Complex(false, false) === Complex(0, 0)
end

@testset "arithmetic" begin
@testset for T in (Float16, Float32, Float64, BigFloat)
t = true
Expand Down

0 comments on commit 6552521

Please sign in to comment.