bpo-38629: implement __floor__ and __ceil__ for float type (GH-16985)

This commit is contained in:
Batuhan Taşkaya 2019-12-16 01:00:28 +03:00 committed by Victor Stinner
parent b08d3f71be
commit cb8b946ac1
4 changed files with 96 additions and 1 deletions

View File

@ -312,6 +312,34 @@ class GeneralFloatCases(unittest.TestCase):
# distinguishes -0.0 and 0.0.
self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
def test_float_floor(self):
self.assertIsInstance(float(0.5).__floor__(), int)
self.assertEqual(float(0.5).__floor__(), 0)
self.assertEqual(float(1.0).__floor__(), 1)
self.assertEqual(float(1.5).__floor__(), 1)
self.assertEqual(float(-0.5).__floor__(), -1)
self.assertEqual(float(-1.0).__floor__(), -1)
self.assertEqual(float(-1.5).__floor__(), -2)
self.assertEqual(float(1.23e167).__floor__(), 1.23e167)
self.assertEqual(float(-1.23e167).__floor__(), -1.23e167)
self.assertRaises(ValueError, float("nan").__floor__)
self.assertRaises(OverflowError, float("inf").__floor__)
self.assertRaises(OverflowError, float("-inf").__floor__)
def test_float_ceil(self):
self.assertIsInstance(float(0.5).__ceil__(), int)
self.assertEqual(float(0.5).__ceil__(), 1)
self.assertEqual(float(1.0).__ceil__(), 1)
self.assertEqual(float(1.5).__ceil__(), 2)
self.assertEqual(float(-0.5).__ceil__(), 0)
self.assertEqual(float(-1.0).__ceil__(), -1)
self.assertEqual(float(-1.5).__ceil__(), -1)
self.assertEqual(float(1.23e167).__ceil__(), 1.23e167)
self.assertEqual(float(-1.23e167).__ceil__(), -1.23e167)
self.assertRaises(ValueError, float("nan").__ceil__)
self.assertRaises(OverflowError, float("inf").__ceil__)
self.assertRaises(OverflowError, float("-inf").__ceil__)
@support.requires_IEEE_754
def test_float_mod(self):
# Check behaviour of % operator for IEEE 754 special cases.

View File

@ -0,0 +1 @@
Added ``__floor__`` and ``__ceil__`` methods to float object. Patch by Batuhan Taşkaya.

View File

@ -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]*/

View File

@ -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