API reference

Every public method of BigInteger, grouped by purpose.

Every method returns a new BigInteger unless the return type says otherwise. All methods belong to PHPMath\BigInteger\BigInteger.

Construction and output#

Create a number and read its value back as a string.

__construct()#

public function __construct(string|int $value)

Creates a number.

  • Parameters: $value a decimal, 0x hexadecimal or 0b binary string, or an int
  • Throws: InvalidValueException if the value is not a whole number

value()#

public function value(): string

Returns the number as a decimal string.

  • Returns: string

Arithmetic#

Basic arithmetic, division, powers, roots, factorials and binomials.

add()#

public function add(BigInteger $value): self

Adds a number.

  • Parameters: $value the other operand
  • Returns: BigInteger

subtract()#

public function subtract(BigInteger $value): self

Subtracts a number.

  • Parameters: $value the other operand
  • Returns: BigInteger

multiply()#

public function multiply(BigInteger $value): self

Multiplies by a number.

  • Parameters: $value the other operand
  • Returns: BigInteger

divide()#

public function divide(BigInteger $value): self

Integer division, truncated toward zero.

  • Parameters: $value the divisor
  • Returns: BigInteger
  • Throws: InvalidValueException if the divisor is zero

divideRemainder()#

public function divideRemainder(BigInteger $value): self

The remainder of truncated division. It takes the sign of the dividend.

  • Parameters: $value the divisor
  • Returns: BigInteger
  • Throws: InvalidValueException if the divisor is zero

mod()#

public function mod(BigInteger $value): self

Modulo. The result is never negative.

  • Parameters: $value the modulus
  • Returns: BigInteger
  • Throws: InvalidValueException if the modulus is zero

abs()#

public function abs(): self

The absolute value.

  • Returns: BigInteger

negate()#

public function negate(): self

Flips the sign. Zero stays zero.

  • Returns: BigInteger

pow()#

public function pow(int $exponent): self

Raises the number to a power.

  • Parameters: $exponent zero or more
  • Returns: BigInteger
  • Throws: InvalidValueException if the exponent is negative

sqrt()#

public function sqrt(): self

The integer square root, rounded toward zero.

root()#

public function root(int $nth): self

The integer n-th root, rounded toward zero.

  • Parameters: $nth the degree of the root, at least 1
  • Returns: BigInteger
  • Throws: InvalidValueException if $nth is below 1, or the number is negative and $nth is even

factorial()#

public function factorial(): self

The factorial of the number.

binomial()#

public function binomial(int $k): self

The binomial coefficient, "this number choose k".

Comparison and sign#

Compare numbers and inspect their sign.

cmp()#

public function cmp(BigInteger $value): int

Compares with another number.

  • Parameters: $value the number to compare with
  • Returns: -1, 0 or 1

equals()#

public function equals(BigInteger $value): bool

Whether both numbers have the same value.

  • Parameters: $value the number to compare with
  • Returns: bool

sign()#

public function sign(): Sign

The sign as an enum.

  • Returns: Sign::Positive, Sign::Negative or Sign::Zero

isNegative()#

public function isNegative(): bool

True when the number is below zero.

  • Returns: bool

isPositive()#

public function isPositive(): bool

True when the number is above zero.

  • Returns: bool

isZero()#

public function isZero(): bool

True when the number is zero.

  • Returns: bool

Bitwise#

Operations on the binary representation of a number.

bitwiseAnd()#

public function bitwiseAnd(BigInteger $value): self

Bitwise AND.

  • Parameters: $value the other operand
  • Returns: BigInteger

bitwiseOr()#

public function bitwiseOr(BigInteger $value): self

Bitwise OR.

  • Parameters: $value the other operand
  • Returns: BigInteger

bitwiseXor()#

public function bitwiseXor(BigInteger $value): self

Bitwise XOR.

  • Parameters: $value the other operand
  • Returns: BigInteger

bitwiseNot()#

public function bitwiseNot(): self

Flips every bit, which equals -(n + 1).

  • Returns: BigInteger

hammingDistance()#

public function hammingDistance(BigInteger $value): int

The number of differing bits.

  • Parameters: $value the number to compare with
  • Returns: int
  • Throws: InvalidValueException if either number is negative

Number theory#

Primes, divisors, modular arithmetic and quadratic residue symbols.

isPrimeNumber()#

public function isPrimeNumber(float $probabilityFactor = 1.0): bool

Probabilistic primality test as a boolean.

checkPrimeProbability()#

public function checkPrimeProbability(): ProbablePrime

Primality test that tells proven primes from probable ones.

  • Returns: ProbablePrime::Yes, Maybe or No

nextPrime()#

public function nextPrime(): self

The first prime greater than this number.

  • Returns: BigInteger

greatestCommonDivisor()#

public function greatestCommonDivisor(BigInteger $value): self

The greatest common divisor.

  • Parameters: $value the other number
  • Returns: BigInteger

leastCommonMultiple()#

public function leastCommonMultiple(BigInteger $value): self

The least common multiple.

  • Parameters: $value the other number
  • Returns: BigInteger

invert()#

public function invert(BigInteger $value): self

The modular multiplicative inverse.

isPerfectSquare()#

public function isPerfectSquare(): bool

Whether the number is n squared for some integer n.

  • Returns: bool

isPerfectPower()#

public function isPerfectPower(): bool

Whether the number is a^b for integers a and b above 1. Negative numbers return false.

  • Returns: bool

jacobi()#

public function jacobi(BigInteger $value): int

The Jacobi symbol.

  • Parameters: $value an odd positive denominator
  • Returns: -1, 0 or 1

legendre()#

public function legendre(BigInteger $value): int

The Legendre symbol.

  • Parameters: $value an odd prime
  • Returns: -1, 0 or 1

kronecker()#

public function kronecker(BigInteger $value): int

The Kronecker symbol, a generalization of Jacobi.

  • Parameters: $value any integer
  • Returns: -1, 0 or 1

Enums#

Two methods return enums instead of numbers.

Type Cases
PHPMath\BigInteger\Sign Positive, Negative, Zero
PHPMath\BigInteger\ProbablePrime Yes, Maybe, No