1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* contains some helpful mathematics models |
4
|
|
|
* |
5
|
|
|
* @package BlueData |
6
|
|
|
* @subpackage Data |
7
|
|
|
* @author Michał Adamiak <[email protected]> |
8
|
|
|
* @copyright bluetree-service |
9
|
|
|
*/ |
10
|
|
|
namespace BlueData\Calculation; |
11
|
|
|
|
12
|
|
|
class Math |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Calculates percent difference between two numbers |
16
|
|
|
* |
17
|
|
|
* @param int|float $from |
18
|
|
|
* @param int|float $into |
19
|
|
|
* @return int|float |
20
|
|
|
*/ |
21
|
1 |
|
public static function getPercentDifference($from, $into) |
22
|
|
|
{ |
23
|
1 |
|
if ($into === 0) { |
24
|
1 |
|
return 0; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
return 100 - (($into / $from) *100); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Calculate which percent is one number of other |
32
|
|
|
* |
33
|
|
|
* @param float $part value that is percent of other value |
34
|
|
|
* @param float $all value to check percent |
35
|
|
|
* @return integer|float |
36
|
|
|
*/ |
37
|
1 |
|
public static function numberToPercent($part, $all) |
38
|
|
|
{ |
39
|
1 |
|
if ($all === 0) { |
|
|
|
|
40
|
1 |
|
return 0; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return ($part / $all) *100; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Calculate percent form value |
48
|
|
|
* |
49
|
|
|
* @param float $part value that will be percent of other value |
50
|
|
|
* @param float $all value from calculate percent |
51
|
|
|
* @return integer|float |
52
|
|
|
*/ |
53
|
1 |
|
public static function percent($part, $all) |
54
|
|
|
{ |
55
|
1 |
|
if ($all === 0) { |
|
|
|
|
56
|
1 |
|
return 0; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return ($part / 100) *$all; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Estimate time to end, by given current usage value and max value |
64
|
|
|
* |
65
|
|
|
* @param float $edition maximum number of value |
66
|
|
|
* @param float $used how many was used |
67
|
|
|
* @param integer $start start time in unix timestamp |
68
|
|
|
* @param integer $timeNow current unix timestamp |
69
|
|
|
* @return integer estimated end time in unix timestamp |
70
|
|
|
*/ |
71
|
1 |
|
public static function end($edition, $used, $start, $timeNow) |
72
|
|
|
{ |
73
|
1 |
|
if (!$used) { |
74
|
1 |
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$end = $edition / ($used / ($timeNow - $start)); |
78
|
1 |
|
$end += $timeNow; |
79
|
|
|
|
80
|
1 |
|
return $end; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $data |
85
|
|
|
* @return float|int |
86
|
|
|
*/ |
87
|
2 |
|
public static function median(array $data) |
88
|
|
|
{ |
89
|
2 |
|
sort($data); |
90
|
2 |
|
$valuesCount = count($data); |
91
|
2 |
|
$key = ($valuesCount -1 ) / 2; |
92
|
2 |
|
$median = $data[$key]; |
93
|
|
|
|
94
|
2 |
|
if (!($valuesCount % 2)) { |
95
|
1 |
|
$median = ($median + $data[$key +1]) / 2; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
2 |
|
return $median; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $data |
103
|
|
|
* @return float|int |
104
|
|
|
*/ |
105
|
2 |
|
public static function average(array $data) |
106
|
|
|
{ |
107
|
2 |
|
return array_sum($data) / count($data); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|