You can do this quite simply:
(n + d // 2) // d, where n is the dividend and d is the divisor.
Alternatives like (((n << 1) // d) + 1) >> 1 or the equivalent (((n * 2) // d) + 1) // 2 may be SLOWER in recent CPythons, where an int is implemented like the old long.
The simple method does 3 variable accesses, 1 constant load, and 3 integer operations. The complicated methods do 2 variable accesses, 3 constant loads, and 4 integer operations. Integer operations are likely to take time which depends on the sizes of the numbers involved. Variable accesses of function locals don't involve "lookups".
If you are really desparate for speed, do benchmarks. Otherwise, KISS.
Answer from John Machin on Stack OverflowYou can do this quite simply:
(n + d // 2) // d, where n is the dividend and d is the divisor.
Alternatives like (((n << 1) // d) + 1) >> 1 or the equivalent (((n * 2) // d) + 1) // 2 may be SLOWER in recent CPythons, where an int is implemented like the old long.
The simple method does 3 variable accesses, 1 constant load, and 3 integer operations. The complicated methods do 2 variable accesses, 3 constant loads, and 4 integer operations. Integer operations are likely to take time which depends on the sizes of the numbers involved. Variable accesses of function locals don't involve "lookups".
If you are really desparate for speed, do benchmarks. Otherwise, KISS.
skip = (((total << 1) // surplus) + 1) >> 1
Shifting things left by one bit effectively multiplies by two, shifting things right by one bit divides by two rounding down. Adding one in the middle makes it so that "rounding down" is actually rounding up if the result would have been above a .5 decimal part.
It's basically the same as if you wrote...
skip = int((1.0*total/surplus) + 0.5)
except with everything multplied by 2, and then later divided by 2, which is something you can do with integer arithmetic (since bit shifts don't require floating point).
Videos
I'm taking my first programming course in college this semester and we've been tasked with a few basic exercises using mostly if-else statements and the odd very basic for loop. For this particular exercise, one issue I've come with that I hadn't before is when using the // operator. From my understanding + research and what has been taught to me, this operator is supposed to simply round down to the nearest whole number. I won't explain the entire exercise/project's goal, as per the rules of the subreddit, but the specific case I've just observed is when rounding -2.4, or more specifically abs(-2.4). I defined a variable as such:
digit1 = abs(num // 10)
num is a previously defined variable containing an integer input ( int(input("")) ). This variable is used to look for the first digit of the inputted number. When I input -24, the result I expected was 2, but the output ended up being 3. I'm not too sure what's going on. Is the absolute function or the fact that it's a negative number affecting the result?
Also, in case this is relevant, we're using replit as an IDE.
Thanks in advance for any responses.
Sidenote: I apologize if this is a very simple issue that I'm just not seeing. Asked similar questions in other sites and people seemed annoyed at how simple the solution was. Reddit's always been pretty nice but hey, you never know.
Do floating point division then convert to an int. No extra modules needed.
Python 3:
>>> int(-1 / 2)
0
>>> int(-3 / 2)
-1
>>> int(1 / 2)
0
>>> int(3 / 2)
1
Python 2:
>>> int(float(-1) / 2)
0
>>> int(float(-3) / 2)
-1
>>> int(float(1) / 2)
0
>>> int(float(3) / 2)
1
For what it's worth, my own favourite solution is this one. Integer arithmetic only, a single division, and everything else linear time:
def integer_divide_towards_zero(a, b):
return -(-a // b) if a < 0 else a // b
That assumes that b is positive, but in most of the applications I've seen that's true. If you need to deal with negative b too, then the function becomes marginally more complicated:
def integer_divide_towards_zero(a, b):
return -(-a // b) if (a < 0) ^ (b < 0) else a // b
Some sample outputs:
>>> integer_divide_towards_zero(11, 3)
3
>>> integer_divide_towards_zero(-11, 3)
-3
>>> integer_divide_towards_zero(6, 3)
2
>>> integer_divide_towards_zero(-6, 3)
-2
>>> integer_divide_towards_zero(11, -3)
-3
>>> integer_divide_towards_zero(-11, -3)
3
The math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
I know this answer is for a question from a while back, but if you don't want to import math and you just want to round up, this works for me.
>>> int(21 / 5)
4
>>> int(21 / 5) + (21 % 5 > 0)
5
The first part becomes 4 and the second part evaluates to "True" if there is a remainder, which in addition True = 1; False = 0. So if there is no remainder, then it stays the same integer, but if there is a remainder it adds 1.