r, a and b are integers.
I need the cheapest computation since in a critical part of the code.
I found :
r = (a / b) + (((a % b) != 0) ? 1 : 0);
if b is a power of 2, then a / b can be replaced with a >> log2(b)
and a % b with a & (b-1) which should save a lot of computation time.
Do you know any better solution ?
For example:
This does assume
aandbare positive. If either are negative, it depends on whether the division is symmetric or floored (modern languages and platforms are symmetric), and the signal ofaandb.If
a*b >= 0, then the formula works as given. If the division is symmetric anda*b < 0, thena / bgives the correct answer.