Integer division that rounds up: Difference between revisions

From wikiluntti
Line 1: Line 1:
== Introduction ==  
== Introduction ==  


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>.
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>
q = \frac{x+y-1}{y}
</math>

Revision as of 10:59, 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: