| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Brick\Math; |
| 6 |
|
| 7 |
use Brick\Math\Exception\DivisionByZeroException; |
| 8 |
use Brick\Math\Exception\IntegerOverflowException; |
| 9 |
use Brick\Math\Exception\MathException; |
| 10 |
use Brick\Math\Exception\NegativeNumberException; |
| 11 |
use Brick\Math\Exception\NumberFormatException; |
| 12 |
use Brick\Math\Internal\Calculator; |
| 13 |
use Override; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class BigInteger extends BigNumber |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private readonly string $value; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected function __construct(string $value) |
| 39 |
{ |
| 40 |
$this->value = $value; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[Override] |
| 47 |
protected static function from(BigNumber $number): static |
| 48 |
{ |
| 49 |
return $number->toBigInteger(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function fromBase(string $number, int $base) : BigInteger |
| 72 |
{ |
| 73 |
if ($number === '') { |
| 74 |
throw new NumberFormatException('The number cannot be empty.'); |
| 75 |
} |
| 76 |
|
| 77 |
if ($base < 2 || $base > 36) { |
| 78 |
throw new \InvalidArgumentException(\sprintf('Base %d is not in range 2 to 36.', $base)); |
| 79 |
} |
| 80 |
|
| 81 |
if ($number[0] === '-') { |
| 82 |
$sign = '-'; |
| 83 |
$number = \substr($number, 1); |
| 84 |
} elseif ($number[0] === '+') { |
| 85 |
$sign = ''; |
| 86 |
$number = \substr($number, 1); |
| 87 |
} else { |
| 88 |
$sign = ''; |
| 89 |
} |
| 90 |
|
| 91 |
if ($number === '') { |
| 92 |
throw new NumberFormatException('The number cannot be empty.'); |
| 93 |
} |
| 94 |
|
| 95 |
$number = \ltrim($number, '0'); |
| 96 |
|
| 97 |
if ($number === '') { |
| 98 |
|
| 99 |
return BigInteger::zero(); |
| 100 |
} |
| 101 |
|
| 102 |
if ($number === '1') { |
| 103 |
|
| 104 |
return new BigInteger($sign . '1'); |
| 105 |
} |
| 106 |
|
| 107 |
$pattern = '/[^' . \substr(Calculator::ALPHABET, 0, $base) . ']/'; |
| 108 |
|
| 109 |
if (\preg_match($pattern, \strtolower($number), $matches) === 1) { |
| 110 |
throw new NumberFormatException(\sprintf('"%s" is not a valid character in base %d.', $matches[0], $base)); |
| 111 |
} |
| 112 |
|
| 113 |
if ($base === 10) { |
| 114 |
|
| 115 |
return new BigInteger($sign . $number); |
| 116 |
} |
| 117 |
|
| 118 |
$result = Calculator::get()->fromBase($number, $base); |
| 119 |
|
| 120 |
return new BigInteger($sign . $result); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public static function fromArbitraryBase(string $number, string $alphabet) : BigInteger |
| 137 |
{ |
| 138 |
if ($number === '') { |
| 139 |
throw new NumberFormatException('The number cannot be empty.'); |
| 140 |
} |
| 141 |
|
| 142 |
$base = \strlen($alphabet); |
| 143 |
|
| 144 |
if ($base < 2) { |
| 145 |
throw new \InvalidArgumentException('The alphabet must contain at least 2 chars.'); |
| 146 |
} |
| 147 |
|
| 148 |
$pattern = '/[^' . \preg_quote($alphabet, '/') . ']/'; |
| 149 |
|
| 150 |
if (\preg_match($pattern, $number, $matches) === 1) { |
| 151 |
throw NumberFormatException::charNotInAlphabet($matches[0]); |
| 152 |
} |
| 153 |
|
| 154 |
$number = Calculator::get()->fromArbitraryBase($number, $alphabet, $base); |
| 155 |
|
| 156 |
return new BigInteger($number); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static function fromBytes(string $value, bool $signed = true) : BigInteger |
| 177 |
{ |
| 178 |
if ($value === '') { |
| 179 |
throw new NumberFormatException('The byte string must not be empty.'); |
| 180 |
} |
| 181 |
|
| 182 |
$twosComplement = false; |
| 183 |
|
| 184 |
if ($signed) { |
| 185 |
$x = \ord($value[0]); |
| 186 |
|
| 187 |
if (($twosComplement = ($x >= 0x80))) { |
| 188 |
$value = ~$value; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
$number = self::fromBase(\bin2hex($value), 16); |
| 193 |
|
| 194 |
if ($twosComplement) { |
| 195 |
return $number->plus(1)->negated(); |
| 196 |
} |
| 197 |
|
| 198 |
return $number; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static function randomBits(int $numBits, ?callable $randomBytesGenerator = null) : BigInteger |
| 216 |
{ |
| 217 |
if ($numBits < 0) { |
| 218 |
throw new \InvalidArgumentException('The number of bits cannot be negative.'); |
| 219 |
} |
| 220 |
|
| 221 |
if ($numBits === 0) { |
| 222 |
return BigInteger::zero(); |
| 223 |
} |
| 224 |
|
| 225 |
if ($randomBytesGenerator === null) { |
| 226 |
$randomBytesGenerator = random_bytes(...); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
$byteLength = \intdiv($numBits - 1, 8) + 1; |
| 231 |
|
| 232 |
$extraBits = ($byteLength * 8 - $numBits); |
| 233 |
$bitmask = \chr(0xFF >> $extraBits); |
| 234 |
|
| 235 |
$randomBytes = $randomBytesGenerator($byteLength); |
| 236 |
$randomBytes[0] = $randomBytes[0] & $bitmask; |
| 237 |
|
| 238 |
return self::fromBytes($randomBytes, false); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public static function randomRange( |
| 258 |
BigNumber|int|float|string $min, |
| 259 |
BigNumber|int|float|string $max, |
| 260 |
?callable $randomBytesGenerator = null |
| 261 |
) : BigInteger { |
| 262 |
$min = BigInteger::of($min); |
| 263 |
$max = BigInteger::of($max); |
| 264 |
|
| 265 |
if ($min->isGreaterThan($max)) { |
| 266 |
throw new MathException('$min cannot be greater than $max.'); |
| 267 |
} |
| 268 |
|
| 269 |
if ($min->isEqualTo($max)) { |
| 270 |
return $min; |
| 271 |
} |
| 272 |
|
| 273 |
$diff = $max->minus($min); |
| 274 |
$bitLength = $diff->getBitLength(); |
| 275 |
|
| 276 |
|
| 277 |
do { |
| 278 |
$randomNumber = self::randomBits($bitLength, $randomBytesGenerator); |
| 279 |
} while ($randomNumber->isGreaterThan($diff)); |
| 280 |
|
| 281 |
return $randomNumber->plus($min); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public static function zero() : BigInteger |
| 290 |
{ |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static $zero; |
| 296 |
|
| 297 |
if ($zero === null) { |
| 298 |
$zero = new BigInteger('0'); |
| 299 |
} |
| 300 |
|
| 301 |
return $zero; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public static function one() : BigInteger |
| 310 |
{ |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
static $one; |
| 316 |
|
| 317 |
if ($one === null) { |
| 318 |
$one = new BigInteger('1'); |
| 319 |
} |
| 320 |
|
| 321 |
return $one; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public static function ten() : BigInteger |
| 330 |
{ |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static $ten; |
| 336 |
|
| 337 |
if ($ten === null) { |
| 338 |
$ten = new BigInteger('10'); |
| 339 |
} |
| 340 |
|
| 341 |
return $ten; |
| 342 |
} |
| 343 |
|
| 344 |
public static function gcdMultiple(BigInteger $a, BigInteger ...$n): BigInteger |
| 345 |
{ |
| 346 |
$result = $a; |
| 347 |
|
| 348 |
foreach ($n as $next) { |
| 349 |
$result = $result->gcd($next); |
| 350 |
|
| 351 |
if ($result->isEqualTo(1)) { |
| 352 |
return $result; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
return $result; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function plus(BigNumber|int|float|string $that) : BigInteger |
| 367 |
{ |
| 368 |
$that = BigInteger::of($that); |
| 369 |
|
| 370 |
if ($that->value === '0') { |
| 371 |
return $this; |
| 372 |
} |
| 373 |
|
| 374 |
if ($this->value === '0') { |
| 375 |
return $that; |
| 376 |
} |
| 377 |
|
| 378 |
$value = Calculator::get()->add($this->value, $that->value); |
| 379 |
|
| 380 |
return new BigInteger($value); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public function minus(BigNumber|int|float|string $that) : BigInteger |
| 391 |
{ |
| 392 |
$that = BigInteger::of($that); |
| 393 |
|
| 394 |
if ($that->value === '0') { |
| 395 |
return $this; |
| 396 |
} |
| 397 |
|
| 398 |
$value = Calculator::get()->sub($this->value, $that->value); |
| 399 |
|
| 400 |
return new BigInteger($value); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public function multipliedBy(BigNumber|int|float|string $that) : BigInteger |
| 411 |
{ |
| 412 |
$that = BigInteger::of($that); |
| 413 |
|
| 414 |
if ($that->value === '1') { |
| 415 |
return $this; |
| 416 |
} |
| 417 |
|
| 418 |
if ($this->value === '1') { |
| 419 |
return $that; |
| 420 |
} |
| 421 |
|
| 422 |
$value = Calculator::get()->mul($this->value, $that->value); |
| 423 |
|
| 424 |
return new BigInteger($value); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function dividedBy(BigNumber|int|float|string $that, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigInteger |
| 437 |
{ |
| 438 |
$that = BigInteger::of($that); |
| 439 |
|
| 440 |
if ($that->value === '1') { |
| 441 |
return $this; |
| 442 |
} |
| 443 |
|
| 444 |
if ($that->value === '0') { |
| 445 |
throw DivisionByZeroException::divisionByZero(); |
| 446 |
} |
| 447 |
|
| 448 |
$result = Calculator::get()->divRound($this->value, $that->value, $roundingMode); |
| 449 |
|
| 450 |
return new BigInteger($result); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public function power(int $exponent) : BigInteger |
| 459 |
{ |
| 460 |
if ($exponent === 0) { |
| 461 |
return BigInteger::one(); |
| 462 |
} |
| 463 |
|
| 464 |
if ($exponent === 1) { |
| 465 |
return $this; |
| 466 |
} |
| 467 |
|
| 468 |
if ($exponent < 0 || $exponent > Calculator::MAX_POWER) { |
| 469 |
throw new \InvalidArgumentException(\sprintf( |
| 470 |
'The exponent %d is not in the range 0 to %d.', |
| 471 |
$exponent, |
| 472 |
Calculator::MAX_POWER |
| 473 |
)); |
| 474 |
} |
| 475 |
|
| 476 |
return new BigInteger(Calculator::get()->pow($this->value, $exponent)); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function quotient(BigNumber|int|float|string $that) : BigInteger |
| 487 |
{ |
| 488 |
$that = BigInteger::of($that); |
| 489 |
|
| 490 |
if ($that->value === '1') { |
| 491 |
return $this; |
| 492 |
} |
| 493 |
|
| 494 |
if ($that->value === '0') { |
| 495 |
throw DivisionByZeroException::divisionByZero(); |
| 496 |
} |
| 497 |
|
| 498 |
$quotient = Calculator::get()->divQ($this->value, $that->value); |
| 499 |
|
| 500 |
return new BigInteger($quotient); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function remainder(BigNumber|int|float|string $that) : BigInteger |
| 513 |
{ |
| 514 |
$that = BigInteger::of($that); |
| 515 |
|
| 516 |
if ($that->value === '1') { |
| 517 |
return BigInteger::zero(); |
| 518 |
} |
| 519 |
|
| 520 |
if ($that->value === '0') { |
| 521 |
throw DivisionByZeroException::divisionByZero(); |
| 522 |
} |
| 523 |
|
| 524 |
$remainder = Calculator::get()->divR($this->value, $that->value); |
| 525 |
|
| 526 |
return new BigInteger($remainder); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public function quotientAndRemainder(BigNumber|int|float|string $that) : array |
| 541 |
{ |
| 542 |
$that = BigInteger::of($that); |
| 543 |
|
| 544 |
if ($that->value === '0') { |
| 545 |
throw DivisionByZeroException::divisionByZero(); |
| 546 |
} |
| 547 |
|
| 548 |
[$quotient, $remainder] = Calculator::get()->divQR($this->value, $that->value); |
| 549 |
|
| 550 |
return [ |
| 551 |
new BigInteger($quotient), |
| 552 |
new BigInteger($remainder) |
| 553 |
]; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public function mod(BigNumber|int|float|string $that) : BigInteger |
| 569 |
{ |
| 570 |
$that = BigInteger::of($that); |
| 571 |
|
| 572 |
if ($that->value === '0') { |
| 573 |
throw DivisionByZeroException::modulusMustNotBeZero(); |
| 574 |
} |
| 575 |
|
| 576 |
$value = Calculator::get()->mod($this->value, $that->value); |
| 577 |
|
| 578 |
return new BigInteger($value); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public function modInverse(BigInteger $m) : BigInteger |
| 590 |
{ |
| 591 |
if ($m->value === '0') { |
| 592 |
throw DivisionByZeroException::modulusMustNotBeZero(); |
| 593 |
} |
| 594 |
|
| 595 |
if ($m->isNegative()) { |
| 596 |
throw new NegativeNumberException('Modulus must not be negative.'); |
| 597 |
} |
| 598 |
|
| 599 |
if ($m->value === '1') { |
| 600 |
return BigInteger::zero(); |
| 601 |
} |
| 602 |
|
| 603 |
$value = Calculator::get()->modInverse($this->value, $m->value); |
| 604 |
|
| 605 |
if ($value === null) { |
| 606 |
throw new MathException('Unable to compute the modInverse for the given modulus.'); |
| 607 |
} |
| 608 |
|
| 609 |
return new BigInteger($value); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public function modPow(BigNumber|int|float|string $exp, BigNumber|int|float|string $mod) : BigInteger |
| 624 |
{ |
| 625 |
$exp = BigInteger::of($exp); |
| 626 |
$mod = BigInteger::of($mod); |
| 627 |
|
| 628 |
if ($this->isNegative() || $exp->isNegative() || $mod->isNegative()) { |
| 629 |
throw new NegativeNumberException('The operands cannot be negative.'); |
| 630 |
} |
| 631 |
|
| 632 |
if ($mod->isZero()) { |
| 633 |
throw DivisionByZeroException::modulusMustNotBeZero(); |
| 634 |
} |
| 635 |
|
| 636 |
$result = Calculator::get()->modPow($this->value, $exp->value, $mod->value); |
| 637 |
|
| 638 |
return new BigInteger($result); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
public function gcd(BigNumber|int|float|string $that) : BigInteger |
| 649 |
{ |
| 650 |
$that = BigInteger::of($that); |
| 651 |
|
| 652 |
if ($that->value === '0' && $this->value[0] !== '-') { |
| 653 |
return $this; |
| 654 |
} |
| 655 |
|
| 656 |
if ($this->value === '0' && $that->value[0] !== '-') { |
| 657 |
return $that; |
| 658 |
} |
| 659 |
|
| 660 |
$value = Calculator::get()->gcd($this->value, $that->value); |
| 661 |
|
| 662 |
return new BigInteger($value); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
n. |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
public function sqrt() : BigInteger |
| 673 |
{ |
| 674 |
if ($this->value[0] === '-') { |
| 675 |
throw new NegativeNumberException('Cannot calculate the square root of a negative number.'); |
| 676 |
} |
| 677 |
|
| 678 |
$value = Calculator::get()->sqrt($this->value); |
| 679 |
|
| 680 |
return new BigInteger($value); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public function abs() : BigInteger |
| 687 |
{ |
| 688 |
return $this->isNegative() ? $this->negated() : $this; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
public function negated() : BigInteger |
| 695 |
{ |
| 696 |
return new BigInteger(Calculator::get()->neg($this->value)); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
public function and(BigNumber|int|float|string $that) : BigInteger |
| 707 |
{ |
| 708 |
$that = BigInteger::of($that); |
| 709 |
|
| 710 |
return new BigInteger(Calculator::get()->and($this->value, $that->value)); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
public function or(BigNumber|int|float|string $that) : BigInteger |
| 721 |
{ |
| 722 |
$that = BigInteger::of($that); |
| 723 |
|
| 724 |
return new BigInteger(Calculator::get()->or($this->value, $that->value)); |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
public function xor(BigNumber|int|float|string $that) : BigInteger |
| 735 |
{ |
| 736 |
$that = BigInteger::of($that); |
| 737 |
|
| 738 |
return new BigInteger(Calculator::get()->xor($this->value, $that->value)); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public function not() : BigInteger |
| 745 |
{ |
| 746 |
return $this->negated()->minus(1); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
public function shiftedLeft(int $distance) : BigInteger |
| 753 |
{ |
| 754 |
if ($distance === 0) { |
| 755 |
return $this; |
| 756 |
} |
| 757 |
|
| 758 |
if ($distance < 0) { |
| 759 |
return $this->shiftedRight(- $distance); |
| 760 |
} |
| 761 |
|
| 762 |
return $this->multipliedBy(BigInteger::of(2)->power($distance)); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public function shiftedRight(int $distance) : BigInteger |
| 769 |
{ |
| 770 |
if ($distance === 0) { |
| 771 |
return $this; |
| 772 |
} |
| 773 |
|
| 774 |
if ($distance < 0) { |
| 775 |
return $this->shiftedLeft(- $distance); |
| 776 |
} |
| 777 |
|
| 778 |
$operand = BigInteger::of(2)->power($distance); |
| 779 |
|
| 780 |
if ($this->isPositiveOrZero()) { |
| 781 |
return $this->quotient($operand); |
| 782 |
} |
| 783 |
|
| 784 |
return $this->dividedBy($operand, RoundingMode::UP); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
public function getBitLength() : int |
| 794 |
{ |
| 795 |
if ($this->value === '0') { |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
if ($this->isNegative()) { |
| 800 |
return $this->abs()->minus(1)->getBitLength(); |
| 801 |
} |
| 802 |
|
| 803 |
return \strlen($this->toBase(2)); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public function getLowestSetBit() : int |
| 812 |
{ |
| 813 |
$n = $this; |
| 814 |
$bitLength = $this->getBitLength(); |
| 815 |
|
| 816 |
for ($i = 0; $i <= $bitLength; $i++) { |
| 817 |
if ($n->isOdd()) { |
| 818 |
return $i; |
| 819 |
} |
| 820 |
|
| 821 |
$n = $n->shiftedRight(1); |
| 822 |
} |
| 823 |
|
| 824 |
return -1; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
public function isEven() : bool |
| 831 |
{ |
| 832 |
return \in_array($this->value[-1], ['0', '2', '4', '6', '8'], true); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
public function isOdd() : bool |
| 839 |
{ |
| 840 |
return \in_array($this->value[-1], ['1', '3', '5', '7', '9'], true); |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
public function testBit(int $n) : bool |
| 853 |
{ |
| 854 |
if ($n < 0) { |
| 855 |
throw new \InvalidArgumentException('The bit to test cannot be negative.'); |
| 856 |
} |
| 857 |
|
| 858 |
return $this->shiftedRight($n)->isOdd(); |
| 859 |
} |
| 860 |
|
| 861 |
#[Override] |
| 862 |
public function compareTo(BigNumber|int|float|string $that) : int |
| 863 |
{ |
| 864 |
$that = BigNumber::of($that); |
| 865 |
|
| 866 |
if ($that instanceof BigInteger) { |
| 867 |
return Calculator::get()->cmp($this->value, $that->value); |
| 868 |
} |
| 869 |
|
| 870 |
return - $that->compareTo($this); |
| 871 |
} |
| 872 |
|
| 873 |
#[Override] |
| 874 |
public function getSign() : int |
| 875 |
{ |
| 876 |
return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1); |
| 877 |
} |
| 878 |
|
| 879 |
#[Override] |
| 880 |
public function toBigInteger() : BigInteger |
| 881 |
{ |
| 882 |
return $this; |
| 883 |
} |
| 884 |
|
| 885 |
#[Override] |
| 886 |
public function toBigDecimal() : BigDecimal |
| 887 |
{ |
| 888 |
return self::newBigDecimal($this->value); |
| 889 |
} |
| 890 |
|
| 891 |
#[Override] |
| 892 |
public function toBigRational() : BigRational |
| 893 |
{ |
| 894 |
return self::newBigRational($this, BigInteger::one(), false); |
| 895 |
} |
| 896 |
|
| 897 |
#[Override] |
| 898 |
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal |
| 899 |
{ |
| 900 |
return $this->toBigDecimal()->toScale($scale, $roundingMode); |
| 901 |
} |
| 902 |
|
| 903 |
#[Override] |
| 904 |
public function toInt() : int |
| 905 |
{ |
| 906 |
$intValue = (int) $this->value; |
| 907 |
|
| 908 |
if ($this->value !== (string) $intValue) { |
| 909 |
throw IntegerOverflowException::toIntOverflow($this); |
| 910 |
} |
| 911 |
|
| 912 |
return $intValue; |
| 913 |
} |
| 914 |
|
| 915 |
#[Override] |
| 916 |
public function toFloat() : float |
| 917 |
{ |
| 918 |
return (float) $this->value; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
public function toBase(int $base) : string |
| 929 |
{ |
| 930 |
if ($base === 10) { |
| 931 |
return $this->value; |
| 932 |
} |
| 933 |
|
| 934 |
if ($base < 2 || $base > 36) { |
| 935 |
throw new \InvalidArgumentException(\sprintf('Base %d is out of range [2, 36]', $base)); |
| 936 |
} |
| 937 |
|
| 938 |
return Calculator::get()->toBase($this->value, $base); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
public function toArbitraryBase(string $alphabet) : string |
| 953 |
{ |
| 954 |
$base = \strlen($alphabet); |
| 955 |
|
| 956 |
if ($base < 2) { |
| 957 |
throw new \InvalidArgumentException('The alphabet must contain at least 2 chars.'); |
| 958 |
} |
| 959 |
|
| 960 |
if ($this->value[0] === '-') { |
| 961 |
throw new NegativeNumberException(__FUNCTION__ . '() does not support negative numbers.'); |
| 962 |
} |
| 963 |
|
| 964 |
return Calculator::get()->toArbitraryBase($this->value, $alphabet, $base); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
public function toBytes(bool $signed = true) : string |
| 986 |
{ |
| 987 |
if (! $signed && $this->isNegative()) { |
| 988 |
throw new NegativeNumberException('Cannot convert a negative number to a byte string when $signed is false.'); |
| 989 |
} |
| 990 |
|
| 991 |
$hex = $this->abs()->toBase(16); |
| 992 |
|
| 993 |
if (\strlen($hex) % 2 !== 0) { |
| 994 |
$hex = '0' . $hex; |
| 995 |
} |
| 996 |
|
| 997 |
$baseHexLength = \strlen($hex); |
| 998 |
|
| 999 |
if ($signed) { |
| 1000 |
if ($this->isNegative()) { |
| 1001 |
$bin = \hex2bin($hex); |
| 1002 |
assert($bin !== false); |
| 1003 |
|
| 1004 |
$hex = \bin2hex(~$bin); |
| 1005 |
$hex = self::fromBase($hex, 16)->plus(1)->toBase(16); |
| 1006 |
|
| 1007 |
$hexLength = \strlen($hex); |
| 1008 |
|
| 1009 |
if ($hexLength < $baseHexLength) { |
| 1010 |
$hex = \str_repeat('0', $baseHexLength - $hexLength) . $hex; |
| 1011 |
} |
| 1012 |
|
| 1013 |
if ($hex[0] < '8') { |
| 1014 |
$hex = 'FF' . $hex; |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
if ($hex[0] >= '8') { |
| 1018 |
$hex = '00' . $hex; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
return \hex2bin($hex); |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[Override] |
| 1027 |
public function __toString() : string |
| 1028 |
{ |
| 1029 |
return $this->value; |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
public function __serialize(): array |
| 1040 |
{ |
| 1041 |
return ['value' => $this->value]; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
public function __unserialize(array $data): void |
| 1055 |
{ |
| 1056 |
if (isset($this->value)) { |
| 1057 |
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.'); |
| 1058 |
} |
| 1059 |
|
| 1060 |
$this->value = $data['value']; |
| 1061 |
} |
| 1062 |
} |