Skip to content

Commit 822e129

Browse files
author
mark.dickinson
committed
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
negative; fix to return an (appropriately signed) zero instead. git-svn-id: http://svn.python.org/projects/python/trunk@62948 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 55780fb commit 822e129

3 files changed

Lines changed: 77 additions & 12 deletions

File tree

Lib/test/test_math.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,24 @@ def testLdexp(self):
374374
self.assertEquals(math.ldexp(NINF, -213), NINF)
375375
self.assert_(math.isnan(math.ldexp(NAN, 0)))
376376

377+
# large second argument
378+
for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
379+
self.assertEquals(math.ldexp(INF, -n), INF)
380+
self.assertEquals(math.ldexp(NINF, -n), NINF)
381+
self.assertEquals(math.ldexp(1., -n), 0.)
382+
self.assertEquals(math.ldexp(-1., -n), -0.)
383+
self.assertEquals(math.ldexp(0., -n), 0.)
384+
self.assertEquals(math.ldexp(-0., -n), -0.)
385+
self.assert_(math.isnan(math.ldexp(NAN, -n)))
386+
387+
self.assertRaises(OverflowError, math.ldexp, 1., n)
388+
self.assertRaises(OverflowError, math.ldexp, -1., n)
389+
self.assertEquals(math.ldexp(0., n), 0.)
390+
self.assertEquals(math.ldexp(-0., n), -0.)
391+
self.assertEquals(math.ldexp(INF, n), INF)
392+
self.assertEquals(math.ldexp(NINF, n), NINF)
393+
self.assert_(math.isnan(math.ldexp(NAN, n)))
394+
377395
def testLog(self):
378396
self.assertRaises(TypeError, math.log)
379397
self.ftest('log(1/e)', math.log(1/math.e), -1)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Extension Modules
2323
Library
2424
-------
2525

26+
- Issue #2487: change the semantics of math.ldexp(x, n) when n is too
27+
large to fit in a C long. ldexp(x, n) now returns a zero (with
28+
suitable sign) if n is large and negative; previously, it raised
29+
OverflowError.
30+
2631
- The toaiff module has been deprecated for removal in Python 3.0.
2732

2833
- The test.testall module has been deprecated for removal in Python 3.0.

Modules/mathmodule.c

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,23 +349,65 @@ static PyObject *
349349
math_ldexp(PyObject *self, PyObject *args)
350350
{
351351
double x, r;
352-
int exp;
353-
if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
352+
PyObject *oexp;
353+
long exp;
354+
if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
354355
return NULL;
355-
errno = 0;
356-
PyFPE_START_PROTECT("in math_ldexp", return 0)
357-
r = ldexp(x, exp);
358-
PyFPE_END_PROTECT(r)
359-
if (Py_IS_FINITE(x) && Py_IS_INFINITY(r))
356+
357+
if (PyLong_Check(oexp)) {
358+
/* on overflow, replace exponent with either LONG_MAX
359+
or LONG_MIN, depending on the sign. */
360+
exp = PyLong_AsLong(oexp);
361+
if (exp == -1 && PyErr_Occurred()) {
362+
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
363+
if (Py_SIZE(oexp) < 0) {
364+
exp = LONG_MIN;
365+
}
366+
else {
367+
exp = LONG_MAX;
368+
}
369+
PyErr_Clear();
370+
}
371+
else {
372+
/* propagate any unexpected exception */
373+
return NULL;
374+
}
375+
}
376+
}
377+
else if (PyInt_Check(oexp)) {
378+
exp = PyInt_AS_LONG(oexp);
379+
}
380+
else {
381+
PyErr_SetString(PyExc_TypeError,
382+
"Expected an int or long as second argument "
383+
"to ldexp.");
384+
return NULL;
385+
}
386+
387+
if (x == 0. || !Py_IS_FINITE(x)) {
388+
/* NaNs, zeros and infinities are returned unchanged */
389+
r = x;
390+
errno = 0;
391+
} else if (exp > INT_MAX) {
392+
/* overflow */
393+
r = copysign(Py_HUGE_VAL, x);
360394
errno = ERANGE;
361-
/* Windows MSVC8 sets errno = EDOM on ldexp(NaN, i);
362-
we unset it to avoid raising a ValueError here. */
363-
if (errno == EDOM)
395+
} else if (exp < INT_MIN) {
396+
/* underflow to +-0 */
397+
r = copysign(0., x);
364398
errno = 0;
399+
} else {
400+
errno = 0;
401+
PyFPE_START_PROTECT("in math_ldexp", return 0);
402+
r = ldexp(x, (int)exp);
403+
PyFPE_END_PROTECT(r);
404+
if (Py_IS_INFINITY(r))
405+
errno = ERANGE;
406+
}
407+
365408
if (errno && is_error(r))
366409
return NULL;
367-
else
368-
return PyFloat_FromDouble(r);
410+
return PyFloat_FromDouble(r);
369411
}
370412

371413
PyDoc_STRVAR(math_ldexp_doc,

0 commit comments

Comments
 (0)