How do you mathematically round a number?
How does someone mathematically round a number to its nearest integer?
For example 1.2
would round down to 1
and 1.7
would round up to 2
Solution 1:
You could use the "ceiling-function / floor-function" which always rounds an integer up/down ( http://en.wikipedia.org/wiki/Floor_and_ceiling_functions ).
If you want the number to be rounded the way you want you could just do
$\text{round}(x)= \lceil x-\frac{1}{2} \rceil$ Note that in this case $\text{round}(1.5)=1$
Or $\text{round'}(x)= \lfloor x+\frac{1}{2} \rfloor$ Note that in this case $\text{round'}(1.5)=2$
Solution 2:
This may not answer the original question, but when I came across this post I was looking for a more mathematical approach (rather than using a defined floor/ceil function). I ended up using modulo to define a floor, ceiling, and "round up from half"
Fractions
To floor round after dividing a numerator n
by a denominator d
:
$$Floor\left(\frac nd\right)\enspace =\enspace f(n, d) = \frac nd - \frac{n \bmod d}{d}$$
To ceiling round after dividing a numerator n
by a demonimator d
:
$$Ceil\left(\frac nd\right)\enspace =\enspace f(n, d) = \frac nd + \left( 1 - \frac{n \bmod d}{d}\right)$$
Decimals
To floor round a decimal x
:
$$Floor\left(x\right)\enspace =\enspace f(x) = x - \left( x \bmod 1 \right)$$
To celing round a decimal x
:
$$Ceil\left(x\right)\enspace =\enspace f(x) = x + \left(1 - \left(x \bmod 1\right) \right)$$
To round a decimal x
up from 0.5:
$$UpFromHalf\left(x\right)\enspace =\enspace f(x) = Floor(\left(x \bmod 1\right) + 0.5)$$
How it works:
The goal is to remove the decimal part of the divided number somehow.
The modulo part gets the remaining numerator. When we divide this by the denominator we get the decimal part of the divided number (always less than 1).
For floor rounding we eliminate the decimal part by subtract the decimal part of the divided number from the divided results.
For ceiling rounding we figure out the number that, when added to the divided results, will increase it to the next whole number.
Expectations:
A: $2/3 \Longrightarrow 0$,$\mspace50pt$ B: $3/3 \Longrightarrow 1$,$\mspace47pt$ C: $10/3 \Longrightarrow 3$
Dividing:
A: $\frac23 = 0.\overline6$,$\mspace56pt$ B: $\frac33 = 1$,$\mspace59pt$ C: $\frac{10}{3} = 3.\overline3$
Mod:
A: $2 \bmod 3 = 2$,$\mspace40pt$ B: $3 \bmod 3 = 0$,$\mspace36pt$ C: $10 \bmod 3 = 1$
Mod Result Divided:
A: $\frac{2 \bmod 3}{3} = 0.\overline6$,$\mspace40pt$ B: $\frac{3 \bmod 3}{3} = 0$,$\mspace40pt$ C: $\frac{10 \bmod 3}{3} = 0.\overline3$
Subtract out the remaining decimal
A: $\frac 23 - \frac{2 \bmod 3}{3} = 0$,$\mspace31pt$ B: $\frac 33 - \frac{3 \bmod 3}{3} = 1$,$\mspace24pt$ C: $\frac{10}{3} - \frac{10 \bmod 3}{3} = 3$