Skip to content

bpo-38629: implement __floor__ and __ceil__ for float #16985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -923,6 +923,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 @@ -1847,6 +1875,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