Reputation:
What is the algorithmic difference between math.ceil() and round() when trailing decimal points are >= 0.5 in Python 3?
For example,
round(9.5) = 10
round(9.67) = 10
math.ceil(9.5) = 10
math.ceil(9.5) = 10
Upvotes: 4
Views: 785
Reputation: 2150
From the docs,
[...] if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).
However, math.ceil
will always "round" up. I.e. the smallest integer greater than or equal to the input.
Moreover, round
and math.ceil
differ greatly when executing on negative numbers.
>>> math.ceil(-2.8)
-2
>>> round(-2.8)
-3
Upvotes: 6