Skip to content

Commit 6e373f2

Browse files
author
raymond.hettinger
committed
Add protection from weirdness while scaling the mantissa to an integer.
git-svn-id: http://svn.python.org/projects/python/trunk@60517 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent cbec689 commit 6e373f2

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

Objects/floatobject.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
11591159
double self;
11601160
double float_part;
11611161
int exponent;
1162+
int i;
11621163

11631164
PyObject *prev;
11641165
PyObject *py_exponent = NULL;
@@ -1198,16 +1199,21 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
11981199
float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
11991200
PyFPE_END_PROTECT(float_part);
12001201

1201-
while (float_part != floor(float_part)) {
1202+
for (i=0; i<300 && float_part != floor(float_part) ; i++) {
12021203
float_part *= 2.0;
12031204
exponent--;
12041205
}
1205-
/* Now, self == float_part * 2**exponent exactly and float_part is integral */
1206+
if (i == 300) {
1207+
/* Could not convert mantissa to an integer */
1208+
Py_INCREF(Py_NotImplemented);
1209+
return Py_NotImplemented;
1210+
}
1211+
/* self == float_part * 2**exponent exactly and float_part is integral */
12061212

12071213
numerator = PyLong_FromDouble(float_part);
12081214
if (numerator == NULL) goto error;
12091215

1210-
/* now self = numerator * 2**exponent exactly; fold in 2**exponent */
1216+
/* fold in 2**exponent */
12111217
denominator = PyLong_FromLong(1);
12121218
py_exponent = PyLong_FromLong(labs((long)exponent));
12131219
if (py_exponent == NULL) goto error;
@@ -1216,8 +1222,7 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
12161222
if (py_exponent == NULL) goto error;
12171223
if (exponent > 0) {
12181224
INPLACE_UPDATE(numerator,
1219-
long_methods->nb_multiply(numerator,
1220-
py_exponent));
1225+
long_methods->nb_multiply(numerator, py_exponent));
12211226
if (numerator == NULL) goto error;
12221227
}
12231228
else {

0 commit comments

Comments
 (0)