Skip to content

Commit 66341a3

Browse files
cafaxosimonbyrne
authored andcommitted
Fix incorrect sign of atanh(complex(x,y)) if x == -1 (#31061)
* 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
1 parent f0d88dd commit 66341a3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

base/complex.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,14 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
952952
return Complex(copysign(zero(x),x), copysign(oftype(y,pi)/2, y))
953953
end
954954
return Complex(real(1/z), copysign(oftype(y,pi)/2, y))
955-
elseif ax==1
955+
end
956+
β = copysign(one(T), x)
957+
z *= β
958+
x, y = reim(z)
959+
if x == 1
956960
if y == 0
957-
ξ = copysign(oftype(x,Inf),x)
958-
η = zero(y)
961+
ξ = oftype(x, Inf)
962+
η = y
959963
else
960964
ym = ay+ρ
961965
ξ = log(sqrt(sqrt(4+y*y))/sqrt(ym))
@@ -970,7 +974,7 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
970974
end
971975
η = angle(Complex((1-x)*(1+x)-ysq, 2y))/2
972976
end
973-
Complex(ξ, η)
977+
β * Complex(ξ, η)
974978
end
975979
atanh(z::Complex) = atanh(float(z))
976980

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,8 @@ this function, see [^AH16_1].
926926
```jldoctest
927927
julia> acos(cos([0.5 0.1; -0.2 0.3]))
928928
2×2 Array{Complex{Float64},2}:
929-
0.5-5.55112e-17im 0.1-2.77556e-17im
930-
-0.2+2.498e-16im 0.3-3.46945e-16im
929+
0.5-8.32667e-17im 0.1+0.0im
930+
-0.2+2.63678e-16im 0.3-3.46945e-16im
931931
```
932932
"""
933933
function acos(A::AbstractMatrix)

test/complex.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ end
696696
@test isequal(atanh(complex(-0.0,-Inf)),complex(-0.0,-pi/2))
697697

698698
@test isequal(atanh(complex( 1.0, 0.0)),complex( Inf, 0.0))
699+
@test isequal(atanh(complex( 1.0,-0.0)),complex( Inf,-0.0))
699700
@test isequal(atanh(complex(-1.0, 0.0)),complex(-Inf, 0.0))
701+
@test isequal(atanh(complex(-1.0,-0.0)),complex(-Inf,-0.0))
700702
@test isequal(atanh(complex( 5.0, Inf)),complex( 0.0, pi/2))
701703
@test isequal(atanh(complex( 5.0,-Inf)),complex( 0.0,-pi/2))
702704
@test isequal(atanh(complex( 5.0, NaN)),complex( NaN, NaN))
@@ -1065,3 +1067,10 @@ end
10651067

10661068
@test @inferred(2.0^(3.0+0im)) === @inferred((2.0+0im)^(3.0+0im)) === @inferred((2.0+0im)^3.0) === 8.0+0.0im
10671069
end
1070+
1071+
@testset "issue #31054" begin
1072+
@test tanh(atanh(complex(1.0,1.0))) == complex(1.0,1.0)
1073+
@test tanh(atanh(complex(1.0,-1.0))) == complex(1.0,-1.0)
1074+
@test tanh(atanh(complex(-1.0,1.0))) == complex(-1.0,1.0)
1075+
@test tanh(atanh(complex(-1.0,-1.0))) == complex(-1.0,-1.0)
1076+
end

0 commit comments

Comments
 (0)