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. 在react-native项目中使用iconfont自定义图标库(android)

    1. 安装react-native-vector-icons yarn add react-native-vector-icons react-native link 如果没有关联成功的话,可以参考官 ...

  2. Python学习(三十六)—— Cookie、Session和自定义分页

    一.Django中操作Cookie 获取Cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR ...

  3. 蓝桥杯 全球变暖(dfs)

    标题:全球变暖 [题目描述]你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: 其中"上下左右"四个方向上连在一起的 ...

  4. JAVA基础复习与总结<二>构造方法_static关键字_final关键字

    构造方法详解 构造器也叫做构造方法(constructor),用于对象的初始化. class Person2 { String name; int age; public Person2(String ...

  5. Thinkphp3.2.3加载外部类并调用类里面的方法 获取token

    例如:加载七牛上传类(thinkphp自带的) $qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig' ...

  6. mongodb配置问题

    1.安装好mongodb后需要在控制行输入很长的一窜很麻烦,可以新建一个.bat文件 cd F:\mongodb\Server\3.0\binmongod --dbpath "F:\mong ...

  7. React_基本原理_ajax

    React 基本原理 初始化显示界面 创建虚拟DOM树 渲染到 原生 DOM 树 绘制界面显示 更新界面 setState() 更新状态机 重新创建虚拟 DOM 树 新/旧树比较差异 (执行一次 DO ...

  8. react_app 项目开发

    react_app 项目开发 npm install -g create-react-app npm root -g        // 查看安装包位置 创建项目 create-react-app m ...

  9. poj 1375

    一道解析几何么,,, 其实就是求直线与圆的切线. 看到方法有很多,比如根据角度之类的. 这里主要用到了初中的几何知识. 考虑这幅图. 首先可以根据相似三角形知道b的长度,同时圆心与点的方向也知道. 那 ...

  10. MySQL数据库下载安装和DataGrip的下载安装和破解

    一: 数据库下载 地址:官网https://dev.mysql.com/downloads/file/?id=482771;如果参数id失效,就选择之前的版本,5.7就可以,太新的没人用,老的很稳定. ...