add floor() (#1551)

* ceil and floor

* ceil and floor
This commit is contained in:
Du YuanChao 2019-11-04 15:28:51 +08:00 committed by Christian Clauss
parent 0e3357ae35
commit 8c443ccfad
2 changed files with 36 additions and 0 deletions

18
maths/ceil.py Normal file
View File

@ -0,0 +1,18 @@
def ceil(x) -> int:
"""
Return the ceiling of x as an Integral.
:param x: the number
:return: the smallest integer >= x.
>>> import math
>>> 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)
if __name__ == '__main__':
import doctest
doctest.testmod()

18
maths/floor.py Normal file
View File

@ -0,0 +1,18 @@
def floor(x) -> int:
"""
Return the floor of x as an Integral.
:param x: the number
:return: the largest integer <= x.
>>> import math
>>> 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)
if __name__ == '__main__':
import doctest
doctest.testmod()