什么是计算平均,所有值的总和超过了双重的限制好的解决办法? [英] What is a good solution for calculating an average where the sum of all values exceeds a double's limits?

查看:174
本文介绍了什么是计算平均,所有值的总和超过了双重的限制好的解决办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,计算一个非常大的组双打(10 ^ 9值)的平均值。该值的总和超过上限的两倍,所以没有人知道任何整洁的小动作,用于计算,不也需要计算总和?

I have a requirement to calculate the average of a very large set of doubles (10^9 values). The sum of the values exceeds the upper bound of a double, so does anyone know any neat little tricks for calculating an average that doesn't require also calculating the sum?

我使用的Java 1.5。

I am using Java 1.5.

推荐答案

我想问您的第一个问题是这样的:

The very first issue I'd like to ask you is this:

  • 请您事先知道值多少?

如果没有,那么你别无选择,只能进行总结,并计数和差距,做了平均水平。如果双击不够高precision来处理这个问题,那么倒霉,你不能使用双击,你需要找到一个可以处理的数据类型。

If not, then you have little choice but to sum, and count, and divide, to do the average. If Double isn't high enough precision to handle this, then tough luck, you can't use Double, you need to find a data type that can handle it.

如果,在另一方面,你的执行的知道值的数量事前,你可以看看你真的做的和变化的如何的你这样做,但保持总的结果。

If, on the other hand, you do know the number of values beforehand, you can look at what you're really doing and change how you do it, but keep the overall result.

N个值,存储在某种集合,平均是这样的:

The average of N values, stored in some collection A, is this:

A[0]   A[1]   A[2]   A[3]          A[N-1]   A[N]
---- + ---- + ---- + ---- + .... + ------ + ----
 N      N      N      N               N       N

要计算这个结果的子集,可以计算分成相等大小的集合,这样你就可以做到这一点,3值集(假设值的数量是divisable 3,否则你需要一个不同的除数)

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)

/ 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

请注意,您在最后一组需要的相同尺寸的套,否则数字,相比于之前所有的设置将不会有足够的值,都会对最终结果的影响更大。

Note that you need equally sized sets, 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.

考虑数字1-7序列,如果你选择了3组大小,你会得到这样的结果:

Consider the numbers 1-7 in sequence, if you pick a set-size of 3, you'll get this result:

/ 1   2   3 \   / 4   5   6 \   / 7 \ 
| - + - + - | + | - + - + - | + | - |
\ 3   3   3 /   \ 3   3   3 /   \ 3 /
 -----------     -----------     ---
      y               y           y

这给:

     2   5   7/3
     - + - + ---
     y   y    y

如果y为3的所有集,你会得到这样的:

If y is 3 for all the sets, you get this:

     2   5   7/3
     - + - + ---
     3   3    3

这给:

2*3   5*3    7
--- + --- + ---
 9     9     9

这就是:

6   15   7
- + -- + -
9    9   9

这总计:

28
-- ~ 3,1111111111111111111111.........1111111.........
 9

1-7平均是4显然这是不行的。需要注意的是,如果你做了上述演习的数字1,2,3,4,5,6,7,0,0(注意两个零的尽头有),然后你会得到上述结果。

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 $相同数量pceeding它,但它将用零填充所有的遗漏值。

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.

因此​​,你需要同样规模的集。真倒霉,如果你的原始输入集由值的质数。

So, you need equally sized sets. Tough luck if your original input set consists of a prime number of values.

我有什么担心的,虽然在这里是precision损失。我不能完全肯定双击会给你足够好的precision在这种情况下,如果一开始不能容纳值的全部资金。

What I'm worried about here though is loss of precision. I'm not entirely sure Double will give you good enough precision in such a case, if it initially cannot hold the entire sum of the values.

这篇关于什么是计算平均,所有值的总和超过了双重的限制好的解决办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆