Skip to main content
Clearer explanation. Fixed some typos.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

There are many waysrounding methods that round an integer to the nearest integer (all, all of themwhich are basically the same except on the half-integers). The sum of integers obtainedreturned will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the abovethem will ensure the rule for sum, "the sum of output integers must be as close to the sum of original numbers as possible". To achieve thatFor example, the nearest integer to $0.6$ and $0.7$ is $1$ but the nearest integer to their sum $0.6+0.7=1.3$ is $1$ instead of $1+1 =2$. Hence to satisfy the rule of sum, we needcannot guarantee that each integer is mapped to keep trackits nearest integer.

Let us pick one of the gap/error betweenrounding methods as $\mathcal R$, which will specify the two sumsexact meaning of "as close as possible". For methods below, we will ensure that $$\mathcal R(\text{sum of input numbers})=\text{sum of output integers}$$ while trying to round integers to their respective nearest integers fairly.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let
Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half and ..." methods above$s = \{a_1\} + \{a_2\} + \cdots + \{a_n\}$. Then

  • roundRound down $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • roundRound up $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$otherwise.

To be fair, sort $a_i$ by $\{a_i\}$ before rounding so that, for example, $4.8$ will be roundrounded up instead of $7.6$ if one of them should be roundrounded up.

Online Method

Input: a source that produces numbers
Output: numbers rounded to integers
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

MixedBuffered Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtainedreturned in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ by $\{a_i\}$ before rounding so that, for example, $4.8$ will be round up instead of $7.6$ if one of them should be round up.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtained in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

There are many rounding methods that round an integer to the nearest integer, all of which are the same except on the half-integers. The sum of integers returned will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of them will ensure the rule for sum, "the sum of output integers must be as close to the sum of original numbers as possible". For example, the nearest integer to $0.6$ and $0.7$ is $1$ but the nearest integer to their sum $0.6+0.7=1.3$ is $1$ instead of $1+1 =2$. Hence to satisfy the rule of sum, we cannot guarantee that each integer is mapped to its nearest integer.

Let us pick one of the rounding methods as $\mathcal R$, which will specify the exact meaning of "as close as possible". For methods below, we will ensure that $$\mathcal R(\text{sum of input numbers})=\text{sum of output integers}$$ while trying to round integers to their respective nearest integers fairly.

Offline Method

Suppose numbers $a_1, a_2, \cdots, a_n$ are given.
Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$ and $s = \{a_1\} + \{a_2\} + \cdots + \{a_n\}$.

  • Round down $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • Round up $a_i$ to $\lfloor a_i\rfloor + 1$ otherwise.

To be fair, sort $a_i$ by $\{a_i\}$ before rounding so that, for example, $4.8$ will be rounded up instead of $7.6$ if one of them should be rounded up.

Online Method

Input: a source that produces numbers
Output: numbers rounded to integers
Procedure:

  1. Let number $gap$ be $0$
  2. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Buffered Method

We can mix the offline method and the online method by repeating the following procedure after initializing $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers returned in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

Per justhalf's feedback, clarified how the given numbers should be sorted.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ by $\{a_i\}$ before rounding so that, for example, $4.8$ will be round up instead of $7.6$ if one of them should be round up.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtained in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ before rounding.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtained in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ by $\{a_i\}$ before rounding so that, for example, $4.8$ will be round up instead of $7.6$ if one of them should be round up.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtained in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

Per Paŭlo Ebermann' feedback, removed the list of all the "round half *" methods.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

  • Round half up. $\ x\to\lfloor x+0.5\rfloor$.
  • Round half down. $\ x\to \lceil x-0.5\rceil$.
  • Round half toward zero. $\ x\to \text{sgn}(x)\lceil|x|-0.5\rceil$
  • Round half away from zero. $\ x\to \text{sgn}(x)\lfloor|x|+0.5\rfloor$
  • Round half to even (banker's rounding).
  • Round half to odd.
  • Round half up or down alternately.
  • Round half up or down randomly.
  • Stochastic rounding.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ before rounding.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Adjust $gap$ byAdd the difference ofbetween the sum of the numbers obtained in step 1 and the sum of the resultingoriginal numbers to $gap$.

The size of each block of numbers is up to your choice.

There are many ways that round to the nearest integer. The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

  • Round half up. $\ x\to\lfloor x+0.5\rfloor$.
  • Round half down. $\ x\to \lceil x-0.5\rceil$.
  • Round half toward zero. $\ x\to \text{sgn}(x)\lceil|x|-0.5\rceil$
  • Round half away from zero. $\ x\to \text{sgn}(x)\lfloor|x|+0.5\rfloor$
  • Round half to even (banker's rounding).
  • Round half to odd.
  • Round half up or down alternately.
  • Round half up or down randomly.
  • Stochastic rounding.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ before rounding.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Adjust $gap$ by the difference of the sum of the numbers and the sum of the resulting numbers.

The size of each block of numbers is up to your choice.

There are many ways that round to the nearest integer (all of them are basically the same except on the half-integers). The sum of integers obtained will be close to the sum of the original numbers on average when the distribution of the fraction parts of the numbers are roughly symmetric to $0.5$. One of those methods might be good enough for you in practice.

However, none of the above will ensure "the sum of integers must be as close to the sum of original numbers as possible". To achieve that, we need to keep track of the gap/error between the two sums.

Offline Method

Suppose a list of numbers $a_1, a_2, \cdots, a_n$ are given. Let $\{a_i\}=a_i-\lfloor a_i\rfloor$ be the fractional part of $a_i$.

Let $s = \{a_1\} + \{a_i\} + \cdots + \{a_n\}$. Let $\mathcal R$ be one of the "round half ..." methods above. Then

  • round $a_i$ to $\lfloor a_i\rfloor$ for $i\le n-\mathcal R(s)$,
  • round $a_i$ to $\lfloor a_i\rfloor + 1$ for $i\gt n-\mathcal R(s)$.

To be fair, sort $a_i$ before rounding.

Online Method

Input: a source that produces numbers
Output: numbers rounded
Procedure:

  1. Let number $gap$ be $0$
  2. Pick one of the "round half ..." methods as $\mathcal R$.
  3. For each number $num$ in the source:
    1. Let $num{\_}rounded=\mathcal R(gap + num)$. Output $num{\_}rounded$.
    2. Add $num - num{\_}rounded$ to $gap$.

Mixed Method

We can mix the offline method and the online method by repeating the following procedure after initializing number $gap=0$.

  1. Apply the offline method to the next block of numbers, including $gap$ as a summand for $s$ as well.
  2. Add the difference between the sum of the numbers obtained in step 1 and the sum of the original numbers to $gap$.

The size of each block of numbers is up to your choice.

Clearer nouns.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91
Loading
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91
Loading