Skip to content

Commit

Permalink
Make complex sin and cos type stable
Browse files Browse the repository at this point in the history
malmaud committed Oct 12, 2015
1 parent 6aa15b8 commit 0792585
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
@@ -551,6 +551,13 @@ function sin(z::Complex)
if !isfinite(zr) && zi == 0 return Complex(oftype(zr, NaN), zi) end
if !isfinite(zr) && isfinite(zi) return Complex(oftype(zr, NaN), oftype(zi, NaN)) end
if !isfinite(zr) && !isfinite(zi) return Complex(zr, oftype(zi, NaN)) end
_sin(z)
end

sin{T<:Integer}(z::Complex{T}) = _sin(z)

function _sin(z::Complex)
zr, zi = reim(z)
Complex(sin(zr)*cosh(zi), cos(zr)*sinh(zi))
end

@@ -567,6 +574,13 @@ function cos(z::Complex)
return Complex(oftype(zr, NaN), zi==0 ? -copysign(zi, zr) : oftype(zi, NaN))
end
if isnan(zr) && zi==0 return Complex(zr, abs(zi)) end
_cos(z)
end

cos{T<:Integer}(z::Complex{T}) = _cos(z)

function _cos(z::Complex)
zr, zi = reim(z)
Complex(cos(zr)*cosh(zi), -sin(zr)*sinh(zi))
end

6 changes: 6 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
@@ -908,3 +908,9 @@ end

# issue #10926
@test typeof- 1im) == Complex{Float64}

# issue #11839: type stability for Complex{Int64}
let x = 1+im
@inferred sin(x)
@inferred cos(x)
end

0 comments on commit 0792585

Please sign in to comment.