Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The very first issue I'd like to ask you is this:</p> <ul> <li>Do you know the number of values beforehand?</li> </ul> <p>If not, then you have little choice but to sum, and count, and divide, to do the average. If <code>Double</code> isn't high enough precision to handle this, then tough luck, you can't use <code>Double</code>, you need to find a data type that can handle it.</p> <p>If, on the other hand, you <em>do</em> know the number of values beforehand, you can look at what you're really doing and change <em>how</em> you do it, but keep the overall result.</p> <p>The average of N values, stored in some collection A, is this:</p> <pre><code>A[0] A[1] A[2] A[3] A[N-1] A[N] ---- + ---- + ---- + ---- + .... + ------ + ---- N N N N N N </code></pre> <p>To calculate subsets of this result, you can split up the calculation into equally sized sets, so you can do this, for 3-valued sets (assuming the number of values is divisable by 3, otherwise you need a different divisor)</p> <pre><code>/ A[0] A[1] A[2] \ / A[3] A[4] A[5] \ // A[N-1] A[N] \ | ---- + ---- + ---- | | ---- + ---- + ---- | \\ + ------ + ---- | \ 3 3 3 / \ 3 3 3 / // 3 3 / --------------------- + -------------------- + \\ -------------- N N N --- --- --- 3 3 3 </code></pre> <p>Note that you need <strong>equally sized sets</strong>, otherwise numbers in the last set, which will not have enough values compared to all the sets before it, will have a higher impact on the final result.</p> <p>Consider the numbers 1-7 in sequence, if you pick a set-size of 3, you'll get this result:</p> <pre><code>/ 1 2 3 \ / 4 5 6 \ / 7 \ | - + - + - | + | - + - + - | + | - | \ 3 3 3 / \ 3 3 3 / \ 3 / ----------- ----------- --- y y y </code></pre> <p>which gives:</p> <pre><code> 2 5 7/3 - + - + --- y y y </code></pre> <p>If y is 3 for all the sets, you get this:</p> <pre><code> 2 5 7/3 - + - + --- 3 3 3 </code></pre> <p>which gives:</p> <pre><code>2*3 5*3 7 --- + --- + --- 9 9 9 </code></pre> <p>which is:</p> <pre><code>6 15 7 - + -- + - 9 9 9 </code></pre> <p>which totals:</p> <pre><code>28 -- ~ 3,1111111111111111111111.........1111111......... 9 </code></pre> <p>The average of 1-7, is 4. Obviously this won't work. Note that if you do the above exercise with the numbers 1, 2, 3, 4, 5, 6, 7, 0, 0 (note the two zeroes at the end there), then you'll get the above result.</p> <p>In other words, if you can't split the number of values up into equally sized sets, the last set will be counted as though it has the same number of values as all the sets preceeding it, but it will be padded with zeroes for all the missing values.</p> <p>So, <strong>you need equally sized sets</strong>. Tough luck if your original input set consists of a prime number of values.</p> <p>What I'm worried about here though is loss of precision. I'm not entirely sure <code>Double</code> will give you good enough precision in such a case, if it initially cannot hold the entire sum of the values.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload