Integer division that rounds up: Difference between revisions
From wikiluntti
(→Proof) |
(→Proof) |
||
Line 57: | Line 57: | ||
</math> | </math> | ||
which was the question. | which was the question. | ||
== References == | |||
https://math.stackexchange.com/questions/2591316/proof-for-integer-division-algorithm-that-rounds-up | |||
For more details, see Algorithmic problem solving by R Backhouse 2011, Exercise 15.12 (following the definition 15.22). |
Latest revision as of 11:29, 8 July 2024
Introduction
Usual integer division rounds down: for . To round up (if overflow is not an issue), you can use following algorithm with the usual roundig down division:
Proof
Proof is in two parts; 1st if divides , and if not. Note that usual integer division rounds down.
Part 1. If divides we have for some . Thus we have
because . This part is ok.
Part 2. If does not divide we have for some and . Thus we have
Which is one greater (the ceiling).
Combine the results, and we have which was the question.
References
https://math.stackexchange.com/questions/2591316/proof-for-integer-division-algorithm-that-rounds-up
For more details, see Algorithmic problem solving by R Backhouse 2011, Exercise 15.12 (following the definition 15.22).