Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functions tan and cot with multiples of pi #28943

Open
raneameya opened this issue Aug 29, 2018 · 10 comments
Open

functions tan and cot with multiples of pi #28943

raneameya opened this issue Aug 29, 2018 · 10 comments
Labels
feature Indicates new feature / enhancement requests maths Mathematical functions

Comments

@raneameya
Copy link

Is it reasonable to expect tan(π/2) == Inf to be true? Similarly, should tan(π) == 0 be true?

Also, not to labour the point, but I noticed that cot(π) threw a MethodError.

julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = "C:\XXXX\atom\app-1.29.0\atom.exe" -a
  JULIA_NUM_THREADS = 4
@pochoi
Copy link
Contributor

pochoi commented Aug 29, 2018

On the other hand, the functions sinpi and cospi work as you would expect.

julia> sinpi(0.5)/cospi(0.5) == Inf
true
julia> sinpi(1.0)/cospi(1.0) == 0.0
true

Unfortunately there is no tanpi yet.


You made a good point. cot(π) will call one(Irrational{:π}) / tan(π). However, one(Irrational{:π}) is not defined. Same situation for csc and sec. It seems that the conversion of type Irrational is not perfect at this moment.

@JeffBezanson JeffBezanson added the maths Mathematical functions label Sep 4, 2018
@JeffBezanson JeffBezanson changed the title Trigonometry functions tan and cot functions tan and cot with multiples of pi Sep 4, 2018
@stevengj
Copy link
Member

See also #7994 and #5561.

@ravibitsgoa
Copy link
Contributor

Can we define a tanpi(x) method as sinpi(x)/cospi(x)?
And similarly for cotpi, cscpi, secpi?

@gekaremi
Copy link

Is it good practice to replace tangent and cotangent with sine-cosine ratios?

I stumbled on the fact that in Julia there is no this function (cotpi and tanpi), and in numpy (as another example) there is no cotangent at all, so I would like to know what can be done?

(sorry for bad English)

@StefanKarpinski
Copy link
Member

Defining accurate versions of all of these trig functions for multiples of pi seems like the right way.

@laborg
Copy link
Contributor

laborg commented Oct 16, 2023

at least tanpi has been added in 1.10

@waldyrious
Copy link
Contributor

Why was this issue closed, @oscardssmith? While there's tanpi now, there still isn't a cotpi, cscpi, or secpi.

@oscardssmith
Copy link
Member

fair enough

@oscardssmith oscardssmith reopened this Oct 16, 2023
@11happy
Copy link
Contributor

11happy commented Dec 27, 2023

I am interested in the issue and have understood the full thread.
Here's my proposed solution :
cotpi:

function cotpi(x::T) where T<:IEEEFloat
    tanpi(x) == 0 && throw(DivideError("cotpi: cotangent is undefined for argument $x"))
    return 1.0 / tanpi(x)
end

secpi:

function secpi(x::T) where T<:IEEEFloat
    cospi(x) == 0 && throw(DivideError("secpi: secant is undefined for argument $x"))
    return 1.0 / cospi(x)
end

cscpi:

function cscpi(x::T) where T<:IEEEFloat
    sinpi(x) == 0 && throw(DivideError("cscpi: cosecant is undefined for argument $xr"))
    return 1.0 / sinpi(x)
end

If someone could please review these and provide any feedback or suggestions, it would be very helpful .
Thank you

@nsajko nsajko added the feature Indicates new feature / enhancement requests label Sep 24, 2024
@Youjack
Copy link

Youjack commented Nov 11, 2024

Current definition of tanpi produces NaN for large complex argument. For example,

julia> tanpi(500 * exp(im * 3pi/4))
NaN + NaN*im

The reason is that sinpi and cospi produce Inf with this argument,

julia> sinpi(500 * exp(im * 3pi/4))
Inf + Inf*im

julia> cospi(500 * exp(im * 3pi/4))
Inf - Inf*im

while tanpi is defined as

tanpi(x::Complex) = sinpi(x) / cospi(x) # Is there a better way to do this?

This NaN causes NaN in some special functions. For example,

julia> using SpecialFunctions

julia> digamma(500 * exp(im * 3pi/4))
NaN + NaN*im

See the related issue in SpecialFunctions.jl repo for details.

A possible way to resolve this problem is to define tanpi as how cotpi is defined in mpmath:
https://github.com/mpmath/mpmath/blob/b600dbcabf4b7406a61e82b9e607f754a9f12ff9/mpmath/libfp.py#L153-L161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests maths Mathematical functions
Projects
None yet
Development

No branches or pull requests