1
import math
print(math.ceil(0.5))

Returns

1.0

But

import math
print(math.ceil(1/2))

Returns

0.0

What's going on here? Explanation would be nice.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
Oscar Johansson
  • 135
  • 1
  • 2
  • 11
  • 2
    Possible duplicate of [How can I force division to be floating point in Python?](http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python) – Marcus Müller Aug 28 '16 at 18:31

2 Answers2

2

It seems you're running that code using python 2.x where you need to cast to float explicitely:

import math
print(math.ceil(0.5))
print(math.ceil(float(1) / float(2)))

If you run python 3.x you won't need to do that cast explicitely and you'll get the same output:

import math
print(math.ceil(0.5))
print(math.ceil(1 / 2))
BPL
  • 9,632
  • 9
  • 59
  • 117
0
import math
print(math.ceil(1/float(2)))
T T
  • 144
  • 1
  • 3
  • 17
  • This question is looking for an *explanation*, not simply for working code. Your answer provides no insight for the questioner, and may be deleted. Please [edit] to explain what cause's Oscar's observations. – Toby Speight Aug 29 '16 at 08:52