Skip to content

Commit f53ab41

Browse files
committed
BUG: fix non-c99 fallback for np.inf input to log1p/expm1
closes numpygh-4225
1 parent fb91c03 commit f53ab41

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎doc/release/1.8.1-notes.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ Issues fixed
4444
* gh-3175: segmentation fault with numpy.array() from bytearray
4545
* gh-4266: histogramdd - wrong result for entries very close to last boundary
4646
* gh-4408: Fix stride_stricks.as_strided function for object arrays
47+
* gh-4225: fix log1p and exmp1 return for np.inf on windows compiler builds

‎numpy/core/src/npymath/npy_math.c.src‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,38 @@
6565
#ifndef HAVE_EXPM1
6666
double npy_expm1(double x)
6767
{
68-
const double u = npy_exp(x);
69-
70-
if (u == 1.0) {
68+
if (npy_isinf(x) && x > 0) {
7169
return x;
72-
} else if (u - 1.0 == -1.0) {
73-
return -1;
74-
} else {
75-
return (u - 1.0) * x/npy_log(u);
70+
}
71+
else {
72+
const double u = npy_exp(x);
73+
74+
if (u == 1.0) {
75+
return x;
76+
} else if (u - 1.0 == -1.0) {
77+
return -1;
78+
} else {
79+
return (u - 1.0) * x/npy_log(u);
80+
}
7681
}
7782
}
7883
#endif
7984

8085
#ifndef HAVE_LOG1P
8186
double npy_log1p(double x)
8287
{
83-
const double u = 1. + x;
84-
const double d = u - 1.;
85-
86-
if (d == 0) {
88+
if (npy_isinf(x) && x > 0) {
8789
return x;
88-
} else {
89-
return npy_log(u) * x / d;
90+
}
91+
else {
92+
const double u = 1. + x;
93+
const double d = u - 1.;
94+
95+
if (d == 0) {
96+
return x;
97+
} else {
98+
return npy_log(u) * x / d;
99+
}
90100
}
91101
}
92102
#endif

‎numpy/core/tests/test_umath.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,28 @@ def test_log1p(self):
303303
assert_almost_equal(ncu.log1p(0.2), ncu.log(1.2))
304304
assert_almost_equal(ncu.log1p(1e-6), ncu.log(1+1e-6))
305305

306+
def test_special(self):
307+
assert_equal(ncu.log1p(np.nan), np.nan)
308+
assert_equal(ncu.log1p(np.inf), np.inf)
309+
with np.errstate(divide="ignore"):
310+
assert_equal(ncu.log1p(-1.), -np.inf)
311+
with np.errstate(invalid="ignore"):
312+
assert_equal(ncu.log1p(-2.), np.nan)
313+
assert_equal(ncu.log1p(-np.inf), np.nan)
314+
306315

307316
class TestExpm1(TestCase):
308317
def test_expm1(self):
309318
assert_almost_equal(ncu.expm1(0.2), ncu.exp(0.2)-1)
310319
assert_almost_equal(ncu.expm1(1e-6), ncu.exp(1e-6)-1)
311320

321+
def test_special(self):
322+
assert_equal(ncu.expm1(np.inf), np.inf)
323+
assert_equal(ncu.expm1(0.), 0.)
324+
assert_equal(ncu.expm1(-0.), -0.)
325+
assert_equal(ncu.expm1(np.inf), np.inf)
326+
assert_equal(ncu.expm1(-np.inf), -1.)
327+
312328

313329
class TestHypot(TestCase, object):
314330
def test_simple(self):

0 commit comments

Comments
 (0)