Skip to content

Commit 1fe7d75

Browse files
committed
Use inv to define inverse of trigonometric functions
1 parent 9a3e1c7 commit 1fe7d75

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

base/special/trig.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -966,17 +966,17 @@ for (finv, f, finvh, fh, finvd, fd, fn) in ((:sec, :cos, :sech, :cosh, :secd, :c
966966
$($name)(x)
967967
968968
Compute the $($fn) of `x`, where `x` is in radians.
969-
""" ($finv)(z::T) where {T<:Number} = one(T) / (($f)(z))
969+
""" ($finv)(z::Number) = inv(($f)(z))
970970
@doc """
971971
$($hname)(x)
972972
973973
Compute the hyperbolic $($fn) of `x`.
974-
""" ($finvh)(z::T) where {T<:Number} = one(T) / (($fh)(z))
974+
""" ($finvh)(z::Number) = inv(($fh)(z))
975975
@doc """
976976
$($dname)(x)
977977
978978
Compute the $($fn) of `x`, where `x` is in degrees.
979-
""" ($finvd)(z::T) where {T<:Number} = one(T) / (($fd)(z))
979+
""" ($finvd)(z::Number) = inv(($fd)(z))
980980
end
981981
end
982982

@@ -988,10 +988,10 @@ for (tfa, tfainv, hfa, hfainv, fn) in ((:asec, :acos, :asech, :acosh, "secant"),
988988
@eval begin
989989
@doc """
990990
$($tname)(x)
991-
Compute the inverse $($fn) of `x`, where the output is in radians. """ ($tfa)(y::T) where {T<:Number} = ($tfainv)(one(T) / y)
991+
Compute the inverse $($fn) of `x`, where the output is in radians. """ ($tfa)(y::Number) = ($tfainv)(inv(y))
992992
@doc """
993993
$($hname)(x)
994-
Compute the inverse hyperbolic $($fn) of `x`. """ ($hfa)(y::T) where {T<:Number} = ($hfainv)(one(T) / y)
994+
Compute the inverse hyperbolic $($fn) of `x`. """ ($hfa)(y::Number) = ($hfainv)(inv(y))
995995
end
996996
end
997997

test/complex.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ end
124124
@test sqrt(x) sqrt(big(x))
125125
@test tan(x) tan(big(x))
126126
@test tanh(x) tanh(big(x))
127+
@test sec(x) sec(big(x))
128+
@test csc(x) csc(big(x))
129+
@test sech(x) sech(big(x))
130+
@test csch(x) csch(big(x))
127131
end
128132
@testset "Inverses" begin
129133
@test acos(cos(x)) x
@@ -156,6 +160,10 @@ end
156160
@test sinh(x) (exp(x)-exp(-x))/2
157161
@test tan(x) sin(x)/cos(x)
158162
@test tanh(x) sinh(x)/cosh(x)
163+
@test sec(x) inv(cos(x))
164+
@test csc(x) inv(sin(x))
165+
@test sech(x) inv(cosh(x))
166+
@test csch(x) inv(sinh(x))
159167
end
160168
end
161169
end
@@ -1125,3 +1133,12 @@ end
11251133
@test tanh(atanh(complex(-1.0,1.0))) == complex(-1.0,1.0)
11261134
@test tanh(atanh(complex(-1.0,-1.0))) == complex(-1.0,-1.0)
11271135
end
1136+
1137+
@testset "issue #29840" begin
1138+
@testset "$T" for T in (ComplexF32, ComplexF64, Complex{BigFloat})
1139+
@test isequal(ComplexF64(sec(T(-10, 1000))), ComplexF64(-0.0, 0.0))
1140+
@test isequal(ComplexF64(csc(T(-10, 1000))), ComplexF64(0.0, 0.0))
1141+
@test isequal(ComplexF64(sech(T(1000, 10))), ComplexF64(-0.0, 0.0))
1142+
@test isequal(ComplexF64(csch(T(1000, 10))), ComplexF64(-0.0, 0.0))
1143+
end
1144+
end

test/math.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ end
170170
@test sqrt(x) sqrt(big(x))
171171
@test tan(x) tan(big(x))
172172
@test tanh(x) tanh(big(x))
173+
@test sec(x) sec(big(x))
174+
@test csc(x) csc(big(x))
175+
@test sech(x) sech(big(x))
173176
end
174177
@testset "Special values" begin
175178
@test isequal(T(1//4)^T(1//2), T(1//2))
@@ -208,6 +211,10 @@ end
208211
@test isequal(sqrt(T(100000000)), T(10000))
209212
@test isequal(tan(T(0)), T(0))
210213
@test tan(T(pi)/4) T(1) atol=eps(T)
214+
@test isequal(sec(T(pi)), -one(T))
215+
@test isequal(csc(T(pi)/2), one(T))
216+
@test isequal(sech(log(one(T))), one(T))
217+
@test isequal(csch(zero(T)), T(Inf))
211218
end
212219
@testset "Inverses" begin
213220
@test acos(cos(x)) x
@@ -244,6 +251,9 @@ end
244251
@test sinh(x) (exp(x)-exp(-x))/2
245252
@test tan(x) sin(x)/cos(x)
246253
@test tanh(x) sinh(x)/cosh(x)
254+
@test sec(x) inv(cos(x))
255+
@test csc(x) inv(sin(x))
256+
@test sech(x) inv(cosh(x))
247257
end
248258
@testset "Edge cases" begin
249259
@test isinf(log(zero(T)))

0 commit comments

Comments
 (0)