From b0c741465585057f3f3d7c306cc2b8123d7c7ba6 Mon Sep 17 00:00:00 2001 From: cafaxo Date: Tue, 19 Feb 2019 23:12:36 +0100 Subject: [PATCH] Fix incorrect sign of atanh(complex(x,y)) if x == -1 (#31061) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 * Tests for expressions that were signed incorrectly * Use the correct type for 1 * Update doc * Update doc: Remove trailing whitespace (cherry picked from commit 66341a3806fb614ae00d4cf9016b3bed14c6986c) --- base/complex.jl | 12 ++++++++---- stdlib/LinearAlgebra/src/dense.jl | 4 ++-- test/complex.jl | 9 +++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) 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