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

tanpi produces NaN for large complex argument #57450

Open
Youjack opened this issue Feb 18, 2025 · 1 comment
Open

tanpi produces NaN for large complex argument #57450

Youjack opened this issue Feb 18, 2025 · 1 comment
Labels
complex Complex numbers maths Mathematical functions

Comments

@Youjack
Copy link

Youjack commented Feb 18, 2025

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.

Inspired by how cotpi is defined in mpmath:
https://github.com/mpmath/mpmath/blob/5e57cb8039a3a4c9f222901a5eb2e08c53b2b2f8/mpmath/libfp.py#L153-L161
I suggest defining tanpi as

tanpi(x::Complex) = imag(x) > 10 ? 1.0im : imag(x) < 10 ? -1.0im : sinpi(x) / cospi(x)

This issue is copied from #28943 (comment).

@nsajko nsajko added maths Mathematical functions complex Complex numbers labels Feb 18, 2025
@nsajko
Copy link
Contributor

nsajko commented Feb 18, 2025

Potentially related: #46315?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

2 participants