1 | /**
|
2 | * When adding a new value to a list, one does not have to necessary
|
3 | * recompute the mean of the list in linear time. They can instead use
|
4 | * this function to compute the new mean by providing the current mean,
|
5 | * the number of elements in the list that produced it and the new
|
6 | * value to add.
|
7 | *
|
8 | * @since 2.5.0
|
9 | * @param {number} mean current mean
|
10 | * @param {number} n number of items in the list
|
11 | * @param {number} newValue the added value
|
12 | * @returns {number} the new mean
|
13 | *
|
14 | * @example
|
15 | * addToMean(14, 5, 53); // => 20.5
|
16 | */
|
17 | function addToMean(mean, n, newValue) {
|
18 | return mean + (newValue - mean) / (n + 1);
|
19 | }
|
20 |
|
21 | export default addToMean;
|