8000 MAINT: core: Restore float128 power on Mac OSX. · numpy/numpy@9875e47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9875e47

Browse files
MAINT: core: Restore float128 power on Mac OSX.
The use of the math library powl was blacklisted on Mac OSX (gh-8318) because powl(0, y) would trigger a divide-by-zero warning even when y > 0 (issue gh-8307). This change creates npy_powl on OSX that avoids the spurious warning by not calling powl when x = 0 and y > 0. The change in npy_math_internal.h.src moves the handling of pow out of a template repeat-loop, so powl and powf are handled individually. This allows the creation of npy_powl to be specialized when NPY_OS_DARWIN is defined. Closes gh-8608.
1 parent 5bf6d68 commit 9875e47

File tree

2 files changed

+49
-2
lines ch 10000 anged

2 files changed

+49
-2
lines changed

numpy/core/src/npymath/npy_math_internal.h.src

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
*/
5656
#include "npy_math_private.h"
5757

58+
5859
/*
5960
*****************************************************************************
6061
** BASIC MATH FUNCTIONS **
@@ -398,8 +399,8 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
398399
/**end repeat1**/
399400

400401
/**begin repeat1
401-
* #kind = atan2,hypot,pow,fmod,copysign#
402-
* #KIND = ATAN2,HYPOT,POW,FMOD,COPYSIGN#
402+
* #kind = atan2,hypot,fmod,copysign#
403+
* #KIND = ATAN2,HYPOT,FMOD,COPYSIGN#
403404
*/
404405
#ifdef @kind@@c@
405406
#undef @kind@@c@
@@ -448,6 +449,37 @@ NPY_INPLACE @type@ npy_frexp@c@(@type@ x, int* exp)
448449
/**end repeat**/
449450

450451

452+
#ifdef powf
453+
#undef powf
454+
#endif
455+
#ifndef HAVE_POWF
456+
NPY_INPLACE npy_float npy_powf(npy_float x, npy_float y)
457+
{
458+
return (npy_float) npy_pow((double) x, (double) y);
459+
}
460+
#endif
461+
462+
#ifdef powl
463+
#undef powl
464+
#endif
465+
#ifndef HAVE_POWL
466+
#if defined(NPY_OS_DARWIN)
467+
NPY_INPLACE npy_longdouble npy_powl(npy_longdouble x, npy_longdouble y)
468+
{
469+
if (x == 0 && y > 0) {
470+
return 0.0L;
471+
}
472+
return powl(x, y);
473+
}
474+
#else
475+
// Fall back to the double precision version.
476+
NPY_INPLACE npy_longdouble npy_powl(npy_longdouble x, npy_longdouble y)
477+
{
478+
return (npy_longdouble) npy_pow((npy_double) x, (npy_double) y);
479+
}
480+
#endif
481+
#endif
482+
451483
/*
452484
* Decorate all the math functions which are available on the current platform
453485
*/

numpy/core/tests/test_umath.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,21 @@ def test_integer_to_negative_power(self):
546546
assert_raises(ValueError, np.power, one, b)
547547
assert_raises(ValueError, np.power, one, minusone)
548548

549+
@pytest.mark.skipif(not hasattr(np, 'float128'),
550+
reason='test requires float128')
551+
def test_float128(self):
552+
x = np.array([0, 1, 2], dtype=np.float128)
553+
# On Mac OSX, this would trigger a divide-by-zero warning.
554+
y = np.power(x, 4)
555+
assert_array_equal(y, [0.0, 1.0, 16.0])
556+
557+
@pytest.mark.skipif(not sys.platform == 'darwin',
558+
reason='only test on Mac OSX (i.e. darwin)')
559+
def test_float128_precision(self):
560+
# Check that we are using more than double precision.
561+
v = np.float128(2) ** 16383
562+
assert_allclose(v, np.float128('5.9486574767861588254e+4931'))
563+
549564

550565
class TestFloat_power:
551566
def test_type_conversion(self):

0 commit comments

Comments
 (0)
0