Integer division that rounds up

From wikiluntti
Revision as of 10:08, 8 July 2024 by Mol (talk | contribs) (→‎Proof)

Introduction

Usual integer division rounds down: Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac ab = \left \lfloor \frac ab \right \rfloor} 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 Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} divides Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} , and if not. Note that usual integer division rounds down.

Part 1. If Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} divides Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} we have Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x=ay} for some Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle a\in\mathbb N_+} . Thus we have

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} \left \lceil \frac{x+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 \\ &= \frac{ay}{y} \\ &= \frac{x}{y} \end{align} } because Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 0 \leq \frac{y-1}y < 1} . This is ok.