InvisibleWolf
InvisibleWolf

Reputation: 1017

Why math.ceil() give different answers

I am new to python. I was trying to solve a matrix problem in which I have to use exit condition in loop for example if column and row of matrix is 3 or 4 then i want to run the loop 2 times and if col and row is 5 or 6 then it run 3 times.

>>> math.ceil(1.5)
2.0
>>> i=3
>>> math.ceil(i/2)
1.0

Upvotes: 1

Views: 2503

Answers (2)

user2684206
user2684206

Reputation: 45

try this first:

i=3/2
print i
j=float(3)/2
print j
print math.ceil(j)

you should see

1
1.5
2.0

the way python deals with integer division is taking the lower bound.

Reference:

http://docs.python.org/2/reference/expressions.html

Upvotes: 0

Danica
Danica

Reputation: 28846

This is because 3 / 2 isn't 1.5 in Python 2, it's 1. Do from __future__ import division and then it'll be what you expect.

Upvotes: 3

Related Questions