Skip to content

tanpi produces NaN for large complex argument #57450

@Youjack

Description

@Youjack

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexComplex numbersmathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions