1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Improved; |
||
6 | |||
7 | /** |
||
8 | * Return the arithmetic mean. |
||
9 | * If no elements are present, the result is NAN. |
||
10 | * |
||
11 | * @param iterable $iterable |
||
12 | * @return float |
||
13 | */ |
||
14 | function iterable_average(iterable $iterable): float |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | { |
||
16 | 13 | $count = 0; |
|
17 | 13 | $sum = 0; |
|
18 | |||
19 | 13 | foreach ($iterable as $item) { |
|
20 | 12 | $count++; |
|
21 | 12 | $sum += $item; |
|
22 | } |
||
23 | |||
24 | 13 | return $count == 0 ? \NAN : ($sum / $count); |
|
25 | } |
||
26 |