Skip to content

Commit

Permalink
Fix and document sign(::Complex) (fixes JuliaLang#12599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfasi committed Aug 14, 2015
1 parent 72c344c commit 88f417d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 0 additions & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ abs(z::Complex) = hypot(real(z), imag(z))
abs2(z::Complex) = real(z)*real(z) + imag(z)*imag(z)
inv(z::Complex) = conj(z)/abs2(z)
inv{T<:Integer}(z::Complex{T}) = inv(float(z))
sign(z::Complex) = z/abs(z)

-(z::Complex) = Complex(-real(z), -imag(z))
+(z::Complex, w::Complex) = Complex(real(z) + real(w), imag(z) + imag(w))
Expand Down
3 changes: 1 addition & 2 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15178,8 +15178,7 @@ doc"""
```rst
::
sign(x)
Return ``+1`` if ``x`` is positive, ``0`` if ``x == 0``, and ``-1`` if ``x`` is negative.
Return zero if ``x==0`` and :math:`x/|x|` otherwise (i.e., ±1 for real ``x``).
```
"""
sign
Expand Down
2 changes: 1 addition & 1 deletion base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ ComplexTypes = Union{Complex64, Complex128}
abs2_fast{T<:ComplexTypes}(x::T) = real(x)*real(x) + imag(x)*imag(x)
conj_fast{T<:ComplexTypes}(x::T) = T(real(x), -imag(x))
inv_fast{T<:ComplexTypes}(x::T) = conj(x) / abs2(x)
sign_fast{T<:ComplexTypes}(x::T) = x / abs(x)
sign_fast{T<:ComplexTypes}(x::T) = x == 0 ? float(zero(x)) : x/abs(x)

add_fast{T<:ComplexTypes}(x::T, y::T) =
T(real(x)+real(y), imag(x)+imag(y))
Expand Down
1 change: 1 addition & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ last(x::Number) = x
divrem(x,y) = (div(x,y),rem(x,y))
fldmod(x,y) = (fld(x,y),mod(x,y))
signbit(x::Real) = x < 0
sign(x::Number) = x == 0? float(zero(x)) : x/abs(x)
sign(x::Real) = ifelse(x < 0, oftype(x,-1), ifelse(x > 0, one(x), x))
sign(x::Unsigned) = ifelse(x > 0, one(x), x)
abs(x::Real) = ifelse(signbit(x), -x, x)
Expand Down
20 changes: 20 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,26 @@ end
@test log10(10+0im) === 1.0 + 0.0im
@test log2(2+0im) === 1.0 + 0.0im

# sign
for T in (Float32, Float64)
z = Complex{T}(1)
@test typeof(sign(z)) == typeof(z)
z = Complex{T}(0)
@test typeof(sign(z)) == typeof(z)
end
for T in (Int32, Int64)
z = Complex{T}(1)
@test typeof(sign(z)) == typeof(float(z))
z = Complex{T}(0)
@test typeof(sign(z)) == typeof(float(z))
end

@test sign(0 + 0im) == 0
@test sign(2 + 0im) == 1
@test sign(-2 + 0im) == -1
@test sign(1 + im) (1 + im) / sqrt(2)
@test sign(1 - im) (1 - im) / sqrt(2)

# cis
@test_approx_eq cis(0.0+1.0im) 0.367879441171442321595523770161460867445811131031767834507836+0.0im
@test_approx_eq cis(1.0+0.0im) 0.54030230586813971740093660744297660373231042061+0.84147098480789650665250232163029899962256306079im
Expand Down

0 comments on commit 88f417d

Please sign in to comment.