summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorJeffrey Yasskin <jyasskin@gmail.com>2008-01-05 20:03:11 (GMT)
committerJeffrey Yasskin <jyasskin@gmail.com>2008-01-05 20:03:11 (GMT)
commitc2155835b49261c7f755774a16d6576c904395ec (patch)
treedae59f83cbe0884f608698e4205ab86245478367 /Modules
parentd348b2587e01f76a30c07bfa248653bb6b60142f (diff)
downloadcpython-c2155835b49261c7f755774a16d6576c904395ec.zip
cpython-c2155835b49261c7f755774a16d6576c904395ec.tar.gz
cpython-c2155835b49261c7f755774a16d6576c904395ec.tar.bz2
Make math.floor and math.ceil return ints instead of floats.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/mathmodule.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 62f9f0b..b773f70 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -48,7 +48,8 @@ is_error(double x)
}
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 @@ math_1(PyObject *arg, double (*func) (double))
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 @@ static PyObject * math_ceil(PyObject *self, PyObject *number) {
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 @@ static PyObject * math_floor(PyObject *self, PyObject *number) {
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,