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 40... Zeile 40...
40
    }
40
    }
Zeile 41... Zeile 41...
41
 
41
 
42
    /**
42
    /**
43
     * Creates a BigInteger of the given value.
43
     * Creates a BigInteger of the given value.
44
     *
-
 
45
     * @param BigNumber|int|float|string $value
-
 
46
     *
-
 
47
     * @return BigInteger
-
 
48
     *
44
     *
49
     * @throws MathException If the value cannot be converted to a BigInteger.
45
     * @throws MathException If the value cannot be converted to a BigInteger.
50
     *
46
     *
51
     * @psalm-pure
47
     * @psalm-pure
52
     */
48
     */
53
    public static function of($value) : BigNumber
49
    public static function of(BigNumber|int|float|string $value) : BigInteger
54
    {
50
    {
55
        return parent::of($value)->toBigInteger();
51
        return parent::of($value)->toBigInteger();
Zeile 56... Zeile 52...
56
    }
52
    }
Zeile 67... Zeile 63...
67
     * For bases greater than 36, and/or custom alphabets, use the fromArbitraryBase() method.
63
     * For bases greater than 36, and/or custom alphabets, use the fromArbitraryBase() method.
68
     *
64
     *
69
     * @param string $number The number to convert, in the given base.
65
     * @param string $number The number to convert, in the given base.
70
     * @param int    $base   The base of the number, between 2 and 36.
66
     * @param int    $base   The base of the number, between 2 and 36.
71
     *
67
     *
72
     * @return BigInteger
-
 
73
     *
-
 
74
     * @throws NumberFormatException     If the number is empty, or contains invalid chars for the given base.
68
     * @throws NumberFormatException     If the number is empty, or contains invalid chars for the given base.
75
     * @throws \InvalidArgumentException If the base is out of range.
69
     * @throws \InvalidArgumentException If the base is out of range.
76
     *
70
     *
77
     * @psalm-pure
71
     * @psalm-pure
78
     */
72
     */
Zeile 134... Zeile 128...
134
     * Because this method accepts an alphabet with any character, including dash, it does not handle negative numbers.
128
     * Because this method accepts an alphabet with any character, including dash, it does not handle negative numbers.
135
     *
129
     *
136
     * @param string $number   The number to parse.
130
     * @param string $number   The number to parse.
137
     * @param string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
131
     * @param string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
138
     *
132
     *
139
     * @return BigInteger
-
 
140
     *
-
 
141
     * @throws NumberFormatException     If the given number is empty or contains invalid chars for the given alphabet.
133
     * @throws NumberFormatException     If the given number is empty or contains invalid chars for the given alphabet.
142
     * @throws \InvalidArgumentException If the alphabet does not contain at least 2 chars.
134
     * @throws \InvalidArgumentException If the alphabet does not contain at least 2 chars.
143
     *
135
     *
144
     * @psalm-pure
136
     * @psalm-pure
145
     */
137
     */
Zeile 179... Zeile 171...
179
     *
171
     *
180
     * @param string $value  The byte string.
172
     * @param string $value  The byte string.
181
     * @param bool   $signed Whether to interpret as a signed number in two's-complement representation with a leading
173
     * @param bool   $signed Whether to interpret as a signed number in two's-complement representation with a leading
182
     *                       sign bit.
174
     *                       sign bit.
183
     *
175
     *
184
     * @return BigInteger
-
 
185
     *
-
 
186
     * @throws NumberFormatException If the string is empty.
176
     * @throws NumberFormatException If the string is empty.
187
     */
177
     */
188
    public static function fromBytes(string $value, bool $signed = true) : BigInteger
178
    public static function fromBytes(string $value, bool $signed = true) : BigInteger
189
    {
179
    {
190
        if ($value === '') {
180
        if ($value === '') {
Zeile 213... Zeile 203...
213
    /**
203
    /**
214
     * Generates a pseudo-random number in the range 0 to 2^numBits - 1.
204
     * Generates a pseudo-random number in the range 0 to 2^numBits - 1.
215
     *
205
     *
216
     * Using the default random bytes generator, this method is suitable for cryptographic use.
206
     * Using the default random bytes generator, this method is suitable for cryptographic use.
217
     *
207
     *
218
     * @psalm-param callable(int): string $randomBytesGenerator
208
     * @psalm-param (callable(int): string)|null $randomBytesGenerator
219
     *
209
     *
220
     * @param int           $numBits              The number of bits.
210
     * @param int           $numBits              The number of bits.
221
     * @param callable|null $randomBytesGenerator A function that accepts a number of bytes as an integer, and returns a
211
     * @param callable|null $randomBytesGenerator A function that accepts a number of bytes as an integer, and returns a
222
     *                                            string of random bytes of the given length. Defaults to the
212
     *                                            string of random bytes of the given length. Defaults to the
223
     *                                            `random_bytes()` function.
213
     *                                            `random_bytes()` function.
224
     *
214
     *
225
     * @return BigInteger
-
 
226
     *
-
 
227
     * @throws \InvalidArgumentException If $numBits is negative.
215
     * @throws \InvalidArgumentException If $numBits is negative.
228
     */
216
     */
229
    public static function randomBits(int $numBits, ?callable $randomBytesGenerator = null) : BigInteger
217
    public static function randomBits(int $numBits, ?callable $randomBytesGenerator = null) : BigInteger
230
    {
218
    {
231
        if ($numBits < 0) {
219
        if ($numBits < 0) {
Zeile 262... Zeile 250...
262
     * @param BigNumber|int|float|string $max                  The upper bound. Must be convertible to a BigInteger.
250
     * @param BigNumber|int|float|string $max                  The upper bound. Must be convertible to a BigInteger.
263
     * @param callable|null              $randomBytesGenerator A function that accepts a number of bytes as an integer,
251
     * @param callable|null              $randomBytesGenerator A function that accepts a number of bytes as an integer,
264
     *                                                         and returns a string of random bytes of the given length.
252
     *                                                         and returns a string of random bytes of the given length.
265
     *                                                         Defaults to the `random_bytes()` function.
253
     *                                                         Defaults to the `random_bytes()` function.
266
     *
254
     *
267
     * @return BigInteger
-
 
268
     *
-
 
269
     * @throws MathException If one of the parameters cannot be converted to a BigInteger,
255
     * @throws MathException If one of the parameters cannot be converted to a BigInteger,
270
     *                       or `$min` is greater than `$max`.
256
     *                       or `$min` is greater than `$max`.
271
     */
257
     */
-
 
258
    public static function randomRange(
-
 
259
        BigNumber|int|float|string $min,
-
 
260
        BigNumber|int|float|string $max,
272
    public static function randomRange($min, $max, ?callable $randomBytesGenerator = null) : BigInteger
261
        ?callable $randomBytesGenerator = null
273
    {
262
    ) : BigInteger {
274
        $min = BigInteger::of($min);
263
        $min = BigInteger::of($min);
275
        $max = BigInteger::of($max);
264
        $max = BigInteger::of($max);
Zeile 276... Zeile 265...
276
 
265
 
277
        if ($min->isGreaterThan($max)) {
266
        if ($min->isGreaterThan($max)) {
Zeile 294... Zeile 283...
294
    }
283
    }
Zeile 295... Zeile 284...
295
 
284
 
296
    /**
285
    /**
297
     * Returns a BigInteger representing zero.
286
     * Returns a BigInteger representing zero.
298
     *
-
 
299
     * @return BigInteger
-
 
300
     *
287
     *
301
     * @psalm-pure
288
     * @psalm-pure
302
     */
289
     */
303
    public static function zero() : BigInteger
290
    public static function zero() : BigInteger
304
    {
291
    {
Zeile 316... Zeile 303...
316
    }
303
    }
Zeile 317... Zeile 304...
317
 
304
 
318
    /**
305
    /**
319
     * Returns a BigInteger representing one.
306
     * Returns a BigInteger representing one.
320
     *
-
 
321
     * @return BigInteger
-
 
322
     *
307
     *
323
     * @psalm-pure
308
     * @psalm-pure
324
     */
309
     */
325
    public static function one() : BigInteger
310
    public static function one() : BigInteger
326
    {
311
    {
Zeile 338... Zeile 323...
338
    }
323
    }
Zeile 339... Zeile 324...
339
 
324
 
340
    /**
325
    /**
341
     * Returns a BigInteger representing ten.
326
     * Returns a BigInteger representing ten.
342
     *
-
 
343
     * @return BigInteger
-
 
344
     *
327
     *
345
     * @psalm-pure
328
     * @psalm-pure
346
     */
329
     */
347
    public static function ten() : BigInteger
330
    public static function ten() : BigInteger
348
    {
331
    {
Zeile 377... Zeile 360...
377
    /**
360
    /**
378
     * Returns the sum of this number and the given one.
361
     * Returns the sum of this number and the given one.
379
     *
362
     *
380
     * @param BigNumber|int|float|string $that The number to add. Must be convertible to a BigInteger.
363
     * @param BigNumber|int|float|string $that The number to add. Must be convertible to a BigInteger.
381
     *
364
     *
382
     * @return BigInteger The result.
-
 
383
     *
-
 
384
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
365
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
385
     */
366
     */
386
    public function plus($that) : BigInteger
367
    public function plus(BigNumber|int|float|string $that) : BigInteger
387
    {
368
    {
388
        $that = BigInteger::of($that);
369
        $that = BigInteger::of($that);
Zeile 389... Zeile 370...
389
 
370
 
390
        if ($that->value === '0') {
371
        if ($that->value === '0') {
Zeile 403... Zeile 384...
403
    /**
384
    /**
404
     * Returns the difference of this number and the given one.
385
     * Returns the difference of this number and the given one.
405
     *
386
     *
406
     * @param BigNumber|int|float|string $that The number to subtract. Must be convertible to a BigInteger.
387
     * @param BigNumber|int|float|string $that The number to subtract. Must be convertible to a BigInteger.
407
     *
388
     *
408
     * @return BigInteger The result.
-
 
409
     *
-
 
410
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
389
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
411
     */
390
     */
412
    public function minus($that) : BigInteger
391
    public function minus(BigNumber|int|float|string $that) : BigInteger
413
    {
392
    {
414
        $that = BigInteger::of($that);
393
        $that = BigInteger::of($that);
Zeile 415... Zeile 394...
415
 
394
 
416
        if ($that->value === '0') {
395
        if ($that->value === '0') {
Zeile 425... Zeile 404...
425
    /**
404
    /**
426
     * Returns the product of this number and the given one.
405
     * Returns the product of this number and the given one.
427
     *
406
     *
428
     * @param BigNumber|int|float|string $that The multiplier. Must be convertible to a BigInteger.
407
     * @param BigNumber|int|float|string $that The multiplier. Must be convertible to a BigInteger.
429
     *
408
     *
430
     * @return BigInteger The result.
-
 
431
     *
-
 
432
     * @throws MathException If the multiplier is not a valid number, or is not convertible to a BigInteger.
409
     * @throws MathException If the multiplier is not a valid number, or is not convertible to a BigInteger.
433
     */
410
     */
434
    public function multipliedBy($that) : BigInteger
411
    public function multipliedBy(BigNumber|int|float|string $that) : BigInteger
435
    {
412
    {
436
        $that = BigInteger::of($that);
413
        $that = BigInteger::of($that);
Zeile 437... Zeile 414...
437
 
414
 
438
        if ($that->value === '1') {
415
        if ($that->value === '1') {
Zeile 452... Zeile 429...
452
     * Returns the result of the division of this number by the given one.
429
     * Returns the result of the division of this number by the given one.
453
     *
430
     *
454
     * @param BigNumber|int|float|string $that         The divisor. Must be convertible to a BigInteger.
431
     * @param BigNumber|int|float|string $that         The divisor. Must be convertible to a BigInteger.
455
     * @param int                        $roundingMode An optional rounding mode.
432
     * @param int                        $roundingMode An optional rounding mode.
456
     *
433
     *
457
     * @return BigInteger The result.
-
 
458
     *
-
 
459
     * @throws MathException If the divisor is not a valid number, is not convertible to a BigInteger, is zero,
434
     * @throws MathException If the divisor is not a valid number, is not convertible to a BigInteger, is zero,
460
     *                       or RoundingMode::UNNECESSARY is used and the remainder is not zero.
435
     *                       or RoundingMode::UNNECESSARY is used and the remainder is not zero.
461
     */
436
     */
462
    public function dividedBy($that, int $roundingMode = RoundingMode::UNNECESSARY) : BigInteger
437
    public function dividedBy(BigNumber|int|float|string $that, int $roundingMode = RoundingMode::UNNECESSARY) : BigInteger
463
    {
438
    {
464
        $that = BigInteger::of($that);
439
        $that = BigInteger::of($that);
Zeile 465... Zeile 440...
465
 
440
 
466
        if ($that->value === '1') {
441
        if ($that->value === '1') {
Zeile 477... Zeile 452...
477
    }
452
    }
Zeile 478... Zeile 453...
478
 
453
 
479
    /**
454
    /**
480
     * Returns this number exponentiated to the given value.
455
     * Returns this number exponentiated to the given value.
481
     *
-
 
482
     * @param int $exponent The exponent.
-
 
483
     *
-
 
484
     * @return BigInteger The result.
-
 
485
     *
456
     *
486
     * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
457
     * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
487
     */
458
     */
488
    public function power(int $exponent) : BigInteger
459
    public function power(int $exponent) : BigInteger
489
    {
460
    {
Zeile 509... Zeile 480...
509
    /**
480
    /**
510
     * Returns the quotient of the division of this number by the given one.
481
     * Returns the quotient of the division of this number by the given one.
511
     *
482
     *
512
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
483
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
513
     *
484
     *
514
     * @return BigInteger
-
 
515
     *
-
 
516
     * @throws DivisionByZeroException If the divisor is zero.
485
     * @throws DivisionByZeroException If the divisor is zero.
517
     */
486
     */
518
    public function quotient($that) : BigInteger
487
    public function quotient(BigNumber|int|float|string $that) : BigInteger
519
    {
488
    {
520
        $that = BigInteger::of($that);
489
        $that = BigInteger::of($that);
Zeile 521... Zeile 490...
521
 
490
 
522
        if ($that->value === '1') {
491
        if ($that->value === '1') {
Zeile 537... Zeile 506...
537
     *
506
     *
538
     * The remainder, when non-zero, has the same sign as the dividend.
507
     * The remainder, when non-zero, has the same sign as the dividend.
539
     *
508
     *
540
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
509
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
541
     *
510
     *
542
     * @return BigInteger
-
 
543
     *
-
 
544
     * @throws DivisionByZeroException If the divisor is zero.
511
     * @throws DivisionByZeroException If the divisor is zero.
545
     */
512
     */
546
    public function remainder($that) : BigInteger
513
    public function remainder(BigNumber|int|float|string $that) : BigInteger
547
    {
514
    {
548
        $that = BigInteger::of($that);
515
        $that = BigInteger::of($that);
Zeile 549... Zeile 516...
549
 
516
 
550
        if ($that->value === '1') {
517
        if ($that->value === '1') {
Zeile 567... Zeile 534...
567
     *
534
     *
568
     * @return BigInteger[] An array containing the quotient and the remainder.
535
     * @return BigInteger[] An array containing the quotient and the remainder.
569
     *
536
     *
570
     * @throws DivisionByZeroException If the divisor is zero.
537
     * @throws DivisionByZeroException If the divisor is zero.
571
     */
538
     */
572
    public function quotientAndRemainder($that) : array
539
    public function quotientAndRemainder(BigNumber|int|float|string $that) : array
573
    {
540
    {
574
        $that = BigInteger::of($that);
541
        $that = BigInteger::of($that);
Zeile 575... Zeile 542...
575
 
542
 
576
        if ($that->value === '0') {
543
        if ($that->value === '0') {
Zeile 593... Zeile 560...
593
     *
560
     *
594
     * The result of the modulo operation, when non-zero, has the same sign as the divisor.
561
     * The result of the modulo operation, when non-zero, has the same sign as the divisor.
595
     *
562
     *
596
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
563
     * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigInteger.
597
     *
564
     *
598
     * @return BigInteger
-
 
599
     *
-
 
600
     * @throws DivisionByZeroException If the divisor is zero.
565
     * @throws DivisionByZeroException If the divisor is zero.
601
     */
566
     */
602
    public function mod($that) : BigInteger
567
    public function mod(BigNumber|int|float|string $that) : BigInteger
603
    {
568
    {
604
        $that = BigInteger::of($that);
569
        $that = BigInteger::of($that);
Zeile 605... Zeile 570...
605
 
570
 
606
        if ($that->value === '0') {
571
        if ($that->value === '0') {
Zeile 613... Zeile 578...
613
    }
578
    }
Zeile 614... Zeile 579...
614
 
579
 
615
    /**
580
    /**
616
     * Returns the modular multiplicative inverse of this BigInteger modulo $m.
581
     * Returns the modular multiplicative inverse of this BigInteger modulo $m.
617
     *
-
 
618
     * @param BigInteger $m
-
 
619
     *
-
 
620
     * @return BigInteger
-
 
621
     *
582
     *
622
     * @throws DivisionByZeroException If $m is zero.
583
     * @throws DivisionByZeroException If $m is zero.
623
     * @throws NegativeNumberException If $m is negative.
584
     * @throws NegativeNumberException If $m is negative.
624
     * @throws MathException           If this BigInteger has no multiplicative inverse mod m (that is, this BigInteger
585
     * @throws MathException           If this BigInteger has no multiplicative inverse mod m (that is, this BigInteger
625
     *                                 is not relatively prime to m).
586
     *                                 is not relatively prime to m).
Zeile 653... Zeile 614...
653
     * This operation only works on positive numbers.
614
     * This operation only works on positive numbers.
654
     *
615
     *
655
     * @param BigNumber|int|float|string $exp The exponent. Must be positive or zero.
616
     * @param BigNumber|int|float|string $exp The exponent. Must be positive or zero.
656
     * @param BigNumber|int|float|string $mod The modulus. Must be strictly positive.
617
     * @param BigNumber|int|float|string $mod The modulus. Must be strictly positive.
657
     *
618
     *
658
     * @return BigInteger
-
 
659
     *
-
 
660
     * @throws NegativeNumberException If any of the operands is negative.
619
     * @throws NegativeNumberException If any of the operands is negative.
661
     * @throws DivisionByZeroException If the modulus is zero.
620
     * @throws DivisionByZeroException If the modulus is zero.
662
     */
621
     */
663
    public function modPow($exp, $mod) : BigInteger
622
    public function modPow(BigNumber|int|float|string $exp, BigNumber|int|float|string $mod) : BigInteger
664
    {
623
    {
665
        $exp = BigInteger::of($exp);
624
        $exp = BigInteger::of($exp);
666
        $mod = BigInteger::of($mod);
625
        $mod = BigInteger::of($mod);
Zeile 667... Zeile 626...
667
 
626
 
Zeile 682... Zeile 641...
682
     * Returns the greatest common divisor of this number and the given one.
641
     * Returns the greatest common divisor of this number and the given one.
683
     *
642
     *
684
     * The GCD is always positive, unless both operands are zero, in which case it is zero.
643
     * The GCD is always positive, unless both operands are zero, in which case it is zero.
685
     *
644
     *
686
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
645
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
687
     *
-
 
688
     * @return BigInteger
-
 
689
     */
646
     */
690
    public function gcd($that) : BigInteger
647
    public function gcd(BigNumber|int|float|string $that) : BigInteger
691
    {
648
    {
692
        $that = BigInteger::of($that);
649
        $that = BigInteger::of($that);
Zeile 693... Zeile 650...
693
 
650
 
694
        if ($that->value === '0' && $this->value[0] !== '-') {
651
        if ($that->value === '0' && $this->value[0] !== '-') {
Zeile 707... Zeile 664...
707
    /**
664
    /**
708
     * Returns the integer square root number of this number, rounded down.
665
     * Returns the integer square root number of this number, rounded down.
709
     *
666
     *
710
     * The result is the largest x such that x² ≤ n.
667
     * The result is the largest x such that x² ≤ n.
711
     *
668
     *
712
     * @return BigInteger
-
 
713
     *
-
 
714
     * @throws NegativeNumberException If this number is negative.
669
     * @throws NegativeNumberException If this number is negative.
715
     */
670
     */
716
    public function sqrt() : BigInteger
671
    public function sqrt() : BigInteger
717
    {
672
    {
718
        if ($this->value[0] === '-') {
673
        if ($this->value[0] === '-') {
Zeile 724... Zeile 679...
724
        return new BigInteger($value);
679
        return new BigInteger($value);
725
    }
680
    }
Zeile 726... Zeile 681...
726
 
681
 
727
    /**
682
    /**
728
     * Returns the absolute value of this number.
-
 
729
     *
-
 
730
     * @return BigInteger
683
     * Returns the absolute value of this number.
731
     */
684
     */
732
    public function abs() : BigInteger
685
    public function abs() : BigInteger
733
    {
686
    {
734
        return $this->isNegative() ? $this->negated() : $this;
687
        return $this->isNegative() ? $this->negated() : $this;
Zeile 735... Zeile 688...
735
    }
688
    }
736
 
689
 
737
    /**
-
 
738
     * Returns the inverse of this number.
-
 
739
     *
690
    /**
740
     * @return BigInteger
691
     * Returns the inverse of this number.
741
     */
692
     */
742
    public function negated() : BigInteger
693
    public function negated() : BigInteger
743
    {
694
    {
Zeile 748... Zeile 699...
748
     * Returns the integer bitwise-and combined with another integer.
699
     * Returns the integer bitwise-and combined with another integer.
749
     *
700
     *
750
     * This method returns a negative BigInteger if and only if both operands are negative.
701
     * This method returns a negative BigInteger if and only if both operands are negative.
751
     *
702
     *
752
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
703
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
753
     *
-
 
754
     * @return BigInteger
-
 
755
     */
704
     */
756
    public function and($that) : BigInteger
705
    public function and(BigNumber|int|float|string $that) : BigInteger
757
    {
706
    {
758
        $that = BigInteger::of($that);
707
        $that = BigInteger::of($that);
Zeile 759... Zeile 708...
759
 
708
 
760
        return new BigInteger(Calculator::get()->and($this->value, $that->value));
709
        return new BigInteger(Calculator::get()->and($this->value, $that->value));
Zeile 764... Zeile 713...
764
     * Returns the integer bitwise-or combined with another integer.
713
     * Returns the integer bitwise-or combined with another integer.
765
     *
714
     *
766
     * This method returns a negative BigInteger if and only if either of the operands is negative.
715
     * This method returns a negative BigInteger if and only if either of the operands is negative.
767
     *
716
     *
768
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
717
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
769
     *
-
 
770
     * @return BigInteger
-
 
771
     */
718
     */
772
    public function or($that) : BigInteger
719
    public function or(BigNumber|int|float|string $that) : BigInteger
773
    {
720
    {
774
        $that = BigInteger::of($that);
721
        $that = BigInteger::of($that);
Zeile 775... Zeile 722...
775
 
722
 
776
        return new BigInteger(Calculator::get()->or($this->value, $that->value));
723
        return new BigInteger(Calculator::get()->or($this->value, $that->value));
Zeile 780... Zeile 727...
780
     * Returns the integer bitwise-xor combined with another integer.
727
     * Returns the integer bitwise-xor combined with another integer.
781
     *
728
     *
782
     * This method returns a negative BigInteger if and only if exactly one of the operands is negative.
729
     * This method returns a negative BigInteger if and only if exactly one of the operands is negative.
783
     *
730
     *
784
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
731
     * @param BigNumber|int|float|string $that The operand. Must be convertible to an integer number.
785
     *
-
 
786
     * @return BigInteger
-
 
787
     */
732
     */
788
    public function xor($that) : BigInteger
733
    public function xor(BigNumber|int|float|string $that) : BigInteger
789
    {
734
    {
790
        $that = BigInteger::of($that);
735
        $that = BigInteger::of($that);
Zeile 791... Zeile 736...
791
 
736
 
792
        return new BigInteger(Calculator::get()->xor($this->value, $that->value));
737
        return new BigInteger(Calculator::get()->xor($this->value, $that->value));
Zeile 793... Zeile 738...
793
    }
738
    }
794
 
739
 
795
    /**
-
 
796
     * Returns the bitwise-not of this BigInteger.
-
 
797
     *
740
    /**
798
     * @return BigInteger
741
     * Returns the bitwise-not of this BigInteger.
799
     */
742
     */
800
    public function not() : BigInteger
743
    public function not() : BigInteger
801
    {
744
    {
Zeile 802... Zeile 745...
802
        return $this->negated()->minus(1);
745
        return $this->negated()->minus(1);
803
    }
746
    }
804
 
-
 
805
    /**
-
 
806
     * Returns the integer left shifted by a given number of bits.
-
 
807
     *
-
 
808
     * @param int $distance The distance to shift.
747
 
809
     *
748
    /**
810
     * @return BigInteger
749
     * Returns the integer left shifted by a given number of bits.
811
     */
750
     */
812
    public function shiftedLeft(int $distance) : BigInteger
751
    public function shiftedLeft(int $distance) : BigInteger
Zeile 822... Zeile 761...
822
        return $this->multipliedBy(BigInteger::of(2)->power($distance));
761
        return $this->multipliedBy(BigInteger::of(2)->power($distance));
823
    }
762
    }
Zeile 824... Zeile 763...
824
 
763
 
825
    /**
764
    /**
826
     * Returns the integer right shifted by a given number of bits.
-
 
827
     *
-
 
828
     * @param int $distance The distance to shift.
-
 
829
     *
-
 
830
     * @return BigInteger
765
     * Returns the integer right shifted by a given number of bits.
831
     */
766
     */
832
    public function shiftedRight(int $distance) : BigInteger
767
    public function shiftedRight(int $distance) : BigInteger
833
    {
768
    {
834
        if ($distance === 0) {
769
        if ($distance === 0) {
Zeile 851... Zeile 786...
851
    /**
786
    /**
852
     * Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
787
     * Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
853
     *
788
     *
854
     * For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation.
789
     * For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation.
855
     * Computes (ceil(log2(this < 0 ? -this : this+1))).
790
     * Computes (ceil(log2(this < 0 ? -this : this+1))).
856
     *
-
 
857
     * @return int
-
 
858
     */
791
     */
859
    public function getBitLength() : int
792
    public function getBitLength() : int
860
    {
793
    {
861
        if ($this->value === '0') {
794
        if ($this->value === '0') {
862
            return 0;
795
            return 0;
Zeile 871... Zeile 804...
871
 
804
 
872
    /**
805
    /**
873
     * Returns the index of the rightmost (lowest-order) one bit in this BigInteger.
806
     * Returns the index of the rightmost (lowest-order) one bit in this BigInteger.
874
     *
807
     *
875
     * Returns -1 if this BigInteger contains no one bits.
-
 
876
     *
-
 
877
     * @return int
808
     * Returns -1 if this BigInteger contains no one bits.
878
     */
809
     */
879
    public function getLowestSetBit() : int
810
    public function getLowestSetBit() : int
880
    {
811
    {
881
        $n = $this;
812
        $n = $this;
Zeile 892... Zeile 823...
892
        return -1;
823
        return -1;
893
    }
824
    }
Zeile 894... Zeile 825...
894
 
825
 
895
    /**
826
    /**
896
     * Returns whether this number is even.
-
 
897
     *
-
 
898
     * @return bool
827
     * Returns whether this number is even.
899
     */
828
     */
900
    public function isEven() : bool
829
    public function isEven() : bool
901
    {
830
    {
902
        return \in_array($this->value[-1], ['0', '2', '4', '6', '8'], true);
831
        return \in_array($this->value[-1], ['0', '2', '4', '6', '8'], true);
Zeile 903... Zeile 832...
903
    }
832
    }
904
 
833
 
905
    /**
-
 
906
     * Returns whether this number is odd.
-
 
907
     *
834
    /**
908
     * @return bool
835
     * Returns whether this number is odd.
909
     */
836
     */
910
    public function isOdd() : bool
837
    public function isOdd() : bool
911
    {
838
    {
Zeile 917... Zeile 844...
917
     *
844
     *
918
     * Computes ((this & (1<<n)) != 0).
845
     * Computes ((this & (1<<n)) != 0).
919
     *
846
     *
920
     * @param int $n The bit to test, 0-based.
847
     * @param int $n The bit to test, 0-based.
921
     *
848
     *
922
     * @return bool
-
 
923
     *
-
 
924
     * @throws \InvalidArgumentException If the bit to test is negative.
849
     * @throws \InvalidArgumentException If the bit to test is negative.
925
     */
850
     */
926
    public function testBit(int $n) : bool
851
    public function testBit(int $n) : bool
927
    {
852
    {
928
        if ($n < 0) {
853
        if ($n < 0) {
Zeile 930... Zeile 855...
930
        }
855
        }
Zeile 931... Zeile 856...
931
 
856
 
932
        return $this->shiftedRight($n)->isOdd();
857
        return $this->shiftedRight($n)->isOdd();
Zeile 933... Zeile -...
933
    }
-
 
934
 
-
 
935
    /**
-
 
936
     * {@inheritdoc}
858
    }
937
     */
859
 
938
    public function compareTo($that) : int
860
    public function compareTo(BigNumber|int|float|string $that) : int
Zeile 939... Zeile 861...
939
    {
861
    {
940
        $that = BigNumber::of($that);
862
        $that = BigNumber::of($that);
941
 
863
 
Zeile 942... Zeile 864...
942
        if ($that instanceof BigInteger) {
864
        if ($that instanceof BigInteger) {
943
            return Calculator::get()->cmp($this->value, $that->value);
865
            return Calculator::get()->cmp($this->value, $that->value);
Zeile 944... Zeile -...
944
        }
-
 
945
 
-
 
946
        return - $that->compareTo($this);
-
 
947
    }
866
        }
948
 
867
 
949
    /**
868
        return - $that->compareTo($this);
950
     * {@inheritdoc}
869
    }
Zeile 951... Zeile -...
951
     */
-
 
952
    public function getSign() : int
-
 
953
    {
-
 
954
        return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1);
870
 
955
    }
871
    public function getSign() : int
956
 
872
    {
957
    /**
873
        return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1);
Zeile 958... Zeile -...
958
     * {@inheritdoc}
-
 
959
     */
-
 
960
    public function toBigInteger() : BigInteger
-
 
961
    {
874
    }
962
        return $this;
875
 
963
    }
876
    public function toBigInteger() : BigInteger
964
 
877
    {
Zeile 965... Zeile -...
965
    /**
-
 
966
     * {@inheritdoc}
-
 
967
     */
-
 
968
    public function toBigDecimal() : BigDecimal
878
        return $this;
969
    {
879
    }
970
        return BigDecimal::create($this->value);
880
 
971
    }
881
    public function toBigDecimal() : BigDecimal
Zeile 972... Zeile -...
972
 
-
 
973
    /**
-
 
974
     * {@inheritdoc}
-
 
975
     */
882
    {
976
    public function toBigRational() : BigRational
883
        return self::newBigDecimal($this->value);
977
    {
884
    }
978
        return BigRational::create($this, BigInteger::one(), false);
885
 
Zeile 979... Zeile -...
979
    }
-
 
980
 
-
 
981
    /**
-
 
982
     * {@inheritdoc}
886
    public function toBigRational() : BigRational
983
     */
887
    {
984
    public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
888
        return self::newBigRational($this, BigInteger::one(), false);
Zeile 985... Zeile 889...
985
    {
889
    }
Zeile 998... Zeile 902...
998
        }
902
        }
Zeile 999... Zeile 903...
999
 
903
 
1000
        return $intValue;
904
        return $intValue;
Zeile 1001... Zeile -...
1001
    }
-
 
1002
 
-
 
1003
    /**
-
 
1004
     * {@inheritdoc}
905
    }
1005
     */
906
 
1006
    public function toFloat() : float
907
    public function toFloat() : float
1007
    {
908
    {
Zeile 1008... Zeile 909...
1008
        return (float) $this->value;
909
        return (float) $this->value;
1009
    }
910
    }
1010
 
911
 
1011
    /**
912
    /**
1012
     * Returns a string representation of this number in the given base.
913
     * Returns a string representation of this number in the given base.
1013
     *
-
 
1014
     * The output will always be lowercase for bases greater than 10.
-
 
1015
     *
-
 
1016
     * @param int $base
-
 
1017
     *
914
     *
1018
     * @return string
915
     * The output will always be lowercase for bases greater than 10.
1019
     *
916
     *
1020
     * @throws \InvalidArgumentException If the base is out of range.
917
     * @throws \InvalidArgumentException If the base is out of range.
1021
     */
918
     */
Zeile 1038... Zeile 935...
1038
     * Because this method accepts an alphabet with any character, including dash, it does not handle negative numbers;
935
     * Because this method accepts an alphabet with any character, including dash, it does not handle negative numbers;
1039
     * a NegativeNumberException will be thrown when attempting to call this method on a negative number.
936
     * a NegativeNumberException will be thrown when attempting to call this method on a negative number.
1040
     *
937
     *
1041
     * @param string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
938
     * @param string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
1042
     *
939
     *
1043
     * @return string
-
 
1044
     *
-
 
1045
     * @throws NegativeNumberException   If this number is negative.
940
     * @throws NegativeNumberException   If this number is negative.
1046
     * @throws \InvalidArgumentException If the given alphabet does not contain at least 2 chars.
941
     * @throws \InvalidArgumentException If the given alphabet does not contain at least 2 chars.
1047
     */
942
     */
1048
    public function toArbitraryBase(string $alphabet) : string
943
    public function toArbitraryBase(string $alphabet) : string
1049
    {
944
    {
Zeile 1074... Zeile 969...
1074
     *
969
     *
1075
     * This representation is compatible with the `fromBytes()` factory method, as long as the `$signed` flags match.
970
     * This representation is compatible with the `fromBytes()` factory method, as long as the `$signed` flags match.
1076
     *
971
     *
1077
     * @param bool $signed Whether to output a signed number in two's-complement representation with a leading sign bit.
972
     * @param bool $signed Whether to output a signed number in two's-complement representation with a leading sign bit.
1078
     *
973
     *
1079
     * @return string
-
 
1080
     *
-
 
1081
     * @throws NegativeNumberException If $signed is false, and the number is negative.
974
     * @throws NegativeNumberException If $signed is false, and the number is negative.
1082
     */
975
     */
1083
    public function toBytes(bool $signed = true) : string
976
    public function toBytes(bool $signed = true) : string
1084
    {
977
    {
1085
        if (! $signed && $this->isNegative()) {
978
        if (! $signed && $this->isNegative()) {
Zeile 1119... Zeile 1012...
1119
        }
1012
        }
Zeile 1120... Zeile 1013...
1120
 
1013
 
1121
        return \hex2bin($hex);
1014
        return \hex2bin($hex);
Zeile 1122... Zeile -...
1122
    }
-
 
1123
 
-
 
1124
    /**
-
 
1125
     * {@inheritdoc}
1015
    }
1126
     */
1016
 
1127
    public function __toString() : string
1017
    public function __toString() : string
1128
    {
1018
    {
Zeile 1147... Zeile 1037...
1147
     * @internal
1037
     * @internal
1148
     * @psalm-suppress RedundantPropertyInitializationCheck
1038
     * @psalm-suppress RedundantPropertyInitializationCheck
1149
     *
1039
     *
1150
     * @param array{value: string} $data
1040
     * @param array{value: string} $data
1151
     *
1041
     *
1152
     * @return void
-
 
1153
     *
-
 
1154
     * @throws \LogicException
1042
     * @throws \LogicException
1155
     */
1043
     */
1156
    public function __unserialize(array $data): void
1044
    public function __unserialize(array $data): void
1157
    {
1045
    {
1158
        if (isset($this->value)) {
1046
        if (isset($this->value)) {
Zeile 1164... Zeile 1052...
1164
 
1052
 
1165
    /**
1053
    /**
1166
     * This method is required by interface Serializable and SHOULD NOT be accessed directly.
1054
     * This method is required by interface Serializable and SHOULD NOT be accessed directly.
1167
     *
1055
     *
1168
     * @internal
-
 
1169
     *
-
 
1170
     * @return string
1056
     * @internal
1171
     */
1057
     */
1172
    public function serialize() : string
1058
    public function serialize() : string
1173
    {
1059
    {
1174
        return $this->value;
1060
        return $this->value;
Zeile 1178... Zeile 1064...
1178
     * This method is only here to implement interface Serializable and cannot be accessed directly.
1064
     * This method is only here to implement interface Serializable and cannot be accessed directly.
1179
     *
1065
     *
1180
     * @internal
1066
     * @internal
1181
     * @psalm-suppress RedundantPropertyInitializationCheck
1067
     * @psalm-suppress RedundantPropertyInitializationCheck
1182
     *
1068
     *
1183
     * @param string $value
-
 
1184
     *
-
 
1185
     * @return void
-
 
1186
     *
-
 
1187
     * @throws \LogicException
1069
     * @throws \LogicException
1188
     */
1070
     */
1189
    public function unserialize($value) : void
1071
    public function unserialize($value) : void
1190
    {
1072
    {
1191
        if (isset($this->value)) {
1073
        if (isset($this->value)) {