FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-38629: implement __floor__ and __ceil__ for float by isidentical · Pull Request #16985 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 28 additions & 0 deletions Lib/test/test_float.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL