Recalculate an average value

人走茶凉 提交于 2019-12-25 14:18:18

问题


In my Java application I need to recalculate an average value based on the following data:

  1. I know current avg value - avgValue
  2. I know that avgValue is an average value for list of 12 values - count.

Based on this information how to recalculate avgValue when a new value is added to this list of previous 12 values. What is the new avgValue for list of 13 values - count + 1 ?


回答1:


Current Sum = avgValue * 12
New sum = current Sum + New value
New average = New sum / 13



回答2:


If you want to make sure you don't overflow your data type then you will want to avoid computing the sum. I would suggest using:

New average = avgValue + ((New value - avgValue) / 13)


来源:https://stackoverflow.com/questions/31904332/recalculate-an-average-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!