aboutsummaryrefslogtreecommitdiff
path: root/devdoc/rounding-errors.doc
diff options
context:
space:
mode:
authorSebastian Geerken <devnull@localhost>2015-06-01 22:00:10 +0200
committerSebastian Geerken <devnull@localhost>2015-06-01 22:00:10 +0200
commit1463c3936ce6a57352590b901c9dbd6bc2f2086d (patch)
tree3e7983b72fe63770fd2870b57683afd9421a36bd /devdoc/rounding-errors.doc
parenteb7ee4703ced8a02404eb0ebfa5b771fc5e916d5 (diff)
Split up user and developer documentation.
Diffstat (limited to 'devdoc/rounding-errors.doc')
-rw-r--r--devdoc/rounding-errors.doc35
1 files changed, 35 insertions, 0 deletions
diff --git a/devdoc/rounding-errors.doc b/devdoc/rounding-errors.doc
new file mode 100644
index 00000000..a442033e
--- /dev/null
+++ b/devdoc/rounding-errors.doc
@@ -0,0 +1,35 @@
+/** \page rounding-errors How to Avoid Rounding Errors
+
+(Probably, this is a standard algorithm, so if someone knows the name,
+drop me a note.)
+
+If something like
+
+\f[y_i = {x_i a \over b}\f]
+
+is to be calculated, and all numbers are integers, a naive
+implementation would result in something, for which
+
+\f[\sum y_i \ne {(\sum x_i) a \over b}\f]
+
+because of rounding errors, due to the integer division. This can be
+avoided by transforming the formula into
+
+\f[y_i = {(\sum_{j=0}^{j=i} x_j) a \over b} - \sum_{j=0}^{j=i-1} y_j\f]
+
+Of corse, when all \f$y_i\f$ are calculated in a sequence,
+\f$\sum_{j=0}^{j=i} x_j\f$ and \f$\sum_{j=0}^{j=i-1} y_j\f$ can be
+accumulated in the same loop. Regard this as sample:
+
+\code
+int n, x[n], a, b; // Should all be initialized.
+int y[n], cumX = 0, cumY = 0;
+
+for (int i = 0; i < n; i++) {
+ cumX += x[i]
+ y[i] = (cumX * a) / b - cumY;
+ cumY += y[i];
+}
+\endcode
+
+*/