|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Litipk\BigNumbers; |
|
5
|
|
|
|
|
6
|
|
|
use Litipk\BigNumbers\Decimal as Decimal; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class that holds many important numeric constants |
|
11
|
|
|
* |
|
12
|
|
|
* @author Andreu Correa Casablanca <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class DecimalConstants |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Decimal */ |
|
17
|
|
|
private static $ZERO = null; |
|
18
|
|
|
/** @var Decimal */ |
|
19
|
|
|
private static $ONE = null; |
|
20
|
|
|
/** @var Decimal */ |
|
21
|
|
|
private static $NEGATIVE_ONE = null; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Decimal */ |
|
24
|
|
|
private static $PI = null; |
|
25
|
|
|
/** @var Decimal */ |
|
26
|
|
|
private static $EulerMascheroni = null; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Decimal */ |
|
29
|
|
|
private static $GoldenRatio = null; |
|
30
|
|
|
/** @var Decimal */ |
|
31
|
|
|
private static $SilverRatio = null; |
|
32
|
|
|
/** @var Decimal */ |
|
33
|
|
|
private static $LightSpeed = null; |
|
34
|
|
|
|
|
35
|
|
|
private function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function __clone() |
|
40
|
|
|
{ |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
36 |
|
public static function zero(): Decimal |
|
44
|
|
|
{ |
|
45
|
36 |
|
if (null === self::$ZERO) { |
|
46
|
1 |
|
self::$ZERO = Decimal::fromInteger(0); |
|
47
|
|
|
} |
|
48
|
36 |
|
return self::$ZERO; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
66 |
|
public static function one(): Decimal |
|
52
|
|
|
{ |
|
53
|
66 |
|
if (null === self::$ONE) { |
|
54
|
1 |
|
self::$ONE = Decimal::fromInteger(1); |
|
55
|
|
|
} |
|
56
|
66 |
|
return self::$ONE; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
40 |
|
public static function negativeOne(): Decimal |
|
60
|
|
|
{ |
|
61
|
40 |
|
if (null === self::$NEGATIVE_ONE) { |
|
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(): Decimal |
|
72
|
|
|
{ |
|
73
|
38 |
|
if (null === self::$PI) { |
|
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
|
2 |
|
public static function e(int $scale = 32): Decimal |
|
87
|
|
|
{ |
|
88
|
2 |
|
if ($scale < 0) { |
|
89
|
1 |
|
throw new \InvalidArgumentException("\$scale must be positive."); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return static::one()->exp($scale); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the Euler-Mascheroni constant. |
|
97
|
|
|
* @return Decimal |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public static function eulerMascheroni(): Decimal |
|
100
|
|
|
{ |
|
101
|
1 |
|
if (null === self::$EulerMascheroni) { |
|
102
|
1 |
|
self::$EulerMascheroni = Decimal::fromString( |
|
103
|
1 |
|
"0.57721566490153286060651209008240" |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
1 |
|
return self::$EulerMascheroni; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the Golden Ration, also named Phi. |
|
111
|
|
|
* @return Decimal |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function goldenRatio(): Decimal |
|
114
|
|
|
{ |
|
115
|
1 |
|
if (null === self::$GoldenRatio) { |
|
116
|
1 |
|
self::$GoldenRatio = Decimal::fromString( |
|
117
|
1 |
|
"1.61803398874989484820458683436564" |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
1 |
|
return self::$GoldenRatio; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the Silver Ratio. |
|
125
|
|
|
* @return Decimal |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public static function silverRatio(): Decimal |
|
128
|
|
|
{ |
|
129
|
1 |
|
if (null === self::$SilverRatio) { |
|
130
|
1 |
|
self::$SilverRatio = Decimal::fromString( |
|
131
|
1 |
|
"2.41421356237309504880168872420970" |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
1 |
|
return self::$SilverRatio; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the Light of Speed measured in meters / second. |
|
139
|
|
|
* @return Decimal |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public static function lightSpeed(): Decimal |
|
142
|
|
|
{ |
|
143
|
1 |
|
if (null === self::$LightSpeed) { |
|
144
|
1 |
|
self::$LightSpeed = Decimal::fromInteger(299792458); |
|
145
|
|
|
} |
|
146
|
1 |
|
return self::$LightSpeed; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|