Skip to content

Commit

Permalink
Added average and median functions for Math
Browse files Browse the repository at this point in the history
  • Loading branch information
chajr committed Jul 7, 2017
1 parent 1b76e83 commit 1379344
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Calculation/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,34 @@ public static function end($edition, $used, $start, $timeNow)

return $end;
}

/**
* @param array $data
* @return float|int|null
*/
public static function median(array $data)
{
$median = null;

sort($data);
$valuesCount = count($data);
$key = ($valuesCount -1 ) / 2;

if ($valuesCount % 2) {
$median = $data[$key];
} else {
$median = ($data[$key] + $data[$key +1]) / 2;
}

return $median;
}

/**
* @param array $data
* @return float|int
*/
public static function average(array $data)
{
return array_sum($data) / count($data);
}
}

0 comments on commit 1379344

Please sign in to comment.