Skip to content

Commit

Permalink
Fix incorrect sign of atanh(complex(x,y)) if x == -1 (#31061)
Browse files Browse the repository at this point in the history
* Fix incorrect sign in atanh

In the case that x==-1, we have to flip the sign of ξ.

* Formatting: Add space after comma

Co-Authored-By: cafaxo <cafaxo@gmail.com>

* Add test

* Do not drop sign of zero

* Flip sign to avoid a DomainError

This fixes atanh(prevfloat(-1.0) + 0im)

* Test all four combinations

Co-Authored-By: cafaxo <cafaxo@gmail.com>

* Tests for expressions that were signed incorrectly

* Use the correct type for 1

* Update doc

* Update doc: Remove trailing whitespace
  • Loading branch information
cafaxo authored and simonbyrne committed Feb 19, 2019
1 parent f0d88dd commit 66341a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
12 changes: 8 additions & 4 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,14 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
return Complex(copysign(zero(x),x), copysign(oftype(y,pi)/2, y))
end
return Complex(real(1/z), copysign(oftype(y,pi)/2, y))
elseif ax==1
end
β = copysign(one(T), x)
z *= β
x, y = reim(z)
if x == 1
if y == 0
ξ = copysign(oftype(x,Inf),x)
η = zero(y)
ξ = oftype(x, Inf)
η = y
else
ym = ay+ρ
ξ = log(sqrt(sqrt(4+y*y))/sqrt(ym))
Expand All @@ -970,7 +974,7 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
end
η = angle(Complex((1-x)*(1+x)-ysq, 2y))/2
end
Complex(ξ, η)
β * Complex(ξ, η)
end
atanh(z::Complex) = atanh(float(z))

Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,8 @@ this function, see [^AH16_1].
```jldoctest
julia> acos(cos([0.5 0.1; -0.2 0.3]))
2×2 Array{Complex{Float64},2}:
0.5-5.55112e-17im 0.1-2.77556e-17im
-0.2+2.498e-16im 0.3-3.46945e-16im
0.5-8.32667e-17im 0.1+0.0im
-0.2+2.63678e-16im 0.3-3.46945e-16im
```
"""
function acos(A::AbstractMatrix)
Expand Down
9 changes: 9 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,9 @@ end
@test isequal(atanh(complex(-0.0,-Inf)),complex(-0.0,-pi/2))

@test isequal(atanh(complex( 1.0, 0.0)),complex( Inf, 0.0))
@test isequal(atanh(complex( 1.0,-0.0)),complex( Inf,-0.0))
@test isequal(atanh(complex(-1.0, 0.0)),complex(-Inf, 0.0))
@test isequal(atanh(complex(-1.0,-0.0)),complex(-Inf,-0.0))
@test isequal(atanh(complex( 5.0, Inf)),complex( 0.0, pi/2))
@test isequal(atanh(complex( 5.0,-Inf)),complex( 0.0,-pi/2))
@test isequal(atanh(complex( 5.0, NaN)),complex( NaN, NaN))
Expand Down Expand Up @@ -1065,3 +1067,10 @@ end

@test @inferred(2.0^(3.0+0im)) === @inferred((2.0+0im)^(3.0+0im)) === @inferred((2.0+0im)^3.0) === 8.0+0.0im
end

@testset "issue #31054" begin
@test tanh(atanh(complex(1.0,1.0))) == complex(1.0,1.0)
@test tanh(atanh(complex(1.0,-1.0))) == complex(1.0,-1.0)
@test tanh(atanh(complex(-1.0,1.0))) == complex(-1.0,1.0)
@test tanh(atanh(complex(-1.0,-1.0))) == complex(-1.0,-1.0)
end

2 comments on commit 66341a3

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.