-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.php
51 lines (47 loc) · 1.51 KB
/
math.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class math {
function is_odd_even($number) {
return ($number & 1) ? "odd" : "even";
}
/**
* The function (Mean, Median, Mode, Range) will calculate the Mean, Median, Mode, or Range of an array.
The function automatically defaults to Mean (average).
* @param type $array
* @param type $output
* @return boolean
*/
function sta($array, $output = 'mean') {
if (!is_array($array)) {
return FALSE;
} else {
switch ($output) {
case 'mean':
$count = count($array);
$sum = array_sum($array);
$total = $sum / $count;
break;
case 'median':
rsort($array);
$middle = round(count($array) / 2);
$total = $array[$middle - 1];
break;
case 'mode':
$v = array_count_values($array);
arsort($v);
foreach ($v as $k => $v) {
$total = $k;
break;
}
break;
case 'range':
sort($array);
$sml = $array[0];
rsort($array);
$lrg = $array[0];
$total = $lrg - $sml;
break;
}
return $total;
}
}
}