Mercurial > cpython-lru_cache-weakref
diff Objects/floatobject.c @ 18838:a8575f358b9f legacy-trunk
SF bug #444510: int() should guarantee truncation.
It's guaranteed now, assuming the platform modf() works correctly.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 26 Jul 2001 20:02:17 +0000 |
| parents | fe4c1034b480 |
| children | 09df3254b49d |
line wrap: on
line diff
--- a/Objects/floatobject.c Thu Jul 26 18:06:58 2001 +0000 +++ b/Objects/floatobject.c Thu Jul 26 20:02:17 2001 +0000 @@ -606,13 +606,19 @@ float_int(PyObject *v) { double x = PyFloat_AsDouble(v); - if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN - : (x = floor(x)) > (double)LONG_MAX) { - PyErr_SetString(PyExc_OverflowError, - "float too large to convert"); - return NULL; - } - return PyInt_FromLong((long)x); + double wholepart; /* integral portion of x, rounded toward 0 */ + long aslong; /* (long)wholepart */ + + (void)modf(x, &wholepart); + /* doubles may have more bits than longs, or vice versa; and casting + to long may yield gibberish in either case. What really matters + is whether converting back to double again reproduces what we + started with. */ + aslong = (long)wholepart; + if ((double)aslong == wholepart) + return PyInt_FromLong(aslong); + PyErr_SetString(PyExc_OverflowError, "float too large to convert"); + return NULL; } static PyObject *
