[Numpy-discussion] Error in tanh for large complex argument

Pauli Virtanen pav at iki.fi
Thu Jan 27 06:11:54 EST 2011

Thu, 27 Jan 2011 11:40:00 +0100, Mark Bakker wrote:
> Not for large complex values:
> In [85]: tanh(1000+0j)
> Out[85]: (nan+nan*j)

Yep, it's a bug. Care to file a ticket?

The implementation is just sinh/cosh, which overflows. 
The fix is to provide an asymptotic expansion (sgn Re z),
although around the imaginary axis the switch is perhaps
somewhat messy to handle.

OTOH, the glibc-provided C99 function doesn't fare too well either:

#include <math.h>
#include <complex.h>
#include <stdio.h>

int main()
    complex double z = 1000;
    double x, y;
    z = ctanh(z); x = creal(z); y = cimag(z);
    printf("%g %g\n", x, y);
    return 0;

### -> Prints 0 0  on glibc 2.12.1

More information about the NumPy-Discussion mailing list