Conditions | 6 |
Paths | 6 |
Total Lines | 24 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
14 | public static function population(array $numbers, bool $sample = true): float |
||
15 | { |
||
16 | if (empty($numbers)) { |
||
17 | throw InvalidArgumentException::arrayCantBeEmpty(); |
||
18 | } |
||
19 | |||
20 | $n = count($numbers); |
||
21 | |||
22 | if ($sample && $n === 1) { |
||
23 | throw InvalidArgumentException::arraySizeToSmall(2); |
||
24 | } |
||
25 | |||
26 | $mean = Mean::arithmetic($numbers); |
||
27 | $carry = 0.0; |
||
28 | foreach ($numbers as $val) { |
||
29 | $carry += ($val - $mean) ** 2; |
||
30 | } |
||
31 | |||
32 | if ($sample) { |
||
33 | --$n; |
||
34 | } |
||
35 | |||
36 | return sqrt((float) ($carry / $n)); |
||
37 | } |
||
38 | |||
61 |