summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>2019-12-15 22:00:28 (GMT)
committerVictor Stinner <vstinner@python.org>2019-12-15 22:00:28 (GMT)
commitcb8b946ac10386e6cab1376945f64f683b5b16d3 (patch)
treede739cc3abe542c43b563cb6167f28757bdcb536 /Objects
parentb08d3f71beab59653edfbbcf7b92a7bc8050d6b8 (diff)
downloadcpython-cb8b946ac10386e6cab1376945f64f683b5b16d3.zip
cpython-cb8b946ac10386e6cab1376945f64f683b5b16d3.tar.gz
cpython-cb8b946ac10386e6cab1376945f64f683b5b16d3.tar.bz2
bpo-38629: implement __floor__ and __ceil__ for float type (GH-16985)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/clinic/floatobject.c.h38
-rw-r--r--Objects/floatobject.c30
2 files changed, 67 insertions, 1 deletions
diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h
index b684ba0..b755483 100644
--- a/Objects/clinic/floatobject.c.h
+++ b/Objects/clinic/floatobject.c.h
@@ -38,6 +38,42 @@ float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored))
return float___trunc___impl(self);
}
+PyDoc_STRVAR(float___floor____doc__,
+"__floor__($self, /)\n"
+"--\n"
+"\n"
+"Return the floor as an Integral.");
+
+#define FLOAT___FLOOR___METHODDEF \
+ {"__floor__", (PyCFunction)float___floor__, METH_NOARGS, float___floor____doc__},
+
+static PyObject *
+float___floor___impl(PyObject *self);
+
+static PyObject *
+float___floor__(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return float___floor___impl(self);
+}
+
+PyDoc_STRVAR(float___ceil____doc__,
+"__ceil__($self, /)\n"
+"--\n"
+"\n"
+"Return the ceiling as an Integral.");
+
+#define FLOAT___CEIL___METHODDEF \
+ {"__ceil__", (PyCFunction)float___ceil__, METH_NOARGS, float___ceil____doc__},
+
+static PyObject *
+float___ceil___impl(PyObject *self);
+
+static PyObject *
+float___ceil__(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return float___ceil___impl(self);
+}
+
PyDoc_STRVAR(float___round____doc__,
"__round__($self, ndigits=None, /)\n"
"--\n"
@@ -351,4 +387,4 @@ float___format__(PyObject *self, PyObject *arg)
exit:
return return_value;
}
-/*[clinic end generated code: output=1676433b9f04fbc9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=25fbbe253f44e2df input=a9049054013a1b77]*/
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 20a5155..4fc412b 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -905,6 +905,34 @@ float___trunc___impl(PyObject *self)
return PyLong_FromDouble(wholepart);
}
+/*[clinic input]
+float.__floor__
+
+Return the floor as an Integral.
+[clinic start generated code]*/
+
+static PyObject *
+float___floor___impl(PyObject *self)
+/*[clinic end generated code: output=e0551dbaea8c01d1 input=77bb13eb12e268df]*/
+{
+ double x = PyFloat_AS_DOUBLE(self);
+ return PyLong_FromDouble(floor(x));
+}
+
+/*[clinic input]
+float.__ceil__
+
+Return the ceiling as an Integral.
+[clinic start generated code]*/
+
+static PyObject *
+float___ceil___impl(PyObject *self)
+/*[clinic end generated code: output=a2fd8858f73736f9 input=79e41ae94aa0a516]*/
+{
+ double x = PyFloat_AS_DOUBLE(self);
+ return PyLong_FromDouble(ceil(x));
+}
+
/* double_round: rounds a finite double to the closest multiple of
10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <=
ndigits <= 323). Returns a Python float, or sets a Python error and
@@ -1828,6 +1856,8 @@ float___format___impl(PyObject *self, PyObject *format_spec)
static PyMethodDef float_methods[] = {
FLOAT_CONJUGATE_METHODDEF
FLOAT___TRUNC___METHODDEF
+ FLOAT___FLOOR___METHODDEF
+ FLOAT___CEIL___METHODDEF
FLOAT___ROUND___METHODDEF
FLOAT_AS_INTEGER_RATIO_METHODDEF
FLOAT_FROMHEX_METHODDEF