1

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
InvisibleWolf
  • 917
  • 1
  • 9
  • 22

2 Answers2

3

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.

Community
  • 1
  • 1
Danica
  • 28,423
  • 6
  • 90
  • 122
0

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

user2684206
  • 35
  • 1
  • 5
  • Correct. Note the **lower** part. Integer division in Python rounds towards -infinite for negative values, **not** towards 0. – Steinar Lima Dec 06 '13 at 10:13