15
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Are you asking for an algorithm to round the number? $\endgroup$
    – Chao Xu
    Jan 13, 2011 at 18:37

3 Answers 3

14
$\begingroup$

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 (x) + Floor(\left(x \bmod 1\right) + 0.5)$$

or:

$$UpFromHalf\left(x\right)\enspace =\enspace f(x) =\enspace Floor \left(x+0,5\right) =\enspace \left(x+0,5\right) - \left(\left(x+0,5\right) \bmod 1\right)$$

###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$

$\endgroup$
1
  • $\begingroup$ This was a helpful answer to this post in general. Thanks for the definitions and examples! $\endgroup$
    – wellplayed
    Sep 3, 2022 at 20:02
13
$\begingroup$

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$

$\endgroup$
2
  • $\begingroup$ round' is what I was after. I didn't know that floor and ceiling functions exists in mathematical syntax $\endgroup$
    – ParoX
    Jan 13, 2011 at 18:46
  • 5
    $\begingroup$ @BHare: in «mathematical syntax» everything you can come up with exists. $\endgroup$ Jan 14, 2011 at 0:39
2
$\begingroup$

This is equivalent to looking at the floor and ceiling functions. See this. In particular, rounding a number $y$ to an integer $q$ is the same thing as looking as $q = \text{floor}(y)$ or $q = \text{ceil}(y)$.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .