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.

 double mean(double[] ary) {
double avg = 0;
int t = 1;
for (double x : ary) {
avg += (x - avg) / t;
++t;
}
return avg;
}

Summary: Calculate average where sum exceed double limits的更多相关文章

  1. MATLAB中文论坛帖子整理(GUI)

    MATLAB中文论坛帖子整理(GUI) 目   录  1.GUI新手之——教你读懂GUI的M文件... 10 2.GUI程序中改变current directory引起的问题... 15 3.GUI中 ...

  2. Max double slice sum 的解法

    1. 上题目: Task description A non-empty zero-indexed array A consisting of N integers is given. A tripl ...

  3. Average Sleep Time CodeForces - 808B (前缀和)

    It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, on ...

  4. HDU 2993 - MAX Average Problem - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Consider a simple sequence which only contains p ...

  5. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  6. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  7. EMA计算的C#实现(c# Exponential Moving Average (EMA) indicator )

    原来国外有个源码(TechnicalAnalysisEngine src 1.25)内部对EMA的计算是: var copyInputValues = input.ToList(); for (int ...

  8. Max Sum of Max-K-sub-sequence(单调队列)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

随机推荐

  1. LCA最近公共祖先

    不会 准备研究一波!!! #include<bits/stdc++.h> ; using namespace std; vector<int> g[maxn]; ][maxn] ...

  2. manjaro安装及设置

    因我的笔记本(联想的拯救者)昨晚打开后什么都还没做就被更新系统“抢走”了画面导致按什么都不管用 所以就想起能不能不用win系统,都知道linux比win稳定,so....就找到了manjaro.以下是 ...

  3. 网络编程-Python高级语法-闭包

    什么叫闭包?通俗来说就是函数里嵌套函数,从表现形式来看,内部函数引用外部函数的作用域里的变量,那么内部函数就称为闭包 举例说明: 1.闭包=函数块+定义函数时的环境,inner就是函数块,x就是环境 ...

  4. Konva入门教程

    啥是 Konva Konva 是一个 canvas 库,可以让我们像操作 DOM 一样来操作 canvas,并提供了对 canvas 中元素的事件机制,拖放操作的支持.所以,用它来做一个拼图游戏什么的 ...

  5. PageHelper补充

    统计总数 Page<?> page = PageHelper.startPage(1,-1); long count = page.getTotal(); 分页 pageNum - 第N页 ...

  6. [HDU4864]Task (贪心)

    此图和上一篇博客的图一起看有奇效 题意 https://vjudge.net/problem/HDU-4864 思路 贪心 代码 by lyd 我实在是敲不来 #include <iostrea ...

  7. hibernate 报query result offset is not supported

    在配置hibernate.cfg.xml时需指定使用数据库的方言: 例: <property name="dialect">org.hibernate.dialect. ...

  8. [LeetCode] Ambiguous Coordinates 模糊的坐标

    We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we re ...

  9. python pymsql的用法 180903

    一.1.pymysql 的下载pip3 install pymysql2.pymysql的使用import pymysqlname=input("请输入用户名:")password ...

  10. dtIntersectSegmentPoly2D 2D上的线段与多边形相交计算 产生结果:是否相交,线段跨越的开始和结束百分比,相交的边

    dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax): http://geomalgori ...