Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b82fedc

Browse files
committed
On int to the negative integral power, let float handle it instead of
raising an error. This was one of the two issues that the VPython folks were particularly problematic for their students. (The other one was integer division...) This implements (my) SF patch #440487.
1 parent e9880c8 commit b82fedc

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

Objects/intobject.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,11 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
510510
CONVERT_TO_LONG(v, iv);
511511
CONVERT_TO_LONG(w, iw);
512512
if (iw < 0) {
513-
if (iv)
514-
PyErr_SetString(PyExc_ValueError,
515-
"cannot raise integer to a negative power");
516-
else
517-
PyErr_SetString(PyExc_ZeroDivisionError,
518-
"cannot raise 0 to a negative power");
519-
return NULL;
513+
/* Return a float. This works because we know that
514+
this calls float_pow() which converts its
515+
arguments to double. */
516+
return PyFloat_Type.tp_as_number->nb_power(
517+
(PyObject *)v, (PyObject *)w, (PyObject *)z);
520518
}
521519
if ((PyObject *)z != Py_None) {
522520
CONVERT_TO_LONG(z, iz);

0 commit comments

Comments
 (0)