444,100 Members | 2,892 Online Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

# Integer division

 P: n/a Hello all, I have two integers and I want to divide one by another, and want to get an integer result which is the higher side whenever the result is a fraction. 3/2 =1 # Usual behavior some_func(3, 2) =2 # Wanted Any easier solution other than int(math.ceil(float(3)/2)) - Suresh Jun 7 '07 #1
9 Replies

 P: n/a jm*******@no.spam.gmail.com wrote: Hello all, I have two integers and I want to divide one by another, and want to get an integer result which is the higher side whenever the result is a fraction. 3/2 =1 # Usual behavior some_func(3, 2) =2 # Wanted Any easier solution other than int(math.ceil(float(3)/2)) The normal solution is to add (divisor-1) to the dividend before division. Ie ceil(3/2) = floor((3+(2-1))/2) = 2. Correct. But ceil(4/2) = floor((4+(2-1))/2) = 2 also. Correct. Hamish Jun 7 '07 #2

 P: n/a On Jun 7, 2:15 pm, Hamish Moffatt >def div_ceil(a, b): .... if a%b: .... return ((a/b)+1) .... else: .... return (a/b) .... >>div_ceil(3,2) 2 >>div_ceil(-3,2) -1 >>-3%2 1 >>-(3%2) -1 >>div_ceil(-5,2) -2 >>div_ceil(5,2) 3 Jun 7 '07 #3

 P: n/a jm*******@no.spam.gmail.com

 P: n/a jm*******@no.spam.gmail.com wrote: On Jun 7, 2:15 pm, Hamish Moffatt jm.sur...@no.spam.gmail.com wrote: >>Hello all, I have two integers and I want to divide one by another, and want toget an integer result which is the higher side whenever the result isa fraction. 3/2 =1 # Usual behavior some_func(3, 2) =2 # WantedAny easier solution other than int(math.ceil(float(3)/2)) The normal solution is to add (divisor-1) to the dividend before division.Ie ceil(3/2) = floor((3+(2-1))/2) = 2. Correct.But ceil(4/2) = floor((4+(2-1))/2) = 2 also. Correct. What about this? >>>def div_ceil(a, b): ... if a%b: ... return ((a/b)+1) ... else: ... return (a/b) Yes, although it's not as short or as fast (probably as my version): def div_ceil(a, b): return ((a+(b-1))/b) Hamish Jun 7 '07 #5

 P: n/a Hamish Moffatt jm*******@no.spam.gmail.com wrote: >>>>def div_ceil(a, b): ... if a%b:... return ((a/b)+1)... else:... return (a/b) Yes, although it's not as short or as fast (probably as my version):def div_ceil(a, b): return ((a+(b-1))/b) If that's what you care about: \$ python -mtimeit -s 'def divc1(a,b): return (a+b-1)/b' 'divc1(3,2)' 1000000 loops, best of 3: 0.443 usec per loop \$ python -mtimeit -s 'def divc2(a,b): return -(-a/b)' 'divc2(3,2)' 1000000 loops, best of 3: 0.331 usec per loop Also, note: >>divc2(sys.maxint, 2) 1073741824 >>divc1(sys.maxint, 2) 1073741824L which is going to cause problems with sys.version_info < (2, 3) . (Or do I mean (2, 2)? I don't have a 2.2 to hand.) -- \S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/ "Frankly I have no feelings towards penguins one way or the other" -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump Jun 7 '07 #6

 P: n/a "Sion Arrowsmith"

 P: n/a On Jun 7, 8:30 pm, Some Other Guy

 P: n/a "Some Other Guy"

 P: n/a In <11*********************@q69g2000hsb.googlegroups. com>, Dan Bishop wrote: On Jun 7, 8:30 pm, Some Other Guy Since this just involves doubling you can avoid multiplying altogetherand just use this:def rounddiv(a,b): return int((a+a+b)/(b+b))That's 3 integer adds and 1 integer divide. The int() cast is justthere is case somebody passes in floats for a or b; if you know you'reonly going to have integer arguments you don't need it. The // operator was added to the language for a reason. I'm surprised at how few Pythonistas are using it six years later. It would just document intent here because it still gives `float` results if used with at least one `float` as operands: In : 10.0 // 2 Out: 5.0 Ciao, Marc 'BlackJack' Rintsch Jun 8 '07 #10

### This discussion thread is closed

Replies have been disabled for this discussion. 