diff options
-rw-r--r-- | numpy/core/src/npymath/npy_math.c.src | 48 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 4 |
2 files changed, 32 insertions, 20 deletions
diff --git a/numpy/core/src/npymath/npy_math.c.src b/numpy/core/src/npymath/npy_math.c.src index 9f56b08a9..ba5c58fb4 100644 --- a/numpy/core/src/npymath/npy_math.c.src +++ b/numpy/core/src/npymath/npy_math.c.src @@ -414,7 +414,7 @@ double npy_log2(double x) { return (@type@) npy_ldexp((double)x, exp); } -#endif +#endif #ifdef frexp@c@ #undef frexp@c@ @@ -477,7 +477,7 @@ double npy_log2(double x) { return ldexp@c@(x, exp); } -#endif +#endif #ifdef HAVE_FREXP@C@ @type@ npy_frexp@c@(@type@ x, int* exp) @@ -545,31 +545,43 @@ double npy_log2(double x) @type@ npy_logaddexp@c@(@type@ x, @type@ y) { - const @type@ tmp = x - y; - if (tmp > 0) { - return x + npy_log1p@c@(npy_exp@c@(-tmp)); - } - else if (tmp <= 0) { - return y + npy_log1p@c@(npy_exp@c@(tmp)); + if (x == y) { + /* Handles infinities of the same sign without warnings */ + return x + LOGE2; } else { - /* NaNs, or infinities of the same sign involved */ - return x + y; + const @type@ tmp = x - y; + if (tmp > 0) { + return x + npy_log1p@c@(npy_exp@c@(-tmp)); + } + else if (tmp <= 0) { + return y + npy_log1p@c@(npy_exp@c@(tmp)); + } + else { + /* NaNs */ + return tmp; + } } } @type@ npy_logaddexp2@c@(@type@ x, @type@ y) { - const @type@ tmp = x - y; - if (tmp > 0) { - return x + npy_log2_1p@c@(npy_exp2@c@(-tmp)); - } - else if (tmp <= 0) { - return y + npy_log2_1p@c@(npy_exp2@c@(tmp)); + if (x == y) { + /* Handles infinities of the same sign without warnings */ + return x + 1; } else { - /* NaNs, or infinities of the same sign involved */ - return x + y; + const @type@ tmp = x - y; + if (tmp > 0) { + return x + npy_log2_1p@c@(npy_exp2@c@(-tmp)); + } + else if (tmp <= 0) { + return y + npy_log2_1p@c@(npy_exp2@c@(tmp)); + } + else { + /* NaNs */ + return tmp; + } } } diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 4bbf5601a..f8d6520d7 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -262,7 +262,7 @@ class TestLogAddExp2(_FilterInvalids): x = [inf, -inf, inf, -inf, inf, 1, -inf, 1] y = [inf, inf, -inf, -inf, 1, inf, 1, -inf] z = [inf, inf, inf, -inf, inf, inf, 1, 1] - with np.errstate(invalid='ignore'): + with np.errstate(invalid='raise'): for dt in ['f', 'd', 'g'] : logxf = np.array(x, dtype=dt) logyf = np.array(y, dtype=dt) @@ -325,7 +325,7 @@ class TestLogAddExp(_FilterInvalids): x = [inf, -inf, inf, -inf, inf, 1, -inf, 1] y = [inf, inf, -inf, -inf, 1, inf, 1, -inf] z = [inf, inf, inf, -inf, inf, inf, 1, 1] - with np.errstate(invalid='ignore'): + with np.errstate(invalid='raise'): for dt in ['f', 'd', 'g'] : logxf = np.array(x, dtype=dt) logyf = np.array(y, dtype=dt) |