mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
SF bug #444510: int() should guarantee truncation.
It's guaranteed now, assuming the platform modf() works correctly.
This commit is contained in:
parent
7cf92fa1c8
commit
7321ec437b
3 changed files with 27 additions and 10 deletions
|
@ -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 *
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue