If you have integer division of positive numbers which rounds toward zero, then you can add one less than the divisor to the dividend to make it round up.
That is to say, replace X / Y
with (X + Y - 1) / Y
.
Proof:
Case 1: X = k * Y
(X is integer multiple of Y): In this case, we have (k * Y + Y - 1) / Y
, which splits into (k * Y) / Y + (Y - 1) / Y
. The (Y - 1)/Y
part rounds to zero, and we are left with a quotient of k
. This is exactly what we want: when the inputs are divisible, we want the adjusted calculation to still produce the correct exact quotient.
Case 2: X = k * Y + m
where 0 < m < Y
(X is not a multiple of Y). In this case we have a numerator of k * Y + m + Y - 1
, or k * Y + Y + m - 1
, and we can write the division out as (k * Y)/Y + Y/Y + (m - 1)/Y
. Since 0 < m < Y
, 0 <= m - 1 < Y - 1
, and so the last term (m - 1)/Y
goes to zero. We are left with (k * Y)/Y + Y/Y
which work out to k + 1
. This shows that the behavior rounds up. If we have an X
which is a k
multiple of Y
, if we add just 1 to it, the division rounds up to k + 1
.
But this rounding is extremely opposite; all inexact divisions go away from zero. How about something in between?
That can be achieved by "priming" the numerator with Y/2
. Instead of X/Y
, calculate (X+Y/2)/Y
. Instead of proof, let's go empirical on this one:
$ round()
> {
> echo $((($1 + $2/2) / $2))
> }
$ round 4 10
0
$ round 5 10
1
$ round 6 10
1
$ round 9 10
1
$ round 10 10
1
$ round 14 10
1
$ round 15 10
2
Whenever the divisor is an even, positive number, if the numerator is congruent to half that number, it rounds up, and rounds down if it is one less than that.
For instance, round 6 12
goes to 1
, as do all values which are equal to 6
, modulo 12
, like 18
(which goes to 2) and so on. round 5 12
goes down to 0
.
For odd numbers, the behavior is correct. None of the exact rational numbers are midway between two consecutive multiples. For instance, with a denominator of 11
we have 5/11 < 5.5/11 (exact middle) < 6/11
; and round 5 11
rounds down, whereas round 6 11
rounds up.