Integer division that rounds up: Difference between revisions

From wikiluntti
Line 3: Line 3:
Usual integer division rounds down: <math>\frac ab = \left \lfloor \frac ab \right \rfloor</math> for <math>a,b \in \mathbb N, b\neq 0</math>. To round up (if overflow is not an issue), you can use following algorithm with the usual roundig down division:
Usual integer division rounds down: <math>\frac ab = \left \lfloor \frac ab \right \rfloor</math> for <math>a,b \in \mathbb N, b\neq 0</math>. To round up (if overflow is not an issue), you can use following algorithm with the usual roundig down division:
<math>
<math>
q = \frac{x+y-1}{y} = \left \lceil \frac xy \right \rceil.
q = \frac{x+y-1}{y} = \left \lceiling \frac xy \right \rceiling.
</math>
</math>

Revision as of 11:00, 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: Failed to parse (syntax error): {\displaystyle q = \frac{x+y-1}{y} = \left \lceiling \frac xy \right \rceiling. }