Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Simplified fix, Overflow in tan and tanh for large complex values. #5518

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

Merged
merged 7 commits into from
Feb 13, 2015

Conversation

charris
Copy link
Member

@charris charris commented Jan 29, 2015

Simplified and rebased version of #3010.

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?

This PR also uses more of the system complex functions if they are available.

Closes #2321.

Set the HAVE_XXXX macros for all of the following functions.

    csin,   ccos,   ctan
    casin,  cacos,  catan
    csinh,  ccosh,  ctanh
    casinh, cacosh, catanh
    cexp,   clog,   cpow
    cabs,   csqrt,  carg
    cimag,  creal,  conj,
    cproj

The macros do not need to be used, but it is good to have all of
them available.
@charris
Copy link
Member Author

charris commented Jan 29, 2015

This PR needs some style cleanups, but I've put those into a separate PR to keep this easier to review. Except for some cleanups that were already made. Same with creating new files and moving functions around.

The cproj function is in complex.h and we don't have a matching ufunc. It would probably be good to add that function, and maybe conj also just so the library is complete.

As far as testing the numpy fallback functions, I'm inclined to add a macro that will force the numpy versions to be used, so they can be tested with the python tests. Suggestions for alternatives are welcome.

Small changes in function values are likely to result, so this should probably be mentioned in the release notes if accepted.

@charris
Copy link
Member Author

charris commented Jan 29, 2015

Looks like configuration testing is needed. Bummer.

@charris charris closed this Jan 29, 2015
@charris charris changed the title BUG: Overflow in tan and tanh for large complex values. BUG: Simplified fix, Overflow in tan and tanh for large complex values. Jan 29, 2015
@charris charris reopened this Jan 30, 2015
@charris charris force-pushed the simple-gh-3010 branch 3 times, most recently from cd0feca to 2010c30 Compare January 30, 2015 19:51
@charris
Copy link
Member Author

charris commented Jan 30, 2015

Still playing with this.

@@ -258,8 +261,11 @@ float npy_copysignf(float x, float y);
float npy_nextafterf(float x, float y);
float npy_spacingf(float x);

float npy_ldexpf(float x, int exp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already in master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks.

@ewmoore
Copy link
Contributor

ewmoore commented Feb 13, 2015

This looks good to me. Thanks for taking the time to clean this up. I allowed my pull request to get way out of hand, but I think parts of this are still good improvements.

ewmoore and others added 6 commits February 13, 2015 09:24
The new functions added:

    npy_ctan,
    npy_cacos, npy_casin, npy_catan
    npy_ccosh, npy_csinh, npy_ctanh
    npy_cacosh, npy_casinh, npy_catanh
This should clear up such things as

>>> np.tanh(372 + 1j)
(nan+nan*j)
The test for precision needed to have difference changed from 2*eps to
2.1*eps for the system supplied sinh.
Numpy computes small integer powers by multiplication, this leads to
different results than the system cpow for such integers. In particular,
cpow((0 + inf*1j), 2) returns (-inf + 0j) rather than (-inf + nanj). The
system version probably makes more sense, but better to keep
compatibility at this point. We could fix our results using cproj.
The library functions casin, casinh, catan, catanh are inaccurate
in glibc version < 2.16, so use the numpy fallback functions for
those.
Document the increased use of the system library for C99 complex
functions and the addition of improved fallback functions for

* npy_ctan,
* npy_cacos, npy_casin, npy_catan
* npy_ccosh, npy_csinh, npy_ctanh,
* npy_cacosh, npy_casinh, npy_catanh
charris added a commit that referenced this pull request Feb 13, 2015
BUG: Simplified fix, Overflow in tan and tanh for large complex values.
@charris charris merged commit 35d01b2 into numpy:master Feb 13, 2015
@charris charris deleted the simple-gh-3010 branch February 13, 2015 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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