Skip to content

mp.tanh and mp.tan return incorrect results at complex infinities #781

@pearu

Description

@pearu

As in the title:

>>> mpmath.mp.tanh(mpmath.mp.mpc(mpmath.inf, mpmath.inf))  # expected (1+0j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tanh(mpmath.mp.mpc(mpmath.inf, -mpmath.inf))  # expected (1+0j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tanh(mpmath.mp.mpc(-mpmath.inf, mpmath.inf))  # expected (-1+0j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tanh(mpmath.mp.mpc(-mpmath.inf, -mpmath.inf))  # expected (-1+0j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tanh(mpmath.mp.mpc(mpmath.inf, 2))  # expected (1-0j)
mpc(real='nan', imag='0.0')
>>> mpmath.mp.tanh(mpmath.mp.mpc(-mpmath.inf, 2))  # expected (-1-0j)
mpc(real='nan', imag='0.0')
>>> mpmath.mp.tanh(mpmath.mp.mpc(0, -mpmath.inf))  # expected (nan+nanj)
mpc(real='0.0', imag='nan')

mpmath.fp.tanh returns expected results (except for the last sample).

>>> mpmath.mp.tan(mpmath.mp.mpc(mpmath.inf, mpmath.inf))  # expected (1j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(mpmath.inf, -mpmath.inf))  # expected (-1j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(-mpmath.inf, mpmath.inf))  # expected (1j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(-mpmath.inf, -mpmath.inf))  # expected (-1j)
mpc(real='nan', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(2, mpmath.inf))  # expected (-0+1j)
mpc(real='0.0', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(2, -mpmath.inf))  # expected (-0-1j)
mpc(real='0.0', imag='nan')
>>> mpmath.mp.tan(mpmath.mp.mpc(-mpmath.inf, 0))  # expected (nan+nanj)
mpc(real='nan', imag='0.0')

mpmath.fp.tan returns expected results (except for the last sample).

Expected values are defined in https://en.cppreference.com/w/cpp/numeric/complex/tanh and https://en.cppreference.com/w/cpp/numeric/complex/tan.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions