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.
This commit is contained in:
parent
4779a0a6fd
commit
c54d19043a
|
@ -2,44 +2,69 @@ import sys
|
|||
import test_support
|
||||
|
||||
def powtest(type):
|
||||
if (type!=float):
|
||||
if type != float:
|
||||
print " Testing 2-argument pow() function..."
|
||||
for i in range(-1000, 1000):
|
||||
if (pow(type(i),0)!=1):
|
||||
if pow(type(i), 0) != 1:
|
||||
raise ValueError, 'pow('+str(i)+',0) != 1'
|
||||
if (pow(type(i),1)!=type(i)):
|
||||
if pow(type(i), 1) != type(i):
|
||||
raise ValueError, 'pow('+str(i)+',1) != '+str(i)
|
||||
if (pow(type(0),1)!=type(0)):
|
||||
if pow(type(0), 1) != type(0):
|
||||
raise ValueError, 'pow(0,'+str(i)+') != 0'
|
||||
if (pow(type(1),1)!=type(1)):
|
||||
if pow(type(1), 1) != type(1):
|
||||
raise ValueError, 'pow(1,'+str(i)+') != 1'
|
||||
|
||||
|
||||
for i in range(-100, 100):
|
||||
if (pow(type(i),3)!=i*i*i):
|
||||
if pow(type(i), 3) != i*i*i:
|
||||
raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
|
||||
|
||||
pow2=1
|
||||
pow2 = 1
|
||||
for i in range(0,31):
|
||||
if (pow(2,i)!=pow2):
|
||||
if pow(2, i) != pow2:
|
||||
raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
|
||||
if (i!=30): pow2=pow2*2
|
||||
if i != 30 : pow2 = pow2*2
|
||||
|
||||
for othertype in int, long:
|
||||
for i in range(-10, 0) + range(1, 10):
|
||||
ii = type(i)
|
||||
for j in range(1, 11):
|
||||
jj = -othertype(j)
|
||||
try:
|
||||
pow(ii, jj)
|
||||
except ValueError:
|
||||
pass # taking an int to a neg int power should fail
|
||||
else:
|
||||
raise ValueError, "pow(%s, %s) did not fail" % (ii, jj)
|
||||
|
||||
for othertype in int, long, float:
|
||||
for i in range(1, 100):
|
||||
zero = type(0)
|
||||
exp = -othertype(i/10.0)
|
||||
if exp == 0:
|
||||
continue
|
||||
try:
|
||||
pow(zero, exp)
|
||||
except ZeroDivisionError:
|
||||
pass # taking zero to any negative exponent should fail
|
||||
else:
|
||||
raise ValueError, "pow(%s, %s) did not fail" % (zero, exp)
|
||||
|
||||
print " Testing 3-argument pow() function..."
|
||||
il, ih = -20, 20
|
||||
jl, jh = -5, 5
|
||||
kl, kh = -10, 10
|
||||
compare = cmp
|
||||
if (type==float):
|
||||
il=1
|
||||
if type == float:
|
||||
il = 1
|
||||
compare = test_support.fcmp
|
||||
elif (type==int):
|
||||
jl=0
|
||||
elif (type==long):
|
||||
jl,jh = 0, 15
|
||||
elif type == int:
|
||||
jl = 0
|
||||
elif type == long:
|
||||
jl, jh = 0, 15
|
||||
for i in range(il, ih+1):
|
||||
for j in range(jl,jh+1):
|
||||
for j in range(jl, jh+1):
|
||||
for k in range(kl, kh+1):
|
||||
if (k!=0):
|
||||
if k != 0:
|
||||
if compare(pow(type(i),j,k), pow(type(i),j)% type(k)):
|
||||
raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
|
||||
"," +str(k)+ ") != pow(" +str(i)+ "," + \
|
||||
|
@ -81,15 +106,15 @@ print
|
|||
for i in range(-10, 11):
|
||||
for j in range(0, 6):
|
||||
for k in range(-7, 11):
|
||||
if (j>=0 and k!=0):
|
||||
o=pow(i,j) % k
|
||||
n=pow(i,j,k)
|
||||
if (o!=n): print 'Integer mismatch:', i,j,k
|
||||
if (j>=0 and k<>0):
|
||||
o=pow(long(i),j) % k
|
||||
n=pow(long(i),j,k)
|
||||
if (o!=n): print 'Long mismatch:', i,j,k
|
||||
if (i>=0 and k<>0):
|
||||
o=pow(float(i),j) % k
|
||||
n=pow(float(i),j,k)
|
||||
if (o!=n): print 'Float mismatch:', i,j,k
|
||||
if j >= 0 and k != 0:
|
||||
o = pow(i,j) % k
|
||||
n = pow(i,j,k)
|
||||
if o != n: print 'Integer mismatch:', i,j,k
|
||||
if j >= 0 and k <> 0:
|
||||
o = pow(long(i),j) % k
|
||||
n = pow(long(i),j,k)
|
||||
if o != n: print 'Long mismatch:', i,j,k
|
||||
if i >= 0 and k <> 0:
|
||||
o = pow(float(i),j) % k
|
||||
n = pow(float(i),j,k)
|
||||
if o != n: print 'Float mismatch:', i,j,k
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue