diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-07-26 20:02:17 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-07-26 20:02:17 (GMT) |
commit | 7321ec437b0aef968b83137d9c638551cabab706 (patch) | |
tree | 83890a0d039e47d282d2abda1f8eb173710de3f8 /Objects/floatobject.c | |
parent | 7cf92fa1c891b53e6143e090009911c0231bb24a (diff) | |
download | cpython-7321ec437b0aef968b83137d9c638551cabab706.zip cpython-7321ec437b0aef968b83137d9c638551cabab706.tar.gz cpython-7321ec437b0aef968b83137d9c638551cabab706.tar.bz2 |
SF bug #444510: int() should guarantee truncation.
It's guaranteed now, assuming the platform modf() works correctly.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r-- | Objects/floatobject.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 2f17d02..d1ce092 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -606,13 +606,19 @@ static PyObject * 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 * |