Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 17... Zeile 17...
17
{
17
{
18
    /**
18
    /**
19
     * The max number of digits the platform can natively add, subtract, multiply or divide without overflow.
19
     * The max number of digits the platform can natively add, subtract, multiply or divide without overflow.
20
     * For multiplication, this represents the max sum of the lengths of both operands.
20
     * For multiplication, this represents the max sum of the lengths of both operands.
21
     *
21
     *
22
     * For addition, it is assumed that an extra digit can hold a carry (1) without overflowing.
22
     * In addition, it is assumed that an extra digit can hold a carry (1) without overflowing.
23
     * Example: 32-bit: max number 1,999,999,999 (9 digits + carry)
23
     * Example: 32-bit: max number 1,999,999,999 (9 digits + carry)
24
     *          64-bit: max number 1,999,999,999,999,999,999 (18 digits + carry)
24
     *          64-bit: max number 1,999,999,999,999,999,999 (18 digits + carry)
25
     */
25
     */
26
    private int $maxDigits;
26
    private int $maxDigits;
Zeile 27... Zeile 27...
27
 
27
 
28
    /**
-
 
29
     * Class constructor.
-
 
30
     *
28
    /**
31
     * @codeCoverageIgnore
29
     * @codeCoverageIgnore
32
     */
30
     */
33
    public function __construct()
31
    public function __construct()
34
    {
32
    {
Zeile 44... Zeile 42...
44
            default:
42
            default:
45
                throw new \RuntimeException('The platform is not 32-bit or 64-bit as expected.');
43
                throw new \RuntimeException('The platform is not 32-bit or 64-bit as expected.');
46
        }
44
        }
47
    }
45
    }
Zeile 48... Zeile -...
48
 
-
 
49
    /**
-
 
50
     * {@inheritdoc}
-
 
51
     */
46
 
52
    public function add(string $a, string $b) : string
47
    public function add(string $a, string $b) : string
53
    {
48
    {
54
        /**
49
        /**
55
         * @psalm-var numeric-string $a
50
         * @psalm-var numeric-string $a
Zeile 78... Zeile 73...
78
        }
73
        }
Zeile 79... Zeile 74...
79
 
74
 
80
        return $result;
75
        return $result;
Zeile 81... Zeile -...
81
    }
-
 
82
 
-
 
83
    /**
-
 
84
     * {@inheritdoc}
76
    }
85
     */
77
 
86
    public function sub(string $a, string $b) : string
78
    public function sub(string $a, string $b) : string
87
    {
79
    {
Zeile 88... Zeile -...
88
        return $this->add($a, $this->neg($b));
-
 
89
    }
-
 
90
 
-
 
91
    /**
80
        return $this->add($a, $this->neg($b));
92
     * {@inheritdoc}
81
    }
93
     */
82
 
94
    public function mul(string $a, string $b) : string
83
    public function mul(string $a, string $b) : string
95
    {
84
    {
Zeile 132... Zeile 121...
132
        }
121
        }
Zeile 133... Zeile 122...
133
 
122
 
134
        return $result;
123
        return $result;
Zeile 135... Zeile -...
135
    }
-
 
136
 
-
 
137
    /**
-
 
138
     * {@inheritdoc}
124
    }
139
     */
125
 
140
    public function divQ(string $a, string $b) : string
126
    public function divQ(string $a, string $b) : string
141
    {
127
    {
Zeile 142... Zeile -...
142
        return $this->divQR($a, $b)[0];
-
 
143
    }
-
 
144
 
-
 
145
    /**
128
        return $this->divQR($a, $b)[0];
146
     * {@inheritdoc}
129
    }
147
     */
130
 
148
    public function divR(string $a, string $b): string
131
    public function divR(string $a, string $b): string
Zeile 149... Zeile -...
149
    {
-
 
150
        return $this->divQR($a, $b)[1];
-
 
151
    }
-
 
152
 
132
    {
153
    /**
133
        return $this->divQR($a, $b)[1];
154
     * {@inheritdoc}
134
    }
155
     */
135
 
156
    public function divQR(string $a, string $b) : array
136
    public function divQR(string $a, string $b) : array
Zeile 206... Zeile 186...
206
        }
186
        }
Zeile 207... Zeile 187...
207
 
187
 
208
        return [$q, $r];
188
        return [$q, $r];
Zeile 209... Zeile -...
209
    }
-
 
210
 
-
 
211
    /**
-
 
212
     * {@inheritdoc}
189
    }
213
     */
190
 
214
    public function pow(string $a, int $e) : string
191
    public function pow(string $a, int $e) : string
215
    {
192
    {
216
        if ($e === 0) {
193
        if ($e === 0) {
Zeile 236... Zeile 213...
236
        return $result;
213
        return $result;
237
    }
214
    }
Zeile 238... Zeile 215...
238
 
215
 
239
    /**
216
    /**
240
     * Algorithm from: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
-
 
241
     *
-
 
242
     * {@inheritdoc}
217
     * Algorithm from: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
243
     */
218
     */
244
    public function modPow(string $base, string $exp, string $mod) : string
219
    public function modPow(string $base, string $exp, string $mod) : string
245
    {
220
    {
246
        // special case: the algorithm below fails with 0 power 0 mod 1 (returns 1 instead of 0)
221
        // special case: the algorithm below fails with 0 power 0 mod 1 (returns 1 instead of 0)
Zeile 272... Zeile 247...
272
        return $res;
247
        return $res;
273
    }
248
    }
Zeile 274... Zeile 249...
274
 
249
 
275
    /**
250
    /**
276
     * Adapted from https://cp-algorithms.com/num_methods/roots_newton.html
-
 
277
     *
-
 
278
     * {@inheritDoc}
251
     * Adapted from https://cp-algorithms.com/num_methods/roots_newton.html
279
     */
252
     */
280
    public function sqrt(string $n) : string
253
    public function sqrt(string $n) : string
281
    {
254
    {
282
        if ($n === '0') {
255
        if ($n === '0') {
Zeile 302... Zeile 275...
302
        return $x;
275
        return $x;
303
    }
276
    }
Zeile 304... Zeile 277...
304
 
277
 
305
    /**
278
    /**
306
     * Performs the addition of two non-signed large integers.
-
 
307
     *
-
 
308
     * @param string $a The first operand.
-
 
309
     * @param string $b The second operand.
-
 
310
     *
-
 
311
     * @return string
279
     * Performs the addition of two non-signed large integers.
312
     */
280
     */
313
    private function doAdd(string $a, string $b) : string
281
    private function doAdd(string $a, string $b) : string
314
    {
282
    {
Zeile 359... Zeile 327...
359
        return $result;
327
        return $result;
360
    }
328
    }
Zeile 361... Zeile 329...
361
 
329
 
362
    /**
330
    /**
363
     * Performs the subtraction of two non-signed large integers.
-
 
364
     *
-
 
365
     * @param string $a The first operand.
-
 
366
     * @param string $b The second operand.
-
 
367
     *
-
 
368
     * @return string
331
     * Performs the subtraction of two non-signed large integers.
369
     */
332
     */
370
    private function doSub(string $a, string $b) : string
333
    private function doSub(string $a, string $b) : string
371
    {
334
    {
372
        if ($a === $b) {
335
        if ($a === $b) {
Zeile 441... Zeile 404...
441
        return $result;
404
        return $result;
442
    }
405
    }
Zeile 443... Zeile 406...
443
 
406
 
444
    /**
407
    /**
445
     * Performs the multiplication of two non-signed large integers.
-
 
446
     *
-
 
447
     * @param string $a The first operand.
-
 
448
     * @param string $b The second operand.
-
 
449
     *
-
 
450
     * @return string
408
     * Performs the multiplication of two non-signed large integers.
451
     */
409
     */
452
    private function doMul(string $a, string $b) : string
410
    private function doMul(string $a, string $b) : string
453
    {
411
    {
454
        $x = \strlen($a);
412
        $x = \strlen($a);
Zeile 518... Zeile 476...
518
    }
476
    }
Zeile 519... Zeile 477...
519
 
477
 
520
    /**
478
    /**
521
     * Performs the division of two non-signed large integers.
479
     * Performs the division of two non-signed large integers.
522
     *
-
 
523
     * @param string $a The first operand.
-
 
524
     * @param string $b The second operand.
-
 
525
     *
480
     *
526
     * @return string[] The quotient and remainder.
481
     * @return string[] The quotient and remainder.
527
     */
482
     */
528
    private function doDiv(string $a, string $b) : array
483
    private function doDiv(string $a, string $b) : array
529
    {
484
    {
Zeile 579... Zeile 534...
579
    }
534
    }
Zeile 580... Zeile 535...
580
 
535
 
581
    /**
536
    /**
582
     * Compares two non-signed large numbers.
537
     * Compares two non-signed large numbers.
583
     *
-
 
584
     * @param string $a The first operand.
-
 
585
     * @param string $b The second operand.
-
 
586
     *
538
     *
587
     * @return int [-1, 0, 1]
539
     * @return int [-1, 0, 1]
588
     */
540
     */
589
    private function doCmp(string $a, string $b) : int
541
    private function doCmp(string $a, string $b) : int
590
    {
542
    {
Zeile 603... Zeile 555...
603
    /**
555
    /**
604
     * Pads the left of one of the given numbers with zeros if necessary to make both numbers the same length.
556
     * Pads the left of one of the given numbers with zeros if necessary to make both numbers the same length.
605
     *
557
     *
606
     * The numbers must only consist of digits, without leading minus sign.
558
     * The numbers must only consist of digits, without leading minus sign.
607
     *
559
     *
608
     * @param string $a The first operand.
-
 
609
     * @param string $b The second operand.
-
 
610
     *
-
 
611
     * @return array{string, string, int}
560
     * @return array{string, string, int}
612
     */
561
     */
613
    private function pad(string $a, string $b) : array
562
    private function pad(string $a, string $b) : array
614
    {
563
    {
615
        $x = \strlen($a);
564
        $x = \strlen($a);