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 41... Zeile 41...
41
     * Sets the Calculator instance to use.
41
     * Sets the Calculator instance to use.
42
     *
42
     *
43
     * An instance is typically set only in unit tests: the autodetect is usually the best option.
43
     * An instance is typically set only in unit tests: the autodetect is usually the best option.
44
     *
44
     *
45
     * @param Calculator|null $calculator The calculator instance, or NULL to revert to autodetect.
45
     * @param Calculator|null $calculator The calculator instance, or NULL to revert to autodetect.
46
     *
-
 
47
     * @return void
-
 
48
     */
46
     */
49
    final public static function set(?Calculator $calculator) : void
47
    final public static function set(?Calculator $calculator) : void
50
    {
48
    {
51
        self::$instance = $calculator;
49
        self::$instance = $calculator;
52
    }
50
    }
Zeile 54... Zeile 52...
54
    /**
52
    /**
55
     * Returns the Calculator instance to use.
53
     * Returns the Calculator instance to use.
56
     *
54
     *
57
     * If none has been explicitly set, the fastest available implementation will be returned.
55
     * If none has been explicitly set, the fastest available implementation will be returned.
58
     *
56
     *
59
     * @return Calculator
-
 
60
     *
-
 
61
     * @psalm-pure
57
     * @psalm-pure
62
     * @psalm-suppress ImpureStaticProperty
58
     * @psalm-suppress ImpureStaticProperty
63
     */
59
     */
64
    final public static function get() : Calculator
60
    final public static function get() : Calculator
65
    {
61
    {
Zeile 73... Zeile 69...
73
 
69
 
74
    /**
70
    /**
75
     * Returns the fastest available Calculator implementation.
71
     * Returns the fastest available Calculator implementation.
76
     *
72
     *
77
     * @codeCoverageIgnore
-
 
78
     *
-
 
79
     * @return Calculator
73
     * @codeCoverageIgnore
80
     */
74
     */
81
    private static function detect() : Calculator
75
    private static function detect() : Calculator
82
    {
76
    {
83
        if (\extension_loaded('gmp')) {
77
        if (\extension_loaded('gmp')) {
Zeile 92... Zeile 86...
92
    }
86
    }
Zeile 93... Zeile 87...
93
 
87
 
94
    /**
88
    /**
95
     * Extracts the sign & digits of the operands.
89
     * Extracts the sign & digits of the operands.
96
     *
-
 
97
     * @param string $a The first operand.
-
 
98
     * @param string $b The second operand.
-
 
99
     *
90
     *
100
     * @return array{bool, bool, string, string} Whether $a and $b are negative, followed by their digits.
91
     * @return array{bool, bool, string, string} Whether $a and $b are negative, followed by their digits.
101
     */
92
     */
102
    final protected function init(string $a, string $b) : array
93
    final protected function init(string $a, string $b) : array
103
    {
94
    {
Zeile 110... Zeile 101...
110
        ];
101
        ];
111
    }
102
    }
Zeile 112... Zeile 103...
112
 
103
 
113
    /**
104
    /**
114
     * Returns the absolute value of a number.
-
 
115
     *
-
 
116
     * @param string $n The number.
-
 
117
     *
-
 
118
     * @return string The absolute value.
105
     * Returns the absolute value of a number.
119
     */
106
     */
120
    final public function abs(string $n) : string
107
    final public function abs(string $n) : string
121
    {
108
    {
122
        return ($n[0] === '-') ? \substr($n, 1) : $n;
109
        return ($n[0] === '-') ? \substr($n, 1) : $n;
Zeile 123... Zeile 110...
123
    }
110
    }
124
 
111
 
125
    /**
-
 
126
     * Negates a number.
-
 
127
     *
-
 
128
     * @param string $n The number.
-
 
129
     *
112
    /**
130
     * @return string The negated value.
113
     * Negates a number.
131
     */
114
     */
132
    final public function neg(string $n) : string
115
    final public function neg(string $n) : string
133
    {
116
    {
Zeile 143... Zeile 126...
143
    }
126
    }
Zeile 144... Zeile 127...
144
 
127
 
145
    /**
128
    /**
146
     * Compares two numbers.
129
     * Compares two numbers.
147
     *
-
 
148
     * @param string $a The first number.
-
 
149
     * @param string $b The second number.
-
 
150
     *
130
     *
151
     * @return int [-1, 0, 1] If the first number is less than, equal to, or greater than the second number.
131
     * @return int [-1, 0, 1] If the first number is less than, equal to, or greater than the second number.
152
     */
132
     */
153
    final public function cmp(string $a, string $b) : int
133
    final public function cmp(string $a, string $b) : int
154
    {
134
    {
Zeile 176... Zeile 156...
176
        return $aNeg ? -$result : $result;
156
        return $aNeg ? -$result : $result;
177
    }
157
    }
Zeile 178... Zeile 158...
178
 
158
 
179
    /**
159
    /**
180
     * Adds two numbers.
-
 
181
     *
-
 
182
     * @param string $a The augend.
-
 
183
     * @param string $b The addend.
-
 
184
     *
-
 
185
     * @return string The sum.
160
     * Adds two numbers.
186
     */
161
     */
Zeile 187... Zeile 162...
187
    abstract public function add(string $a, string $b) : string;
162
    abstract public function add(string $a, string $b) : string;
188
 
163
 
189
    /**
-
 
190
     * Subtracts two numbers.
-
 
191
     *
-
 
192
     * @param string $a The minuend.
-
 
193
     * @param string $b The subtrahend.
-
 
194
     *
164
    /**
195
     * @return string The difference.
165
     * Subtracts two numbers.
Zeile 196... Zeile 166...
196
     */
166
     */
197
    abstract public function sub(string $a, string $b) : string;
167
    abstract public function sub(string $a, string $b) : string;
198
 
-
 
199
    /**
-
 
200
     * Multiplies two numbers.
-
 
201
     *
-
 
202
     * @param string $a The multiplicand.
-
 
203
     * @param string $b The multiplier.
168
 
204
     *
169
    /**
Zeile 205... Zeile 170...
205
     * @return string The product.
170
     * Multiplies two numbers.
206
     */
171
     */
Zeile 245... Zeile 210...
245
     * @return string The power.
210
     * @return string The power.
246
     */
211
     */
247
    abstract public function pow(string $a, int $e) : string;
212
    abstract public function pow(string $a, int $e) : string;
Zeile 248... Zeile 213...
248
 
213
 
249
    /**
-
 
250
     * @param string $a
214
    /**
251
     * @param string $b The modulus; must not be zero.
-
 
252
     *
-
 
253
     * @return string
215
     * @param string $b The modulus; must not be zero.
254
     */
216
     */
255
    public function mod(string $a, string $b) : string
217
    public function mod(string $a, string $b) : string
256
    {
218
    {
257
        return $this->divR($this->add($this->divR($a, $b), $b), $b);
219
        return $this->divR($this->add($this->divR($a, $b), $b), $b);
Zeile 262... Zeile 224...
262
     *
224
     *
263
     * If $x has no multiplicative inverse mod m, this method must return null.
225
     * If $x has no multiplicative inverse mod m, this method must return null.
264
     *
226
     *
265
     * This method can be overridden by the concrete implementation if the underlying library has built-in support.
227
     * This method can be overridden by the concrete implementation if the underlying library has built-in support.
266
     *
228
     *
267
     * @param string $x
-
 
268
     * @param string $m The modulus; must not be negative or zero.
229
     * @param string $m The modulus; must not be negative or zero.
269
     *
-
 
270
     * @return string|null
-
 
271
     */
230
     */
272
    public function modInverse(string $x, string $m) : ?string
231
    public function modInverse(string $x, string $m) : ?string
273
    {
232
    {
274
        if ($m === '1') {
233
        if ($m === '1') {
275
            return '0';
234
            return '0';
Zeile 294... Zeile 253...
294
     * Raises a number into power with modulo.
253
     * Raises a number into power with modulo.
295
     *
254
     *
296
     * @param string $base The base number; must be positive or zero.
255
     * @param string $base The base number; must be positive or zero.
297
     * @param string $exp  The exponent; must be positive or zero.
256
     * @param string $exp  The exponent; must be positive or zero.
298
     * @param string $mod  The modulus; must be strictly positive.
257
     * @param string $mod  The modulus; must be strictly positive.
299
     *
-
 
300
     * @return string The power.
-
 
301
     */
258
     */
302
    abstract public function modPow(string $base, string $exp, string $mod) : string;
259
    abstract public function modPow(string $base, string $exp, string $mod) : string;
Zeile 303... Zeile 260...
303
 
260
 
304
    /**
261
    /**
305
     * Returns the greatest common divisor of the two numbers.
262
     * Returns the greatest common divisor of the two numbers.
306
     *
263
     *
307
     * This method can be overridden by the concrete implementation if the underlying library
264
     * This method can be overridden by the concrete implementation if the underlying library
308
     * has built-in support for GCD calculations.
265
     * has built-in support for GCD calculations.
309
     *
-
 
310
     * @param string $a The first number.
-
 
311
     * @param string $b The second number.
-
 
312
     *
266
     *
313
     * @return string The GCD, always positive, or zero if both arguments are zero.
267
     * @return string The GCD, always positive, or zero if both arguments are zero.
314
     */
268
     */
315
    public function gcd(string $a, string $b) : string
269
    public function gcd(string $a, string $b) : string
316
    {
270
    {
Zeile 345... Zeile 299...
345
    /**
299
    /**
346
     * Returns the square root of the given number, rounded down.
300
     * Returns the square root of the given number, rounded down.
347
     *
301
     *
348
     * The result is the largest x such that x² ≤ n.
302
     * The result is the largest x such that x² ≤ n.
349
     * The input MUST NOT be negative.
303
     * The input MUST NOT be negative.
350
     *
-
 
351
     * @param string $n The number.
-
 
352
     *
-
 
353
     * @return string The square root.
-
 
354
     */
304
     */
355
    abstract public function sqrt(string $n) : string;
305
    abstract public function sqrt(string $n) : string;
Zeile 356... Zeile 306...
356
 
306
 
357
    /**
307
    /**
Zeile 480... Zeile 430...
480
     *
430
     *
481
     * @param string $a            The dividend.
431
     * @param string $a            The dividend.
482
     * @param string $b            The divisor, must not be zero.
432
     * @param string $b            The divisor, must not be zero.
483
     * @param int    $roundingMode The rounding mode.
433
     * @param int    $roundingMode The rounding mode.
484
     *
434
     *
485
     * @return string
-
 
486
     *
-
 
487
     * @throws \InvalidArgumentException  If the rounding mode is invalid.
435
     * @throws \InvalidArgumentException  If the rounding mode is invalid.
488
     * @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided but rounding is necessary.
436
     * @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided but rounding is necessary.
489
     *
437
     *
490
     * @psalm-suppress ImpureFunctionCall
438
     * @psalm-suppress ImpureFunctionCall
491
     */
439
     */
Zeile 563... Zeile 511...
563
    /**
511
    /**
564
     * Calculates bitwise AND of two numbers.
512
     * Calculates bitwise AND of two numbers.
565
     *
513
     *
566
     * This method can be overridden by the concrete implementation if the underlying library
514
     * This method can be overridden by the concrete implementation if the underlying library
567
     * has built-in support for bitwise operations.
515
     * has built-in support for bitwise operations.
568
     *
-
 
569
     * @param string $a
-
 
570
     * @param string $b
-
 
571
     *
-
 
572
     * @return string
-
 
573
     */
516
     */
574
    public function and(string $a, string $b) : string
517
    public function and(string $a, string $b) : string
575
    {
518
    {
576
        return $this->bitwise('and', $a, $b);
519
        return $this->bitwise('and', $a, $b);
577
    }
520
    }
Zeile 579... Zeile 522...
579
    /**
522
    /**
580
     * Calculates bitwise OR of two numbers.
523
     * Calculates bitwise OR of two numbers.
581
     *
524
     *
582
     * This method can be overridden by the concrete implementation if the underlying library
525
     * This method can be overridden by the concrete implementation if the underlying library
583
     * has built-in support for bitwise operations.
526
     * has built-in support for bitwise operations.
584
     *
-
 
585
     * @param string $a
-
 
586
     * @param string $b
-
 
587
     *
-
 
588
     * @return string
-
 
589
     */
527
     */
590
    public function or(string $a, string $b) : string
528
    public function or(string $a, string $b) : string
591
    {
529
    {
592
        return $this->bitwise('or', $a, $b);
530
        return $this->bitwise('or', $a, $b);
593
    }
531
    }
Zeile 595... Zeile 533...
595
    /**
533
    /**
596
     * Calculates bitwise XOR of two numbers.
534
     * Calculates bitwise XOR of two numbers.
597
     *
535
     *
598
     * This method can be overridden by the concrete implementation if the underlying library
536
     * This method can be overridden by the concrete implementation if the underlying library
599
     * has built-in support for bitwise operations.
537
     * has built-in support for bitwise operations.
600
     *
-
 
601
     * @param string $a
-
 
602
     * @param string $b
-
 
603
     *
-
 
604
     * @return string
-
 
605
     */
538
     */
606
    public function xor(string $a, string $b) : string
539
    public function xor(string $a, string $b) : string
607
    {
540
    {
608
        return $this->bitwise('xor', $a, $b);
541
        return $this->bitwise('xor', $a, $b);
609
    }
542
    }
Zeile 612... Zeile 545...
612
     * Performs a bitwise operation on a decimal number.
545
     * Performs a bitwise operation on a decimal number.
613
     *
546
     *
614
     * @param 'and'|'or'|'xor' $operator The operator to use.
547
     * @param 'and'|'or'|'xor' $operator The operator to use.
615
     * @param string           $a        The left operand.
548
     * @param string           $a        The left operand.
616
     * @param string           $b        The right operand.
549
     * @param string           $b        The right operand.
617
     *
-
 
618
     * @return string
-
 
619
     */
550
     */
620
    private function bitwise(string $operator, string $a, string $b) : string
551
    private function bitwise(string $operator, string $a, string $b) : string
621
    {
552
    {
622
        [$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
553
        [$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
Zeile 671... Zeile 602...
671
        return $negative ? $this->neg($result) : $result;
602
        return $negative ? $this->neg($result) : $result;
672
    }
603
    }
Zeile 673... Zeile 604...
673
 
604
 
674
    /**
605
    /**
675
     * @param string $number A positive, binary number.
-
 
676
     *
-
 
677
     * @return string
606
     * @param string $number A positive, binary number.
678
     */
607
     */
679
    private function twosComplement(string $number) : string
608
    private function twosComplement(string $number) : string
680
    {
609
    {
Zeile 702... Zeile 631...
702
 
631
 
703
    /**
632
    /**
704
     * Converts a decimal number to a binary string.
633
     * Converts a decimal number to a binary string.
705
     *
634
     *
706
     * @param string $number The number to convert, positive or zero, only digits.
-
 
707
     *
-
 
708
     * @return string
635
     * @param string $number The number to convert, positive or zero, only digits.
709
     */
636
     */
710
    private function toBinary(string $number) : string
637
    private function toBinary(string $number) : string
711
    {
638
    {
Zeile 721... Zeile 648...
721
 
648
 
722
    /**
649
    /**
723
     * Returns the positive decimal representation of a binary number.
650
     * Returns the positive decimal representation of a binary number.
724
     *
651
     *
725
     * @param string $bytes The bytes representing the number.
-
 
726
     *
-
 
727
     * @return string
652
     * @param string $bytes The bytes representing the number.
728
     */
653
     */
729
    private function toDecimal(string $bytes) : string
654
    private function toDecimal(string $bytes) : string
730
    {
655
    {
731
        $result = '0';
656
        $result = '0';