Skip to content

Commit

Permalink
Use inv to define inverse of trigonometric functions (#32252)
Browse files Browse the repository at this point in the history
* Use `inv` to define inverse of trigonometric functions

* Add tests for secd and cscd
  • Loading branch information
giordano authored and ararslan committed Jun 11, 2019
1 parent 39e1205 commit 5ca47e0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/special/trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -966,17 +966,17 @@ for (finv, f, finvh, fh, finvd, fd, fn) in ((:sec, :cos, :sech, :cosh, :secd, :c
$($name)(x)
Compute the $($fn) of `x`, where `x` is in radians.
""" ($finv)(z::T) where {T<:Number} = one(T) / (($f)(z))
""" ($finv)(z::Number) = inv(($f)(z))
@doc """
$($hname)(x)
Compute the hyperbolic $($fn) of `x`.
""" ($finvh)(z::T) where {T<:Number} = one(T) / (($fh)(z))
""" ($finvh)(z::Number) = inv(($fh)(z))
@doc """
$($dname)(x)
Compute the $($fn) of `x`, where `x` is in degrees.
""" ($finvd)(z::T) where {T<:Number} = one(T) / (($fd)(z))
""" ($finvd)(z::Number) = inv(($fd)(z))
end
end

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

Expand Down
23 changes: 23 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ end
@test sqrt(x) sqrt(big(x))
@test tan(x) tan(big(x))
@test tanh(x) tanh(big(x))
@test sec(x) sec(big(x))
@test csc(x) csc(big(x))
@test secd(x) secd(big(x))
@test cscd(x) cscd(big(x))
@test sech(x) sech(big(x))
@test csch(x) csch(big(x))
end
@testset "Inverses" begin
@test acos(cos(x)) x
Expand Down Expand Up @@ -156,6 +162,12 @@ end
@test sinh(x) (exp(x)-exp(-x))/2
@test tan(x) sin(x)/cos(x)
@test tanh(x) sinh(x)/cosh(x)
@test sec(x) inv(cos(x))
@test csc(x) inv(sin(x))
@test secd(x) inv(cosd(x))
@test cscd(x) inv(sind(x))
@test sech(x) inv(cosh(x))
@test csch(x) inv(sinh(x))
end
end
end
Expand Down Expand Up @@ -1125,3 +1137,14 @@ end
@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

@testset "issue #29840" begin
@testset "$T" for T in (ComplexF32, ComplexF64, Complex{BigFloat})
@test isequal(ComplexF64(sec(T(-10, 1000))), ComplexF64(-0.0, 0.0))
@test isequal(ComplexF64(csc(T(-10, 1000))), ComplexF64(0.0, 0.0))
@test isequal(ComplexF64(sech(T(1000, 10))), ComplexF64(-0.0, 0.0))
@test isequal(ComplexF64(csch(T(1000, 10))), ComplexF64(-0.0, 0.0))
@test isequal(ComplexF64(secd(T(-1000, 100000))), ComplexF64(0.0, 0.0))
@test isequal(ComplexF64(cscd(T(-1000, 100000))), ComplexF64(0.0, -0.0))
end
end
18 changes: 18 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ end
@test sqrt(x) sqrt(big(x))
@test tan(x) tan(big(x))
@test tanh(x) tanh(big(x))
@test sec(x) sec(big(x))
@test csc(x) csc(big(x))
@test secd(x) secd(big(x))
@test cscd(x) cscd(big(x))
@test sech(x) sech(big(x))
@test csch(x) csch(big(x))
end
@testset "Special values" begin
@test isequal(T(1//4)^T(1//2), T(1//2))
Expand Down Expand Up @@ -208,6 +214,12 @@ end
@test isequal(sqrt(T(100000000)), T(10000))
@test isequal(tan(T(0)), T(0))
@test tan(T(pi)/4) T(1) atol=eps(T)
@test isequal(sec(T(pi)), -one(T))
@test isequal(csc(T(pi)/2), one(T))
@test isequal(secd(T(180)), -one(T))
@test isequal(cscd(T(90)), one(T))
@test isequal(sech(log(one(T))), one(T))
@test isequal(csch(zero(T)), T(Inf))
end
@testset "Inverses" begin
@test acos(cos(x)) x
Expand Down Expand Up @@ -244,6 +256,12 @@ end
@test sinh(x) (exp(x)-exp(-x))/2
@test tan(x) sin(x)/cos(x)
@test tanh(x) sinh(x)/cosh(x)
@test sec(x) inv(cos(x))
@test csc(x) inv(sin(x))
@test secd(x) inv(cosd(x))
@test cscd(x) inv(sind(x))
@test sech(x) inv(cosh(x))
@test csch(x) inv(sinh(x))
end
@testset "Edge cases" begin
@test isinf(log(zero(T)))
Expand Down

0 comments on commit 5ca47e0

Please sign in to comment.