summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2000-10-06 00:36:09 (GMT)
committerTim Peters <tim.peters@gmail.com>2000-10-06 00:36:09 (GMT)
commitc54d19043a595679f253a55e46fda2910f513c52 (patch)
treea737fd6e07e1224f22837ac9822f36b5cdb1524d /Objects
parent4779a0a6fd444d594f1cbb992c636d66f59a5d1d (diff)
downloadcpython-c54d19043a595679f253a55e46fda2910f513c52.zip
cpython-c54d19043a595679f253a55e46fda2910f513c52.tar.gz
cpython-c54d19043a595679f253a55e46fda2910f513c52.tar.bz2
SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
raise ValueError. Checked in the patch as far as it went, but also changed all of ints, longs and floats to raise ZeroDivisionError instead when raising 0 to a negative number. This is what 754-inspired stds require, as the "true result" is an infinity obtained from finite operands, i.e. it's a singularity. Also changed float pow to not be so timid about using its square-and-multiply algorithm. Note that what math.pow does is unrelated to what builtin pow does, and will still vary by platform.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c63
-rw-r--r--Objects/intobject.c8
-rw-r--r--Objects/longobject.c8
3 files changed, 47 insertions, 32 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 17f70b2..d776147 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -449,20 +449,33 @@ float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z)
iv = v->ob_fval;
iw = ((PyFloatObject *)w)->ob_fval;
intw = (long)iw;
- if (iw == intw && -10000 < intw && intw < 10000) {
- /* Sort out special cases here instead of relying on pow() */
- if (intw == 0) { /* x**0 is 1, even 0**0 */
- PyFPE_START_PROTECT("pow", return 0)
- if ((PyObject *)z!=Py_None) {
- ix=fmod(1.0, z->ob_fval);
- if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
- }
- else ix=1.0;
- PyFPE_END_PROTECT(ix)
- return PyFloat_FromDouble(ix);
+
+ /* Sort out special cases here instead of relying on pow() */
+ if (iw == 0) { /* x**0 is 1, even 0**0 */
+ PyFPE_START_PROTECT("pow", return NULL)
+ if ((PyObject *)z != Py_None) {
+ ix = fmod(1.0, z->ob_fval);
+ if (ix != 0 && z->ob_fval < 0)
+ ix += z->ob_fval;
}
+ else
+ ix = 1.0;
+ PyFPE_END_PROTECT(ix)
+ return PyFloat_FromDouble(ix);
+ }
+ if (iv == 0.0) {
+ if (iw < 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "0.0 to a negative power");
+ return NULL;
+ }
+ return PyFloat_FromDouble(0.0);
+ }
+
+ if (iw == intw && intw > LONG_MIN) {
+ /* ruled out LONG_MIN because -LONG_MIN isn't representable */
errno = 0;
- PyFPE_START_PROTECT("pow", return 0)
+ PyFPE_START_PROTECT("pow", return NULL)
if (intw > 0)
ix = powu(iv, intw);
else
@@ -471,21 +484,13 @@ float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z)
}
else {
/* Sort out special cases here instead of relying on pow() */
- if (iv == 0.0) {
- if (iw < 0.0) {
- PyErr_SetString(PyExc_ValueError,
- "0.0 to a negative power");
- return NULL;
- }
- return PyFloat_FromDouble(0.0);
- }
if (iv < 0.0) {
PyErr_SetString(PyExc_ValueError,
"negative number to a float power");
return NULL;
}
errno = 0;
- PyFPE_START_PROTECT("pow", return 0)
+ PyFPE_START_PROTECT("pow", return NULL)
ix = pow(iv, iw);
PyFPE_END_PROTECT(ix)
}
@@ -495,13 +500,15 @@ float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z)
PyErr_SetFromErrno(PyExc_OverflowError);
return NULL;
}
- if ((PyObject *)z!=Py_None) {
- PyFPE_START_PROTECT("pow", return 0)
- ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
- if ( ix!=0 &&
- ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
- ix+=z->ob_fval;
- }
+ if ((PyObject *)z != Py_None) {
+ PyFPE_START_PROTECT("pow", return NULL)
+ ix = fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
+ if (ix != 0 &&
+ ((iv < 0 && z->ob_fval > 0) ||
+ (iv > 0 && z->ob_fval < 0)
+ )) {
+ ix += z->ob_fval;
+ }
PyFPE_END_PROTECT(ix)
}
return PyFloat_FromDouble(ix);
diff --git a/Objects/intobject.c b/Objects/intobject.c
index b88a05d..c9d1f6a 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -483,8 +483,12 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
iv = v->ob_ival;
iw = w->ob_ival;
if (iw < 0) {
- PyErr_SetString(PyExc_ValueError,
- "integer to the negative power");
+ if (iv)
+ PyErr_SetString(PyExc_ValueError,
+ "integer to a negative power");
+ else
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "0 to a negative power");
return NULL;
}
if ((PyObject *)z != Py_None) {
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 65dcaa0..bfb431f 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1244,8 +1244,12 @@ long_pow(PyLongObject *a, PyLongObject *b, PyLongObject *c)
size_b = b->ob_size;
if (size_b < 0) {
- PyErr_SetString(PyExc_ValueError,
- "long integer to the negative power");
+ if (a->ob_size)
+ PyErr_SetString(PyExc_ValueError,
+ "long integer to a negative power");
+ else
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "zero to a negative power");
return NULL;
}
z = (PyLongObject *)PyLong_FromLong(1L);