1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Litipk\BigNumbers; |
4
|
|
|
|
5
|
|
|
use Litipk\BigNumbers\Decimal as Decimal; |
6
|
|
|
use Litipk\Exceptions\InvalidArgumentTypeException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* git statu class that holds many important numeric constants |
11
|
|
|
* |
12
|
|
|
* @author Andreu Correa Casablanca <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class DecimalConstants |
15
|
|
|
{ |
16
|
|
|
private static $ZERO = null; |
17
|
|
|
private static $ONE = null; |
18
|
|
|
private static $NEGATIVE_ONE = null; |
19
|
|
|
|
20
|
|
|
private static $PI = null; |
21
|
|
|
private static $EulerMascheroni = null; |
22
|
|
|
|
23
|
|
|
private static $GoldenRatio = null; |
24
|
|
|
|
25
|
|
|
private static $LightSpeed = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Private constructor |
29
|
|
|
*/ |
30
|
|
|
private function __construct() |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Private clone method |
37
|
|
|
*/ |
38
|
|
|
private function __clone() |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
42 |
|
public static function zero() |
44
|
|
|
{ |
45
|
42 |
|
if (self::$ZERO === null) { |
46
|
1 |
|
self::$ZERO = Decimal::fromInteger(0); |
47
|
|
|
} |
48
|
42 |
|
return self::$ZERO; |
49
|
|
|
} |
50
|
|
|
|
51
|
61 |
|
public static function one() |
52
|
|
|
{ |
53
|
61 |
|
if (self::$ONE === null) { |
54
|
1 |
|
self::$ONE = Decimal::fromInteger(1); |
55
|
|
|
} |
56
|
61 |
|
return self::$ONE; |
57
|
|
|
} |
58
|
|
|
|
59
|
40 |
|
public static function negativeOne() |
60
|
|
|
{ |
61
|
40 |
|
if (self::$NEGATIVE_ONE === null) { |
62
|
1 |
|
self::$NEGATIVE_ONE = Decimal::fromInteger(-1); |
63
|
|
|
} |
64
|
40 |
|
return self::$NEGATIVE_ONE; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the Pi number. |
69
|
|
|
* @return Decimal |
70
|
|
|
*/ |
71
|
38 |
|
public static function pi() |
72
|
|
|
{ |
73
|
38 |
|
if (self::$PI === null) { |
74
|
1 |
|
self::$PI = Decimal::fromString( |
75
|
1 |
|
"3.14159265358979323846264338327950" |
76
|
|
|
); |
77
|
|
|
} |
78
|
38 |
|
return self::$PI; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the Euler's E number. |
83
|
|
|
* @param integer $scale |
84
|
|
|
* @return Decimal |
85
|
|
|
*/ |
86
|
3 |
|
public static function e($scale = 32) |
87
|
|
|
{ |
88
|
3 |
|
if (!is_int($scale)) { |
89
|
1 |
|
throw new InvalidArgumentTypeException(['integer'], gettype($scale)); |
90
|
|
|
} |
91
|
2 |
|
if ($scale < 0) { |
92
|
1 |
|
throw new \InvalidArgumentException("\$scale must be positive."); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return self::$ONE->exp($scale); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the Euler-Mascheroni constant. |
100
|
|
|
* @return Decimal |
101
|
|
|
*/ |
102
|
1 |
|
public static function eulerMascheroni() |
103
|
|
|
{ |
104
|
1 |
|
if (self::$EulerMascheroni === null) { |
105
|
1 |
|
self::$EulerMascheroni = Decimal::fromString( |
106
|
1 |
|
"0.57721566490153286060651209008240" |
107
|
|
|
); |
108
|
|
|
} |
109
|
1 |
|
return self::$EulerMascheroni; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the Golden Ration, also named Phi. |
114
|
|
|
* @return Decimal |
115
|
|
|
*/ |
116
|
1 |
|
public static function goldenRatio() |
117
|
|
|
{ |
118
|
1 |
|
if (self::$GoldenRatio === null) { |
119
|
1 |
|
self::$GoldenRatio = Decimal::fromString( |
120
|
1 |
|
"1.61803398874989484820458683436564" |
121
|
|
|
); |
122
|
|
|
} |
123
|
1 |
|
return self::$GoldenRatio; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the Light of Speed measured in meters / second. |
128
|
|
|
* @return Decimal |
129
|
|
|
*/ |
130
|
1 |
|
public static function lightSpeed() |
131
|
|
|
{ |
132
|
1 |
|
if (self::$LightSpeed === null) { |
133
|
1 |
|
self::$LightSpeed = Decimal::fromInteger(299792458); |
134
|
|
|
} |
135
|
1 |
|
return self::$LightSpeed; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|