-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
BUG: Overflow in tan and tanh for large complex values #3010
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
9e7be8c
BUG: Overflow in tan and tanh for large complex values
ewmoore f442ead
STY: npymath: tabs -> spaces, long lines
ewmoore d62c590
ENH: Check for, and use all C99 complex functions if available
ewmoore af5ff55
ENH: npymath: Use a better complex division algorithm.
ewmoore e6b99c9
MAINT: umath: generate the ldexp and frexp ufuncs like all the others.
ewmoore 0fdaa54
MAINT: umath/npymath: Add ldexp and frexp to npymath
ewmoore 2d33f7c
ENH: npymath: handle clog edge cases more carefully.
ewmoore 50d6720
ENH: evaluate c99 complex funcs at build time
ewmoore d504ea1
BUG: NPY_LOGE2 is log(2)
ewmoore 5362b35
BUG: Test aginst +/-TANH_HUGE
ewmoore e61e74f
TST: check for tanh(1000+0j) == 1 + 0j etc.
ewmoore 852cd10
BUG: Fix up some corner cases in npy_cexp
ewmoore 0f57b42
MAINT: remove debug print statement
ewmoore eb06557
MAINT: fix two typos in test_c99complex.c
ewmoore 77705ab
ENH: Be more careful with large real parts in npy_cexp
ewmoore e574e29
ENH: Import the ccosh/ccos implementation from FreeBSD
ewmoore 3710481
ENH: Import the csinh/csin implementation from FreeBSD
ewmoore a837853
ENH: Import the catanh/catan implemenation from FreeBSD
ewmoore 36e0d8b
BUG: printf requires a literal for the first argument.
ewmoore 330b54a
BUG: Py3 '-2j' is (NZERO - 2j) causes test failures
ewmoore def6327
ENH: Import the cacos,casin,cacosh,casinh implemenation from FreeBSD
ewmoore c8f13ee
TST: Enable signed zero branch cut tests
ewmoore eab5228
BUG: fix cpow tests to match what npy_cpow does.
ewmoore 2b5f1ad
STY: long lines, whitespace
ewmoore 1fb1b74
MAINT: remove gccisms and unbreak MSVC build.
ewmoore be42cae
MAINT: fix ldexp/frexp changes to compile with MSVC.
ewmoore ac075b2
MAINT: npy_a(exp,log,sqrt,fabs) don't really exist.
ewmoore 79afefd
ENH: Run the test_c99complex.c tests with the test suite too.
ewmoore cfe9fcc
BUG: Fix tests to compile under MSVC
ewmoore cbc97fa
MAINT: pick sunmath.h from npy_math.h
ewmoore 863d97d
MAINT: Update note about FreeBSD.
ewmoore 2348023
MAINT: make sure npy_math builds when long double is double double
ewmoore 5771dae
MAINT: move npy_(get,clear)_floatstatus() to their own file.
ewmoore 566c15e
BUG Use the floating point status functions in build time tests
ewmoore 503047b
MAINT: move test_c99complex.c to npymath
ewmoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
BUG: Overflow in tan and tanh for large complex values
gh-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.
- Loading branch information
commit 9e7be8cfe8f8e734ed3af3cbd50d9caa2ad45dc9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| * Most of the code is taken from the msun library in FreeBSD (HEAD @ 30th June | ||
| * 2009), under the following license: | ||
| * | ||
| * Copyright (c) 2007 David Schultz <[email protected]> | ||
| * Copyright (c) 2007, 2011 David Schultz <[email protected]> | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
|
|
@@ -227,6 +227,123 @@ | |
| return npy_cpack@c@(npy_sin@c@(x) * npy_cosh@c@(y), npy_cos@c@(x) * npy_sinh@c@(y)); | ||
| } | ||
| #endif | ||
|
|
||
| #ifndef HAVE_CTAN@C@ | ||
| @ctype@ npy_ctan@c@(@ctype@ z) | ||
| { | ||
| /* ctan(z) = -I * ctanh(I * z) */ | ||
| z = npy_ctanh@c@(npy_cpack@c@(-npy_cimag@c@(z), npy_creal@c@(z))); | ||
| return (npy_cpack@c@(npy_cimag@c@(z), -npy_creal@c@(z))); | ||
| } | ||
| #endif | ||
|
|
||
| #ifndef HAVE_CTANH@C@ | ||
| /* | ||
| * Taken from the msun library in FreeBSD, rev 226600. | ||
| * | ||
| * Hyperbolic tangent of a complex argument z = x + i y. | ||
| * | ||
| * The algorithm is from: | ||
| * | ||
| * W. Kahan. Branch Cuts for Complex Elementary Functions or Much | ||
| * Ado About Nothing's Sign Bit. In The State of the Art in | ||
| * Numerical Analysis, pp. 165 ff. Iserles and Powell, eds., 1987. | ||
| * | ||
| * Method: | ||
| * | ||
| * Let t = tan(x) | ||
| * beta = 1/cos^2(y) | ||
| * s = sinh(x) | ||
| * rho = cosh(x) | ||
| * | ||
| * We have: | ||
| * | ||
| * tanh(z) = sinh(z) / cosh(z) | ||
| * | ||
| * sinh(x) cos(y) + i cosh(x) sin(y) | ||
| * = --------------------------------- | ||
| * cosh(x) cos(y) + i sinh(x) sin(y) | ||
| * | ||
| * cosh(x) sinh(x) / cos^2(y) + i tan(y) | ||
| * = ------------------------------------- | ||
| * 1 + sinh^2(x) / cos^2(y) | ||
| * | ||
| * beta rho s + i t | ||
| * = ---------------- | ||
| * 1 + beta s^2 | ||
| * | ||
| * Modifications: | ||
| * | ||
| * I omitted the original algorithm's handling of overflow in tan(x) after | ||
| * verifying with nearpi.c that this can't happen in IEEE single or double | ||
| * precision. I also handle large x differently. | ||
| */ | ||
|
|
||
| #define TANH_HUGE 22.0 | ||
| #define TANHF_HUGE 11.0F | ||
| #define TANHL_HUGE 42.0L | ||
|
|
||
| @ctype@ npy_ctanh@c@(@ctype@ z) | ||
| { | ||
| @type@ x, y; | ||
| @type@ t, beta, s, rho, denom; | ||
|
|
||
| x = npy_creal@c@(z); | ||
| y = npy_cimag@c@(z); | ||
|
|
||
| /* | ||
| * ctanh(NaN + i 0) = NaN + i 0 | ||
| * | ||
| * ctanh(NaN + i y) = NaN + i NaN for y != 0 | ||
| * | ||
| * The imaginary part has the sign of x*sin(2*y), but there's no | ||
| * special effort to get this right. | ||
| * | ||
| * ctanh(+-Inf +- i Inf) = +-1 +- 0 | ||
| * | ||
| * ctanh(+-Inf + i y) = +-1 + 0 sin(2y) for y finite | ||
| * | ||
| * The imaginary part of the sign is unspecified. This special | ||
| * case is only needed to avoid a spurious invalid exception when | ||
| * y is infinite. | ||
| */ | ||
| if (!npy_isfinite(x)) { | ||
| if (npy_isnan(x)) | ||
| return npy_cpack@c@(x, (y == 0 ? y : x * y)); | ||
| return npy_cpack@c@(npy_copysign@c@(1,x), | ||
| npy_copysign@c@(0, npy_isinf(y) ? y : npy_sin@c@(y) * npy_cos@c@(y))); | ||
| } | ||
|
|
||
| /* | ||
| * ctanh(x + i NAN) = NaN + i NaN | ||
| * ctanh(x +- i Inf) = NaN + i NaN | ||
| */ | ||
| if (!npy_isfinite(y)) | ||
| return (npy_cpack@c@(y - y, y - y)); | ||
|
|
||
| /* | ||
| * ctanh(+-huge + i +-y) ~= +-1 +- i 2sin(2y)/exp(2x), using the | ||
| * approximation sinh^2(huge) ~= exp(2*huge) / 4. | ||
| * We use a modified formula to avoid spurious overflow. | ||
| */ | ||
| if (x >= TANH@C@_HUGE) { | ||
| @type@ exp_mx = npy_exp@c@(-npy_fabs@c@(x)); | ||
| return (npy_cpack@c@(npy_copysign@c@(1, x), | ||
| 4 * npy_sin@c@(y) * npy_cos@c@(y) * exp_mx * exp_mx)); | ||
| } | ||
|
|
||
| /* Kahan's algorithm */ | ||
| t = npy_tan@c@(y); | ||
| beta = 1.0 + t * t; /* = 1 / cos^2(y) */ | ||
| s = npy_sinh@c@(x); | ||
| rho = npy_sqrt@c@(1 + s * s); /* = cosh(x) */ | ||
| denom = 1 + beta * s * s; | ||
| return (npy_cpack@c@((beta * rho * s) / denom, t / denom)); | ||
| } | ||
| #undef TANH_HUGE | ||
| #undef TANHF_HUGE | ||
| #undef TANHL_HUGE | ||
| #endif | ||
| /**end repeat**/ | ||
|
|
||
| /*========================================================== | ||
|
|
@@ -254,8 +371,8 @@ | |
| /**end repeat1**/ | ||
|
|
||
| /**begin repeat1 | ||
| * #kind = cexp,clog,csqrt,ccos,csin# | ||
| * #KIND = CEXP,CLOG,CSQRT,CCOS,CSIN# | ||
| * #kind = cexp,clog,csqrt,ccos,csin,ctan,ctanh# | ||
| * #KIND = CEXP,CLOG,CSQRT,CCOS,CSIN,CTAN,CTANH# | ||
| */ | ||
| #ifdef HAVE_@KIND@@C@ | ||
| @ctype@ npy_@kind@@c@(@ctype@ z) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should perhaps be updated.