Skip to content

Leave sign of 0 unspecified for tanh inf + yj #1006

Open

Description

@ndgrigorian

The array API spec states, for tanh complex special cases

If a is +infinity and b is a positive (i.e., greater than 0) finite number, the result is 1 + 0j

from open-std pages describing ctanh

— ctanh(+∞ + iy) returns 1 + i0sin(2y), for positive-signed finite y.

This matches up to the math (we can derive this from tanh(x + iy) taking the limit as x approaches infinity and constraining y to be finite), and gives us 1 ± 0i, with the sign being inherited from sin(2y) if we care about signed zeros, rather than positive zero in all cases.

This was brought up because this special case is tested by the array API tests, which causes dpnp to fail the test.

Activity

  1. kgryte commented on May 15, 2026

    @kgryte
    Contributor

    Thanks for raising this issue, @ndgrigorian. For complex number special handling, we deferred to behavior described in C99: https://en.cppreference.com/c/numeric/complex/ctanh. Namely,

    If z is +∞+yi (for any finite positive y), the result is 1+0i
    
  2. ndgrigorian commented on May 15, 2026

    @ndgrigorian
    Author

    Thanks for raising this issue, @ndgrigorian. For complex number special handling, we deferred to behavior described in C99: https://en.cppreference.com/c/numeric/complex/ctanh. Namely,

    If z is +∞+yi (for any finite positive y), the result is 1+0i
    

    yes, but I would argue, that the cppreference page is a bit misleading as well. The ISO standard (final draft) text for C99 also uses sin(2y).

    NumPy also gives the same:

    import numpy as np
    
    print([np.tanh(complex(np.inf, n)) for n in range(4)])
    # [np.complex128(1+0j), np.complex128(1+0j), np.complex128(1-0j), np.complex128(1-0j)]
    

    at least for conda-forge NumPy, there can be discrepancies on PyPI with complex edge cases I've found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions