问题
In my Java application I need to recalculate an average value based on the following data:
- I know current avg value -
avgValue
- 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