Integer division that rounds up: Difference between revisions
From wikiluntti
(→Proof) |
(→Proof) |
||
Line 11: | Line 11: | ||
'''Part 1'''. If <math>y</math> divides <math>x</math> we have <math>x=ay</math> for some <math>a\in\mathbb N_+</math>. Thus we have | '''Part 1'''. If <math>y</math> divides <math>x</math> we have <math>x=ay</math> for some <math>a\in\mathbb N_+</math>. Thus we have | ||
<math> | <math> | ||
\begin{align} | \begin{align} | ||
\left \lceil \frac{x+y-1}{y} \right \rceil | \left \lceil \frac{x+y-1}{y} \right \rceil | ||
= | &= | ||
\left \lceil \frac{x}{y} + \frac{y-1}y \right \rceil \\ | \left \lceil \frac{x}{y} + \frac{y-1}y \right \rceil \\ | ||
= | &= | ||
\left \lceil \frac{ay}{y} + \frac{y-1}y \right \rceil \\ | \left \lceil \frac{ay}{y} + \frac{y-1}y \right \rceil \\ | ||
= | &= | ||
\frac{ay}{y} \\ | \frac{ay}{y} \\ | ||
= | &= | ||
\frac{x}{y} | \frac{x}{y} | ||
\end{align} | \end{align} | ||
</math> | </math> | ||
because <math>0 \leq \frac{y-1}y < 1</math>. This is ok. | because <math>0 \leq \frac{y-1}y < 1</math>. This is ok. |
Revision as of 11:08, 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 is ok.