diff --git a/base/complex.jl b/base/complex.jl index dc736ebb8e797..a635ae1f7881c 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -917,10 +917,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)) @@ -935,7 +939,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)) diff --git a/stdlib/LinearAlgebra/src/dense.jl b/stdlib/LinearAlgebra/src/dense.jl index ee5f42b0a2a1d..000398b5d3686 100644 --- a/stdlib/LinearAlgebra/src/dense.jl +++ b/stdlib/LinearAlgebra/src/dense.jl @@ -925,8 +925,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) diff --git a/test/complex.jl b/test/complex.jl index 67be8f2a5fa4d..cd9975074d700 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -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)) @@ -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