summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-01 23:45:44 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-01 23:45:44 (GMT)
commitf9859037fc7e296a6235f27d4ac160197aed828e (patch)
tree1c843c9a06765a405f7c54c21a44f757c2fc8775 /Objects
parent1bcb99a9cbda4d969209ff092b2f13ea2acb5934 (diff)
downloadcpython-f9859037fc7e296a6235f27d4ac160197aed828e.zip
cpython-f9859037fc7e296a6235f27d4ac160197aed828e.tar.gz
cpython-f9859037fc7e296a6235f27d4ac160197aed828e.tar.bz2
Add protection from weirdness while scaling the mantissa to an integer.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 5f42ca5..ca05f0d 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1159,6 +1159,7 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
double self;
double float_part;
int exponent;
+ int i;
PyObject *prev;
PyObject *py_exponent = NULL;
@@ -1198,16 +1199,21 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
PyFPE_END_PROTECT(float_part);
- while (float_part != floor(float_part)) {
+ for (i=0; i<300 && float_part != floor(float_part) ; i++) {
float_part *= 2.0;
exponent--;
}
- /* Now, self == float_part * 2**exponent exactly and float_part is integral */
+ if (i == 300) {
+ /* Could not convert mantissa to an integer */
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ /* self == float_part * 2**exponent exactly and float_part is integral */
numerator = PyLong_FromDouble(float_part);
if (numerator == NULL) goto error;
- /* now self = numerator * 2**exponent exactly; fold in 2**exponent */
+ /* fold in 2**exponent */
denominator = PyLong_FromLong(1);
py_exponent = PyLong_FromLong(labs((long)exponent));
if (py_exponent == NULL) goto error;
@@ -1216,8 +1222,7 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
if (py_exponent == NULL) goto error;
if (exponent > 0) {
INPLACE_UPDATE(numerator,
- long_methods->nb_multiply(numerator,
- py_exponent));
+ long_methods->nb_multiply(numerator, py_exponent));
if (numerator == NULL) goto error;
}
else {