To give a short answer ...
Python offers only native operators for two types of division: true division and rounding of division. So what you want is not available as a single function. However, using short expressions you can easily implement many different types of division with rounding.
According to the title request: when using strictly integer inputs, rounding can be implemented using (a+(-a%b))//b , and rounding from zero can be done using a more complex a//b if a*b<0 else (a+(-a%b))//b . One of them is probably what you want. As for why ...
To give a longer answer ...
First, let me answer the question of why math.ceil(3/2)==1.0 3/2==1 and math.ceil(3/2)==1.0 , explaining how the Python split operator works. There are two main problems in the game ...
float vs int division: In the Python 2 section, division behaves differently depending on the type of inputs. If both a and b are integers, a/b performs the rounding down or integer separation (for example, -3/2==-2 3/2==1 , but -3/2==-2 ). This is equivalent to int(math.floor(float(a)/b)) .
But if at least one of a and b is a float, Python does the “true” division and gives the result float (for example, 3.0/2==1.5 and -3.0/2==-1.5 ). This is why you sometimes see the float(a)/b construct: it is used to force true division, even if both inputs are integers (for example, float(3)/2==1.5 ). This is why your math.ceil(3/2) example returns 1.0 , while math.ceil(float(3)/2) returns 2.0 . The result is already rounded before it reaches math.ceil() .
true division by default . In 2001, it was decided ( PEP 238 ) that the Python division operator should be changed so that it always performs “true” division, regardless of whether the inputs are floats or integers (for example, this would make 3/2 3/2==1.5 ). In order not to break existing scripts, the default behavior change was delayed until Python 3.0; in order to get this behavior under Python 2.x, you must include it for each file by adding from __future__ import division to the top of the file. Otherwise, the old type-specific behavior is used.
But the “rounding” separation is still often necessary, so PEP has not completely dealt with it. Instead, he introduced a new division operator: a//b , which always performs rounded division, even if the inputs contain floats. This can be used without doing anything special under Python 2.2+ and 3.x.
Thus, division with rounding:
To simplify the work, all expressions use the a//b operator when working on integers, since it will behave the same in all versions of python. In addition, I make the assumption that 0<=a%b<b if b positive, and b<=a%b<=0 if b is negative. This is how Python behaves, but in other languages there may be slightly different module statements.
Four main types of integer division with rounding:
"round down" aka "integer" aka "round to minus infinity" divsion: python offers this initially through a//b .
"round up" aka "ceiling integer", aka "rounding to positive infinity": this can be achieved using int(math.ceil(float(a)/b)) or (a+(-a%b))//b . The last equation works because -a%b is 0 if a is a multiple of b , and the rest is the sum that we need to add to a in order to move to the next largest edge.
"round to zero" , aka "truncated" separation - this can be achieved using int(float(a)/b) . Doing this without using a floating point is more complicated ... since Python only offers rounded integer rounding division, and the % operator has a similar rounding offset down, we have no floating point operators that are rounded symmetrically around 0. Therefore, the only way I can to think is to build a piecewise expression from rounding and rounding: a//b if a*b>0 else (a+(-a%b))//b .
“round off from zero aka round to (or) infinity” division - unfortunately, this is even more difficult than a round to zero. We can no longer use the shortening behavior of the int operator, so I cannot come up with a simple expression even when using floating point operations. Therefore, I have to go back to the round expression and use a//b if a*b<0 else (a+(-a%b))//b .
Please note that if you use only positive integers, (a+b-1)//b provides rounding up / from zero even more efficiently than any of the above solutions, but falls apart into negatives.
Hope this helps ... and am happy to make a change if anyone can suggest the best equations for a round to / from zero. I find those that I have especially unsatisfactory.