Skip to content

Instantly share code, notes, and snippets.

@turboezh
Created December 17, 2015 14:31
Show Gist options
  • Save turboezh/6fd5d23d6e15f7df2da6 to your computer and use it in GitHub Desktop.
Save turboezh/6fd5d23d6e15f7df2da6 to your computer and use it in GitHub Desktop.
Incremental average counter
/**
* Считает среднее арифметическое,
* позволяя добавлять знаения последовательно без знания их количества.
*/
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