6

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 :

>>> count = friends.count()
>>> print count
1
>>> per_page = 2
>>> print per_page
2
>>> pages = math.ceil(count/per_pages)
>>> print pages
0.0
>>> pages = float(count/per_pages)
>>> print pages
0.0

What am I doing wrong, and why math.ceil gives float number instead of int ?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
tom_pl
  • 425
  • 2
  • 9
  • 16
  • it works when I do it like this : count = float(count), per_page = float(per_page), pages = math.ceil(count/per_page) and finally pages = int(pages) . But that's a bit dumb way. – tom_pl Jul 29 '10 at 22:07
  • See [ Why doesn’t this division work in python? ](http://stackoverflow.com/questions/1787249/why-doesnt-this-division-work-in-python/). – Matthew Flaschen Jul 29 '10 at 22:14

6 Answers6

15

Python does integer division when both operands are integers, meaning that 1 / 2 is basically "how many times does 2 go into 1", which is of course 0 times. To do what you want, convert one operand to a float: 1 / float(2) == 0.5, as you're expecting. And, of course, math.ceil(1 / float(2)) will yield 1, as you expect.

(I think this division behavior changes in Python 3.)

mipadi
  • 398,885
  • 90
  • 523
  • 479
6

Integer division is the default of the / operator in Python < 3.0. This has behaviour that seems a little weird. It returns the dividend without a remainder.

>>> 10 / 3
3

If you're running Python 2.6+, try:

from __future__ import division

>>> 10 / 3
3.3333333333333335

If you're running a lower version of Python than this, you will need to convert at least one of the numerator or denominator to a float:

>>> 10 / float(3)
3.3333333333333335

Also, math.ceil always returns a float...

>>> import math 
>>> help(math.ceil)

ceil(...)
    ceil(x)

    Return the ceiling of x as a float.
    This is the smallest integral value >= x.
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
1

From Python documentation (math module):

math.ceil(x)

Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

Community
  • 1
  • 1
qbi
  • 2,104
  • 1
  • 23
  • 35
0

They're integers, so count/per_pages is zero before the functions ever get to do anything beyond that. I'm not a Python programmer really but I know that (count * 1.0) / pages will do what you want. There's probably a right way to do that however.

edit — yes see @mipadi's answer and float(x)

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

its because how you have it set up is performing the operation and then converting it to a float try

count = friends.count()
print count

per_page = float(2)
print per_page

pages = math.ceil(count/per_pages)

print pages
pages = count/per_pages

By converting either count or per_page to a float all of its future operations should be able to do divisions and end up with non whole numbers

Hugoagogo
  • 1,598
  • 16
  • 34
0
>>> 10 / float(3)
3.3333333333333335
>>> #Or 
>>> 10 / 3.0
3.3333333333333335
>>> #Python make any decimal number to float
>>> a = 3
>>> type(a)
<type 'int'>
>>> b = 3.0
>>> type(b)
<type 'float'>
>>> 

The best solution maybe is to use from __future__ import division

snippsat
  • 206
  • 2
  • 2