Skip to content

Commit

Permalink
bpo-38629: implement __floor__ and __ceil__ for float type (pythonGH-…
Browse files Browse the repository at this point in the history
  • Loading branch information
isidentical authored and shihai1991 committed Jan 31, 2020
1 parent b1ad15d commit 5d7d318
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,34 @@ def assertEqualAndEqualSign(self, a, b):
# 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``__floor__`` and ``__ceil__`` methods to float object. Patch by Batuhan Taşkaya.
38 changes: 37 additions & 1 deletion Objects/clinic/floatobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d7d318

Please sign in to comment.