Created
December 17, 2015 14:31
-
-
Save turboezh/6fd5d23d6e15f7df2da6 to your computer and use it in GitHub Desktop.
Incremental average counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Считает среднее арифметическое, | |
* позволяя добавлять знаения последовательно без знания их количества. | |
*/ | |
class IncrementalAvg | |
{ | |
private $n = 0; | |
private $mean = 0; | |
/** | |
* @param number $value | |
* | |
* @return $this | |
*/ | |
public function addValue($value) | |
{ | |
$this->n++; | |
$this->mean = $this->mean + ($value - $this->mean) / $this->n; | |
return $this; | |
} | |
public function getResult() | |
{ | |
return $this->mean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment