[Python-3000-checkins] r59747 - in python/branches/py3k: Lib/test/test_math.py Modules/mathmodule.c

jeffrey.yasskin python-3000-checkins at python.org
Sat Jan 5 21:03:13 CET 2008


Author: jeffrey.yasskin
Date: Sat Jan  5 21:03:11 2008
New Revision: 59747

Modified:
   python/branches/py3k/Lib/test/test_math.py
   python/branches/py3k/Modules/mathmodule.c
Log:
Make math.floor and math.ceil return ints instead of floats.


Modified: python/branches/py3k/Lib/test/test_math.py
==============================================================================
--- python/branches/py3k/Lib/test/test_math.py	(original)
+++ python/branches/py3k/Lib/test/test_math.py	Sat Jan  5 21:03:11 2008
@@ -51,6 +51,7 @@
 
     def testCeil(self):
         self.assertRaises(TypeError, math.ceil)
+        self.assertEquals(int, type(math.ceil(0.5)))
         self.ftest('ceil(0.5)', math.ceil(0.5), 1)
         self.ftest('ceil(1.0)', math.ceil(1.0), 1)
         self.ftest('ceil(1.5)', math.ceil(1.5), 2)
@@ -103,6 +104,7 @@
 
     def testFloor(self):
         self.assertRaises(TypeError, math.floor)
+        self.assertEquals(int, type(math.floor(0.5)))
         self.ftest('floor(0.5)', math.floor(0.5), 0)
         self.ftest('floor(1.0)', math.floor(1.0), 1)
         self.ftest('floor(1.5)', math.floor(1.5), 1)

Modified: python/branches/py3k/Modules/mathmodule.c
==============================================================================
--- python/branches/py3k/Modules/mathmodule.c	(original)
+++ python/branches/py3k/Modules/mathmodule.c	Sat Jan  5 21:03:11 2008
@@ -48,7 +48,8 @@
 }
 
 static PyObject *
-math_1(PyObject *arg, double (*func) (double))
+math_1_to_whatever(PyObject *arg, double (*func) (double),
+                   PyObject *(*from_double_func) (double))
 {
 	double x = PyFloat_AsDouble(arg);
 	if (x == -1.0 && PyErr_Occurred())
@@ -61,7 +62,19 @@
 	if (errno && is_error(x))
 		return NULL;
 	else
-		return PyFloat_FromDouble(x);
+        	return (*from_double_func)(x);
+}
+
+static PyObject *
+math_1(PyObject *arg, double (*func) (double))
+{
+	return math_1_to_whatever(arg, func, PyFloat_FromDouble);
+}
+
+static PyObject *
+math_1_to_int(PyObject *arg, double (*func) (double))
+{
+	return math_1_to_whatever(arg, func, PyLong_FromDouble);
 }
 
 static PyObject *
@@ -120,13 +133,13 @@
 
 	method = _PyType_Lookup(Py_TYPE(number), ceil_str);
 	if (method == NULL)
-		return math_1(number, ceil);
+		return math_1_to_int(number, ceil);
 	else
 		return PyObject_CallFunction(method, "O", number);
 }
 
 PyDoc_STRVAR(math_ceil_doc,
-	     "ceil(x)\n\nReturn the ceiling of x as a float.\n"
+	     "ceil(x)\n\nReturn the ceiling of x as an int.\n"
 	     "This is the smallest integral value >= x.");
 
 FUNC1(cos, cos,
@@ -160,13 +173,13 @@
 
 	method = _PyType_Lookup(Py_TYPE(number), floor_str);
 	if (method == NULL)
-		return math_1(number, floor);
+        	return math_1_to_int(number, floor);
 	else
 		return PyObject_CallFunction(method, "O", number);
 }
 
 PyDoc_STRVAR(math_floor_doc,
-	     "floor(x)\n\nReturn the floor of x as a float.\n"
+	     "floor(x)\n\nReturn the floor of x as an int.\n"
 	     "This is the largest integral value <= x.");
 
 FUNC2(fmod, fmod,


More information about the Python-3000-checkins mailing list