From 211097262e62709d929124284d96e30c7be7b248 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 3 Nov 2019 22:26:12 +0800 Subject: [PATCH 1/2] ceil and floor --- maths/ceil.py | 30 ++++++++++++++++++++++++++++++ maths/floor.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 maths/ceil.py create mode 100644 maths/floor.py diff --git a/maths/ceil.py b/maths/ceil.py new file mode 100644 index 000000000000..d67e706920cc --- /dev/null +++ b/maths/ceil.py @@ -0,0 +1,30 @@ +def ceil(x) -> int: + """ + Return the ceiling of x as an Integral. + + :param x: the number + :return: the smallest integer >= x. + + >>> import math + >>> ceil(1) == math.ceil(1) + True + >>> ceil(-1) == math.ceil(-1) + True + >>> ceil(0) == math.ceil(0) + True + >>> ceil(-0) == math.ceil(-0) + True + >>> ceil(1.1) == math.ceil(1.1) + True + >>> ceil(-1.1) == math.ceil(-1.1) + True + >>> ceil(-1.0) == math.ceil(-1.0) + True + """ + return x if isinstance(x, int) or x - int(x) == 0 else int(x + 1) if x > 0 else int(x) + + +if __name__ == '__main__': + import doctest + + doctest.testmod() diff --git a/maths/floor.py b/maths/floor.py new file mode 100644 index 000000000000..64a2b1dfed20 --- /dev/null +++ b/maths/floor.py @@ -0,0 +1,30 @@ +def floor(x) -> int: + """ + Return the floor of x as an Integral. + + :param x: the number + :return: the largest integer <= x. + + >>> import math + >>> floor(1) == math.floor(1) + True + >>> floor(-1) == math.floor(-1) + True + >>> floor(0) == math.floor(0) + True + >>> floor(-0) == math.floor(-0) + True + >>> floor(1.1) == math.floor(1.1) + True + >>> floor(-1.1) == math.floor(-1.1) + True + >>> floor(-1.0) == math.floor(-1.0) + True + """ + return x if isinstance(x, int) or x - int(x) == 0 else int(x) if x > 0 else int(x - 1) + + +if __name__ == '__main__': + import doctest + + doctest.testmod() From 92f624c9f952e17bad864545132e6b38d205d626 Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 4 Nov 2019 15:20:18 +0800 Subject: [PATCH 2/2] ceil and floor --- maths/ceil.py | 14 +------------- maths/floor.py | 14 +------------- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/maths/ceil.py b/maths/ceil.py index d67e706920cc..3e46f1474dcf 100644 --- a/maths/ceil.py +++ b/maths/ceil.py @@ -6,19 +6,7 @@ def ceil(x) -> int: :return: the smallest integer >= x. >>> import math - >>> ceil(1) == math.ceil(1) - True - >>> ceil(-1) == math.ceil(-1) - True - >>> ceil(0) == math.ceil(0) - True - >>> ceil(-0) == math.ceil(-0) - True - >>> ceil(1.1) == math.ceil(1.1) - True - >>> ceil(-1.1) == math.ceil(-1.1) - True - >>> ceil(-1.0) == math.ceil(-1.0) + >>> all(ceil(n) == math.ceil(n) for n in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000)) True """ return x if isinstance(x, int) or x - int(x) == 0 else int(x + 1) if x > 0 else int(x) diff --git a/maths/floor.py b/maths/floor.py index 64a2b1dfed20..a9b680b37b97 100644 --- a/maths/floor.py +++ b/maths/floor.py @@ -6,19 +6,7 @@ def floor(x) -> int: :return: the largest integer <= x. >>> import math - >>> floor(1) == math.floor(1) - True - >>> floor(-1) == math.floor(-1) - True - >>> floor(0) == math.floor(0) - True - >>> floor(-0) == math.floor(-0) - True - >>> floor(1.1) == math.floor(1.1) - True - >>> floor(-1.1) == math.floor(-1.1) - True - >>> floor(-1.0) == math.floor(-1.0) + >>> all(floor(n) == math.floor(n) for n in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000)) True """ return x if isinstance(x, int) or x - int(x) == 0 else int(x) if x > 0 else int(x - 1)