[Python-checkins] CVS: python/dist/src/Objects floatobject.c,2.82,2.83

Tim Peters tim_one@users.sourceforge.net
Thu, 26 Jul 2001 13:02:19 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv7893/python/dist/src/Objects

Modified Files:
	floatobject.c 
Log Message:
SF bug #444510: int() should guarantee truncation.
It's guaranteed now, assuming the platform modf() works correctly.


Index: floatobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.82
retrieving revision 2.83
diff -C2 -d -r2.82 -r2.83
*** floatobject.c	2001/05/08 15:19:57	2.82
--- floatobject.c	2001/07/26 20:02:17	2.83
***************
*** 607,617 ****
  {
  	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);
  }
  
--- 607,623 ----
  {
  	double x = PyFloat_AsDouble(v);
! 	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;
  }