|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Samsara\Fermat\Provider; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Samsara\Exceptions\SystemError\PlatformError\MissingPackage; |
|
8
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
|
9
|
|
|
use Samsara\Fermat\Enums\CalcMode; |
|
10
|
|
|
use Samsara\Fermat\Enums\NumberBase; |
|
11
|
|
|
use Samsara\Fermat\Numbers; |
|
12
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface; |
|
13
|
|
|
use Samsara\Fermat\Values\ImmutableDecimal; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class ConstantProvider |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
private static DecimalInterface $pi; |
|
22
|
|
|
private static DecimalInterface $e; |
|
23
|
|
|
private static DecimalInterface $ln10; |
|
24
|
|
|
private static DecimalInterface $ln2; |
|
25
|
|
|
private static DecimalInterface $ln1p1; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param int $digits |
|
29
|
|
|
* @return string |
|
30
|
|
|
* @throws IntegrityConstraint |
|
31
|
|
|
* @throws MissingPackage |
|
32
|
|
|
*/ |
|
33
|
5 |
|
public static function makePi(int $digits): string |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
5 |
|
if (isset(self::$pi) && self::$pi->numberOfDecimalDigits() >= $digits) { |
|
37
|
2 |
|
return self::$pi->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
$internalScale = ($digits*2) + 10; |
|
41
|
|
|
|
|
42
|
5 |
|
$C = Numbers::make(Numbers::IMMUTABLE, '10005', $internalScale)->setMode(CalcMode::Precision)->sqrt($internalScale)->multiply(426880); |
|
43
|
5 |
|
$M = Numbers::make(Numbers::IMMUTABLE, '1', $internalScale)->setMode(CalcMode::Precision); |
|
44
|
5 |
|
$L = Numbers::make(Numbers::IMMUTABLE, '13591409', $internalScale)->setMode(CalcMode::Precision); |
|
45
|
5 |
|
$K = Numbers::make(Numbers::IMMUTABLE, '6', $internalScale)->setMode(CalcMode::Precision); |
|
46
|
5 |
|
$X = Numbers::make(Numbers::IMMUTABLE, '1')->setMode(CalcMode::Precision); |
|
47
|
5 |
|
$sum = Numbers::make(Numbers::MUTABLE,'0', $internalScale + 2)->setMode(CalcMode::Precision); |
|
48
|
5 |
|
$termNum = 0; |
|
49
|
5 |
|
$one = Numbers::makeOne($internalScale)->setMode(CalcMode::Precision); |
|
50
|
|
|
|
|
51
|
5 |
|
$continue = true; |
|
52
|
|
|
|
|
53
|
5 |
|
while ($continue) { |
|
54
|
5 |
|
$term = $M->multiply($L)->divide($X, $internalScale); |
|
55
|
|
|
|
|
56
|
5 |
|
if ($termNum > $internalScale) { |
|
57
|
5 |
|
$continue = false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
$sum->add($term); |
|
61
|
|
|
|
|
62
|
5 |
|
$M = $M->multiply($K->pow(3)->subtract($K->multiply(16))->divide($one->add($termNum)->pow(3), $internalScale)); |
|
63
|
5 |
|
$L = $L->add(545140134); |
|
64
|
5 |
|
$X = $X->multiply('-262537412640768000'); |
|
65
|
5 |
|
$K = $K->add(12); |
|
66
|
5 |
|
$termNum++; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
$pi = $C->divide($sum, $internalScale); |
|
70
|
|
|
|
|
71
|
5 |
|
self::$pi = $pi->truncateToScale($digits); |
|
72
|
|
|
|
|
73
|
5 |
|
return $pi->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Consider also: sum [0 -> INF] { (2n + 2) / (2n + 1)! } |
|
79
|
|
|
* |
|
80
|
|
|
* This converges faster (though it's unclear if the calculation is actually faster), and can be represented by this |
|
81
|
|
|
* set of Fermat calls: |
|
82
|
|
|
* |
|
83
|
|
|
* SequenceProvider::nthEvenNumber($n + 1)->divide(SequenceProvider::nthOddNumber($n)->factorial()); |
|
84
|
|
|
* |
|
85
|
|
|
* Perhaps by substituting the nthOddNumber()->factorial() call with something tracked locally, the performance can |
|
86
|
|
|
* be improved. Current performance is acceptable even out past 200 digits. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $digits |
|
89
|
|
|
* @return string |
|
90
|
|
|
* @throws IntegrityConstraint |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public static function makeE(int $digits): string |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
2 |
|
if (isset(self::$e) && self::$e->numberOfDecimalDigits() >= $digits) { |
|
96
|
|
|
return self::$e->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$internalScale = $digits + 3; |
|
100
|
|
|
|
|
101
|
2 |
|
$one = Numbers::makeOne($internalScale+5)->setMode(CalcMode::Precision); |
|
102
|
2 |
|
$denominator = Numbers::make(Numbers::MUTABLE, '1', $internalScale)->setMode(CalcMode::Precision); |
|
103
|
2 |
|
$e = Numbers::make(NUmbers::MUTABLE, '2', $internalScale)->setMode(CalcMode::Precision); |
|
104
|
2 |
|
$n = Numbers::make(Numbers::MUTABLE, '2', $internalScale)->setMode(CalcMode::Precision); |
|
105
|
|
|
|
|
106
|
2 |
|
$continue = true; |
|
107
|
|
|
|
|
108
|
2 |
|
while ($continue) { |
|
109
|
2 |
|
$denominator->multiply($n); |
|
110
|
2 |
|
$n->add($one); |
|
111
|
2 |
|
$term = $one->divide($denominator); |
|
112
|
|
|
|
|
113
|
2 |
|
if ($term->numberOfLeadingZeros() > $internalScale || $term->isEqual(0)) { |
|
|
|
|
|
|
114
|
2 |
|
$continue = false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
$e->add($term); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
self::$e = $e->truncateToScale($digits); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
2 |
|
return $e->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* The lnScale() implementation is very efficient, so this is probably our best bet for computing more digits of |
|
128
|
|
|
* ln(10) to provide. |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $digits |
|
131
|
|
|
* @return string |
|
132
|
|
|
* @throws IntegrityConstraint |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public static function makeLn10(int $digits): string |
|
135
|
|
|
{ |
|
136
|
|
|
|
|
137
|
1 |
|
if (isset(self::$ln10) && self::$ln10->numberOfDecimalDigits() >= $digits) { |
|
138
|
|
|
return self::$ln10->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
$ln10 = Numbers::make(Numbers::IMMUTABLE, 10, $digits+2)->setMode(CalcMode::Precision); |
|
142
|
1 |
|
$ln10 = $ln10->ln(); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
1 |
|
self::$ln10 = $ln10; |
|
145
|
|
|
|
|
146
|
1 |
|
return $ln10->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* This function is a special case of the ln() function where x can be represented by (n + 1)/n, where n is an |
|
152
|
|
|
* integer. This particular special case converges extremely rapidly. For ln(2), n = 1. |
|
153
|
|
|
* |
|
154
|
|
|
* @param int $digits |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function makeLn2(int $digits): string |
|
158
|
1 |
|
{ |
|
159
|
|
|
|
|
160
|
|
|
if (isset(self::$ln2) && self::$ln2->numberOfDecimalDigits() >= $digits) { |
|
161
|
1 |
|
return self::$ln2->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$twoThirds = Numbers::make(Numbers::IMMUTABLE, str_pad('0.', $digits+3, '6')); |
|
165
|
1 |
|
$nine = Numbers::make(Numbers::IMMUTABLE, 9, $digits+3); |
|
166
|
1 |
|
$ln2 = self::_makeLnSpecial($digits, $nine, $twoThirds); |
|
|
|
|
|
|
167
|
1 |
|
|
|
168
|
1 |
|
self::$ln2 = $ln2; |
|
169
|
1 |
|
|
|
170
|
1 |
|
return $ln2->getValue(NumberBase::Ten); |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
/** |
|
175
|
|
|
* This function is a special case of the ln() function where x can be represented by (n + 1)/n, where n is an |
|
176
|
1 |
|
* integer. This particular special case converges extremely rapidly. For ln(1.1), n = 10. |
|
177
|
|
|
* |
|
178
|
1 |
|
* @param int $digits |
|
179
|
|
|
* @return string |
|
180
|
1 |
|
*/ |
|
181
|
|
|
public static function makeLn1p1(int $digits): string |
|
182
|
1 |
|
{ |
|
183
|
1 |
|
|
|
184
|
|
|
if (isset(self::$ln1p1) && self::$ln1p1->numberOfDecimalDigits() >= $digits) { |
|
185
|
1 |
|
return self::$ln1p1->truncateToScale($digits)->getValue(NumberBase::Ten); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
1 |
|
|
|
188
|
|
|
$two = Numbers::make(Numbers::IMMUTABLE, 2, $digits+3); |
|
189
|
|
|
$twentyOne = Numbers::make(Numbers::IMMUTABLE, 21, $digits+3); |
|
190
|
|
|
$fourFortyOne = Numbers::make(Numbers::IMMUTABLE, 441, $digits+3); |
|
191
|
|
|
$twoDivTwentyOne = $two->divide($twentyOne); |
|
192
|
|
|
$ln1p1 = self::_makeLnSpecial($digits, $fourFortyOne, $twoDivTwentyOne); |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
self::$ln1p1 = $ln1p1; |
|
195
|
|
|
|
|
196
|
|
|
return $ln1p1->getValue(NumberBase::Ten); |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
5 |
|
/** |
|
201
|
|
|
* @param int $digits |
|
202
|
|
|
* @param ImmutableDecimal $innerNum |
|
203
|
5 |
|
* @param ImmutableDecimal $outerNum |
|
204
|
3 |
|
* @return ImmutableDecimal |
|
205
|
|
|
*/ |
|
206
|
|
|
private static function _makeLnSpecial(int $digits, ImmutableDecimal $innerNum, ImmutableDecimal $outerNum): ImmutableDecimal |
|
207
|
5 |
|
{ |
|
208
|
5 |
|
$one = Numbers::makeOne($digits+3); |
|
209
|
5 |
|
$two = Numbers::make(Numbers::IMMUTABLE, 2, $digits+3); |
|
210
|
5 |
|
$sum = Numbers::makeZero($digits+3); |
|
211
|
5 |
|
$k = 0; |
|
212
|
5 |
|
|
|
213
|
5 |
|
do { |
|
214
|
|
|
|
|
215
|
|
|
$diff = $one->divide($one->add($two->multiply($k))->multiply($innerNum->pow($k)), $digits+3)->truncate($digits+2); |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
5 |
|
$sum = $sum->add($diff); |
|
218
|
|
|
|
|
219
|
5 |
|
$k++; |
|
220
|
|
|
|
|
221
|
5 |
|
} while (!$diff->isEqual(0)); |
|
222
|
|
|
|
|
223
|
5 |
|
$lnSpecial = $outerNum->multiply($sum); |
|
224
|
|
|
return $lnSpecial->truncateToScale($digits); |
|
225
|
5 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.