Integer division that rounds up: Difference between revisions

From wikiluntti
Line 5: Line 5:
q = \frac{x+y-1}{y} = \left \lceil \frac xy \right \rceil
q = \frac{x+y-1}{y} = \left \lceil \frac xy \right \rceil
</math>
</math>
== Proof ==
Proof is in two parts; 1st if <math>y</math> divides <math>x</math>, and if not.
'''Part 1'''. If <math>y</math> divides <math>x</math> we have <math>x=ay</math> for some <math>a\in\mathbb N_+</math>.

Revision as of 11:03, 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.

Part 1. If divides we have for some .