mirror of
https://github.com/python/cpython.git
synced 2025-07-30 14:44:10 +00:00
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
negative; fix to return an (appropriately signed) zero instead.
This commit is contained in:
parent
8bd5334a9e
commit
f8476c1573
3 changed files with 78 additions and 13 deletions
|
@ -374,6 +374,24 @@ class MathTests(unittest.TestCase):
|
||||||
self.assertEquals(math.ldexp(NINF, -213), NINF)
|
self.assertEquals(math.ldexp(NINF, -213), NINF)
|
||||||
self.assert_(math.isnan(math.ldexp(NAN, 0)))
|
self.assert_(math.isnan(math.ldexp(NAN, 0)))
|
||||||
|
|
||||||
|
# large second argument
|
||||||
|
for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
|
||||||
|
self.assertEquals(math.ldexp(INF, -n), INF)
|
||||||
|
self.assertEquals(math.ldexp(NINF, -n), NINF)
|
||||||
|
self.assertEquals(math.ldexp(1., -n), 0.)
|
||||||
|
self.assertEquals(math.ldexp(-1., -n), -0.)
|
||||||
|
self.assertEquals(math.ldexp(0., -n), 0.)
|
||||||
|
self.assertEquals(math.ldexp(-0., -n), -0.)
|
||||||
|
self.assert_(math.isnan(math.ldexp(NAN, -n)))
|
||||||
|
|
||||||
|
self.assertRaises(OverflowError, math.ldexp, 1., n)
|
||||||
|
self.assertRaises(OverflowError, math.ldexp, -1., n)
|
||||||
|
self.assertEquals(math.ldexp(0., n), 0.)
|
||||||
|
self.assertEquals(math.ldexp(-0., n), -0.)
|
||||||
|
self.assertEquals(math.ldexp(INF, n), INF)
|
||||||
|
self.assertEquals(math.ldexp(NINF, n), NINF)
|
||||||
|
self.assert_(math.isnan(math.ldexp(NAN, n)))
|
||||||
|
|
||||||
def testLog(self):
|
def testLog(self):
|
||||||
self.assertRaises(TypeError, math.log)
|
self.assertRaises(TypeError, math.log)
|
||||||
self.ftest('log(1/e)', math.log(1/math.e), -1)
|
self.ftest('log(1/e)', math.log(1/math.e), -1)
|
||||||
|
|
|
@ -23,6 +23,11 @@ Extension Modules
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #2487: change the semantics of math.ldexp(x, n) when n is too
|
||||||
|
large to fit in a C long. ldexp(x, n) now returns a zero (with
|
||||||
|
suitable sign) if n is large and negative; previously, it raised
|
||||||
|
OverflowError.
|
||||||
|
|
||||||
- The toaiff module has been deprecated for removal in Python 3.0.
|
- The toaiff module has been deprecated for removal in Python 3.0.
|
||||||
|
|
||||||
- The test.testall module has been deprecated for removal in Python 3.0.
|
- The test.testall module has been deprecated for removal in Python 3.0.
|
||||||
|
|
|
@ -349,22 +349,64 @@ static PyObject *
|
||||||
math_ldexp(PyObject *self, PyObject *args)
|
math_ldexp(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
double x, r;
|
double x, r;
|
||||||
int exp;
|
PyObject *oexp;
|
||||||
if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
|
long exp;
|
||||||
|
if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
if (PyLong_Check(oexp)) {
|
||||||
|
/* on overflow, replace exponent with either LONG_MAX
|
||||||
|
or LONG_MIN, depending on the sign. */
|
||||||
|
exp = PyLong_AsLong(oexp);
|
||||||
|
if (exp == -1 && PyErr_Occurred()) {
|
||||||
|
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
|
||||||
|
if (Py_SIZE(oexp) < 0) {
|
||||||
|
exp = LONG_MIN;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
exp = LONG_MAX;
|
||||||
|
}
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* propagate any unexpected exception */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (PyInt_Check(oexp)) {
|
||||||
|
exp = PyInt_AS_LONG(oexp);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"Expected an int or long as second argument "
|
||||||
|
"to ldexp.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x == 0. || !Py_IS_FINITE(x)) {
|
||||||
|
/* NaNs, zeros and infinities are returned unchanged */
|
||||||
|
r = x;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
PyFPE_START_PROTECT("in math_ldexp", return 0)
|
} else if (exp > INT_MAX) {
|
||||||
r = ldexp(x, exp);
|
/* overflow */
|
||||||
PyFPE_END_PROTECT(r)
|
r = copysign(Py_HUGE_VAL, x);
|
||||||
if (Py_IS_FINITE(x) && Py_IS_INFINITY(r))
|
|
||||||
errno = ERANGE;
|
errno = ERANGE;
|
||||||
/* Windows MSVC8 sets errno = EDOM on ldexp(NaN, i);
|
} else if (exp < INT_MIN) {
|
||||||
we unset it to avoid raising a ValueError here. */
|
/* underflow to +-0 */
|
||||||
if (errno == EDOM)
|
r = copysign(0., x);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
} else {
|
||||||
|
errno = 0;
|
||||||
|
PyFPE_START_PROTECT("in math_ldexp", return 0);
|
||||||
|
r = ldexp(x, (int)exp);
|
||||||
|
PyFPE_END_PROTECT(r);
|
||||||
|
if (Py_IS_INFINITY(r))
|
||||||
|
errno = ERANGE;
|
||||||
|
}
|
||||||
|
|
||||||
if (errno && is_error(r))
|
if (errno && is_error(r))
|
||||||
return NULL;
|
return NULL;
|
||||||
else
|
|
||||||
return PyFloat_FromDouble(r);
|
return PyFloat_FromDouble(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue