If you have an integer division of positive numbers that is rounded to zero, then you can add one less divisor to the dividend to round it.
That is, replace X / Y with (X + Y - 1) / Y
Evidence:
Case 1: X = k * Y (X is an integer multiple of Y): In this case, we have (k * Y + Y - 1) / Y , which is divided into (k * Y) / Y + (Y - 1) / Y . The party (Y - 1)/Y rounded to zero, and we are left with a coefficient k . This is exactly what we want: when the inputs are divided, we want the adjusted calculation to still produce the correct accurate coefficient.
Case 2: X = k * Y + m where 0 < m < Y (X is not a multiple of Y). In this case, we have the numerator k * Y + m + Y - 1 , or k * Y + Y + m - 1 , and we can write the division as (k * Y)/Y + Y/Y + (m - 1)/Y Since 0 < m < Y , 0 <= m - 1 < Y - 1 , therefore, the last term (m - 1)/Y vanishes. Remains (k * Y)/Y + Y/Y , which work up to k + 1 . This shows that the behavior is rounded. If we have X , which is k multiple of Y , if you add only 1 to it, the division is rounded to k + 1 .
But this rounding is extremely opposite; all inaccurate divisions go from scratch. How about something in between?
This can be achieved by βfillingβ the numerator with Y/2 . Instead of X/Y calculate (X+Y/2)/Y Instead of evidence, release the empirical on this:
$ 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 a divisor is an even, positive number, if the numerator is comparable to half that number, it is rounded and rounded if it is less than that.
For example, round 6 12 goes to 1 , like all values ββequal to 6 , modulo 12 , such as 18 (which corresponds to 2), etc. round 5 12 goes to 0 .
For odd numbers, the behavior is correct. None of the exact rational numbers are halfway between two consecutive multiple. For example, with denominator 11 we have 5/11 < 5.5/11 (exact middle) < 6/11 ; and round 5 11 rounded, and round 6 11 rounded.
Kaz Jun 16 '14 at 22:43 2014-06-16 22:43
source share