首页 > 其他 > 详细

Summary: Calculate average where sum exceed double limits

时间:2017-01-31 10:24:23      阅读:239      评论:0      收藏:0      [点我收藏+]
What is a good solution for calculating an average where the sum of all values exceeds a double‘s limits?

According to the highest vote in:

http://stackoverflow.com/questions/1930454/what-is-a-good-solution-for-calculating-an-average-where-the-sum-of-all-values-e

 

You can calculate the mean iteratively. This algorithm is simple, fast, you have to process each value just once, and the variables never get larger than the largest value in the set, so you won‘t get an overflow.

1 double mean(double[] ary) {
2   double avg = 0;
3   int t = 1;
4   for (double x : ary) {
5     avg += (x - avg) / t;
6     ++t;
7   }
8   return avg;
9 }

 

Summary: Calculate average where sum exceed double limits

原文:http://www.cnblogs.com/EdwardLiu/p/6358595.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!