Skip to content

Commit cb8b946

Browse files
isidenticalvstinner
authored andcommitted
bpo-38629: implement __floor__ and __ceil__ for float type (GH-16985)
1 parent b08d3f7 commit cb8b946

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

Lib/test/test_float.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,34 @@ def assertEqualAndEqualSign(self, a, b):
312312
# distinguishes -0.0 and 0.0.
313313
self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
314314

315+
def test_float_floor(self):
316+
self.assertIsInstance(float(0.5).__floor__(), int)
317+
self.assertEqual(float(0.5).__floor__(), 0)
318+
self.assertEqual(float(1.0).__floor__(), 1)
319+
self.assertEqual(float(1.5).__floor__(), 1)
320+
self.assertEqual(float(-0.5).__floor__(), -1)
321+
self.assertEqual(float(-1.0).__floor__(), -1)
322+
self.assertEqual(float(-1.5).__floor__(), -2)
323+
self.assertEqual(float(1.23e167).__floor__(), 1.23e167)
324+
self.assertEqual(float(-1.23e167).__floor__(), -1.23e167)
325+
self.assertRaises(ValueError, float("nan").__floor__)
326+
self.assertRaises(OverflowError, float("inf").__floor__)
327+
self.assertRaises(OverflowError, float("-inf").__floor__)
328+
329+
def test_float_ceil(self):
330+
self.assertIsInstance(float(0.5).__ceil__(), int)
331+
self.assertEqual(float(0.5).__ceil__(), 1)
332+
self.assertEqual(float(1.0).__ceil__(), 1)
333+
self.assertEqual(float(1.5).__ceil__(), 2)
334+
self.assertEqual(float(-0.5).__ceil__(), 0)
335+
self.assertEqual(float(-1.0).__ceil__(), -1)
336+
self.assertEqual(float(-1.5).__ceil__(), -1)
337+
self.assertEqual(float(1.23e167).__ceil__(), 1.23e167)
338+
self.assertEqual(float(-1.23e167).__ceil__(), -1.23e167)
339+
self.assertRaises(ValueError, float("nan").__ceil__)
340+
self.assertRaises(OverflowError, float("inf").__ceil__)
341+
self.assertRaises(OverflowError, float("-inf").__ceil__)
342+
315343
@support.requires_IEEE_754
316344
def test_float_mod(self):
317345
# Check behaviour of % operator for IEEE 754 special cases.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``__floor__`` and ``__ceil__`` methods to float object. Patch by Batuhan Taşkaya.

Objects/clinic/floatobject.c.h

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/floatobject.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,34 @@ float___trunc___impl(PyObject *self)
905905
return PyLong_FromDouble(wholepart);
906906
}
907907

908+
/*[clinic input]
909+
float.__floor__
910+
911+
Return the floor as an Integral.
912+
[clinic start generated code]*/
913+
914+
static PyObject *
915+
float___floor___impl(PyObject *self)
916+
/*[clinic end generated code: output=e0551dbaea8c01d1 input=77bb13eb12e268df]*/
917+
{
918+
double x = PyFloat_AS_DOUBLE(self);
919+
return PyLong_FromDouble(floor(x));
920+
}
921+
922+
/*[clinic input]
923+
float.__ceil__
924+
925+
Return the ceiling as an Integral.
926+
[clinic start generated code]*/
927+
928+
static PyObject *
929+
float___ceil___impl(PyObject *self)
930+
/*[clinic end generated code: output=a2fd8858f73736f9 input=79e41ae94aa0a516]*/
931+
{
932+
double x = PyFloat_AS_DOUBLE(self);
933+
return PyLong_FromDouble(ceil(x));
934+
}
935+
908936
/* double_round: rounds a finite double to the closest multiple of
909937
10**-ndigits; here ndigits is within reasonable bounds (typically, -308 <=
910938
ndigits <= 323). Returns a Python float, or sets a Python error and
@@ -1828,6 +1856,8 @@ float___format___impl(PyObject *self, PyObject *format_spec)
18281856
static PyMethodDef float_methods[] = {
18291857
FLOAT_CONJUGATE_METHODDEF
18301858
FLOAT___TRUNC___METHODDEF
1859+
FLOAT___FLOOR___METHODDEF
1860+
FLOAT___CEIL___METHODDEF
18311861
FLOAT___ROUND___METHODDEF
18321862
FLOAT_AS_INTEGER_RATIO_METHODDEF
18331863
FLOAT_FROMHEX_METHODDEF

0 commit comments

Comments
 (0)