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. 通用JDBC-demo

    1.JDBC 的工具包(utils):包含获取数据库连接, 关闭数据库资源等方法 JDBCTools_pro.java package com.app.utils; import java.beans ...

  2. 5.基于优化的攻击——CW

    CW攻击原论文地址——https://arxiv.org/pdf/1608.04644.pdf 1.CW攻击的原理 CW攻击是一种基于优化的攻击,攻击的名称是两个作者的首字母.首先还是贴出攻击算法的公 ...

  3. vs2019 cdkey 秘钥

    专业版NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y 企业版BF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  4. 输入,输出与Mad Libs游戏

    name1=input('请输入一个名字') name2=input('请输入一个名字') car=input('请输入一种车子') print('饥饿的{}看到{}穿着三级甲骑着{}下山'.form ...

  5. oc与c语言的相互调用

    一:OC调用C语言 C语言的.h文件 // // TestPrint.h // TestDemo // // Created by Techsun on 14-8-12. // Copyright ( ...

  6. Android手机camera和IMU的标定

    https://qingsimon.github.io/post/2018-12-28-android%E6%89%8B%E6%9C%BA%E7%9B%B8camera%E5%92%8Cimu%E7% ...

  7. DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件

    DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件   本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...

  8. django创建命令及配置

    创建项目django-admin startproject XXX(项目名字)运行项目 python manage.py runserver创建子应用python manage.py startapp ...

  9. 线性表->链式存储->线形链表(单链表)

    文字描述: 为了表示前后两个数据元素的逻辑关系,对于每个数据元素,除了存储其本身的信息之外(数据域),还需存储一个指示其直接后继的信息(即直接后继的存储位置,指针域). 示意图: 算法分析: 在单链表 ...

  10. 使用vue-cli3搭建一个项目

    前面说过用vue-cli3快速开发原型的搭建,下面来说一下搭建一个完整的项目 首先我们可以输入命令(创建一个项目名为test的项目) vue create test 输完这个命令后,会让你选择配置项, ...