-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
BUG: Overflow in tan and tanh for large complex values #5510
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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 #2321.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,6 +209,32 @@ def check_prec(prec): | |
| else: | ||
| priv.extend([(fname2def(f), 1) for f in flist]) | ||
|
|
||
| flist = [f + prec for f in C99_COMPLEX_FUNCS_CHECKED] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes me wonder if these tests shouldn't be done in python.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was some discussion of using ctypes to call these functions from python. I didn't go down that route since we then need to find libc ourselves, but doing it this way allowed the tool chain to find it for us. I suppose we could make a tiny module that simply exposes them to python and then use that for the tests. |
||
| decl = dict([(f, True) for f in flist]) | ||
| exists = [] | ||
| if not config.check_funcs_once(flist, call=decl, decl=decl, | ||
| libraries=mathlibs): | ||
| for f in C99_COMPLEX_FUNCS_CHECKED: | ||
| if config.check_func(f + prec, call=True, decl=True, | ||
| libraries=mathlibs): | ||
| exists.append(f) | ||
| else: | ||
| exists.extend(C99_COMPLEX_FUNCS_CHECKED) | ||
|
|
||
| if len(exists) > 0: | ||
| fp = open(join('.', 'numpy', 'core', 'src', 'npymath', | ||
| 'test_c99complex.c'), 'r') | ||
| obody = fp.read() | ||
| fp.close() | ||
| precname = {'f':'FLOAT', '':'DOUBLE', 'l':'LONGDOUBLE'}[prec] | ||
| for f in exists: | ||
| body = obody.replace('PYTESTPRECISION', precname) \ | ||
| .replace('PYTESTFUNC', f.upper()) | ||
| inc_dir = join('.', 'numpy', 'core', 'src', 'npymath') | ||
| if config.try_run(body, libraries=mathlibs, | ||
| include_dirs=[inc_dir]): | ||
| priv.append((fname2def(f + prec), 1)) | ||
|
|
||
| check_prec('') | ||
| check_prec('f') | ||
| check_prec('l') | ||
|
|
@@ -683,7 +709,8 @@ def get_mathlib_info(*args): | |
| npymath_sources = [join('src', 'npymath', 'npy_math.c.src'), | ||
| join('src', 'npymath', 'ieee754.c.src'), | ||
| join('src', 'npymath', 'npy_math_complex.c.src'), | ||
| join('src', 'npymath', 'halffloat.c')] | ||
| join('src', 'npymath', 'halffloat.c'), | ||
| join('src', 'npymath', 'fpstatus.c')] | ||
| config.add_installed_library('npymath', | ||
| sources=npymath_sources + [get_mathlib_info], | ||
| install_dir='lib') | ||
|
|
@@ -973,6 +1000,16 @@ def generate_umath_c(ext, build_dir): | |
| config.add_extension('operand_flag_tests', | ||
| sources = [join('src', 'umath', 'operand_flag_tests.c.src')]) | ||
|
|
||
| ####################################################################### | ||
| # npymath_tests module # | ||
| ####################################################################### | ||
|
|
||
| config.add_extension('npymath_tests', | ||
| sources = [join('src', 'npymath', 'npymath_tests.c')], | ||
| depends = ['test_c99complex.c'], | ||
| libraries = ['npymath'] | ||
| ) | ||
|
|
||
| config.add_data_dir('tests') | ||
| config.add_data_dir('tests/data') | ||
|
|
||
|
|
||
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.
npy_ldexp and npy_frexp in all three flavors already exist in master. These came in as part of #4852, which pulled the ldexp and frexp changes from this.