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

Error in tanh for large complex argument (Trac #1726) #2321

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 2 comments · Fixed by #5518
Closed

Error in tanh for large complex argument (Trac #1726) #2321

numpy-gitbot opened this issue Oct 19, 2012 · 2 comments · Fixed by #5518

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1726 on 2011-01-28 by trac user mbakker, assigned to unknown.

tanh computes nans for large complex argument:

In [85]: tanh(1000+0j)
Out[85]: (nan+nan*j)

while the correct answer is 1.0

Solution: Program tanh as:

tanh(z) = (1.0 - exp(-2.0_z)) / (1.0 + exp(-2.0_z))

@numpy-gitbot
Copy link
Author

trac user mbakker wrote on 2011-01-28

As Pauli pointed out the suggested solution has problems for z -> -inf.
Need to check what others do.

@numpy-gitbot
Copy link
Author

trac user mbakker wrote on 2012-04-07

tanh is computed correctly for large complex argument by cmath. We may look there for the correct approach.

ewmoore added a commit to ewmoore/numpy that referenced this issue Oct 2, 2013
numpygh-2321 (trac 1726).

np.tanh(1000+0j) gave nan+nan*j, should be 1.0+0j.  The
same bug was present in np.tan(0+1000j).

Bug fixed by replacing our complex tan and tanh implementation
with one from FreeBSD.
ewmoore added a commit to ewmoore/numpy that referenced this issue Oct 2, 2013
ewmoore added a commit to ewmoore/numpy that referenced this issue Feb 26, 2014
numpygh-2321 (trac 1726).

np.tanh(1000+0j) gave nan+nan*j, should be 1.0+0j.  The
same bug was present in np.tan(0+1000j).

Bug fixed by replacing our complex tan and tanh implementation
with one from FreeBSD.
ewmoore added a commit to ewmoore/numpy that referenced this issue Feb 26, 2014
charris pushed a commit to charris/numpy that referenced this issue Jan 27, 2015
np.tanh(1000+0j) gives nan+nan*j instead of 1.0+0j.
np.tan(0+1000j) gives  nan+nan*j instead of 1j.

I've imported the implementation for ctanh from FreeBSD's math library
which handles large arguments correctly and fixes this bug and the
equivalent bug in np.tan.

The problem here is that importing the function into npy_math_complex.c
and adding a config check causes us to use the implementation from glibc
on Linux, which also has this bug. Although it seems to have been fixed
in glibc last April
(http://sourceware.org/bugzilla/show_bug.cgi?id=11521).

I see several things that could be done here, the easiest is probably to
use our version of ctanh unconditionally. Although there are a multitude
of ways to go about that, for instance should I remove the
implementation from npy_math_complex.c and just place it directly in
umath/funcs.inc, where the buggy version was? Or should I just remove
the config check and add a note about it somewhere?

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