I recently transitioned one script to use Python 2.7 from 3.4 (at the request of a colleague), and I'm finding that the math.ceil function does not work the same...
When I divide 5/2 and round it up with math.ceil(), I expect to get math.ceil(5/2) = 3.
Given the following code:
from __future__ import print_function
import math
a = 5
b = 2
c = math.ceil(a/b)
print("Ceiling output: {}".format(c))
Python 2.7 will report 2 as the answer, Python 3.4 reports 3 as expected.
Why is this?
I know I can get 2.7 to work if I cast a
and b
as float
:
c = math.ceil(float(a)/float(b)
However, casting the division won't work either:
c = math.ceil(float(a/b))
How does 3.4 work around this?
Finding this makes me question how much math I need to re-check in the 2.7 version of the script.