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:
$valuea decimal,0xhexadecimal or0bbinary string, or an int - Throws: InvalidValueException if the value is not a whole number
Arithmetic#
Basic arithmetic, division, powers, roots, factorials and binomials.
add()#
public function add(BigInteger $value): self
Adds a number.
- Parameters:
$valuethe other operand - Returns:
BigInteger
subtract()#
public function subtract(BigInteger $value): self
Subtracts a number.
- Parameters:
$valuethe other operand - Returns:
BigInteger
multiply()#
public function multiply(BigInteger $value): self
Multiplies by a number.
- Parameters:
$valuethe other operand - Returns:
BigInteger
divide()#
public function divide(BigInteger $value): self
Integer division, truncated toward zero.
- Parameters:
$valuethe 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:
$valuethe divisor - Returns:
BigInteger - Throws: InvalidValueException if the divisor is zero
mod()#
public function mod(BigInteger $value): self
Modulo. The result is never negative.
- Parameters:
$valuethe modulus - Returns:
BigInteger - Throws: InvalidValueException if the modulus is zero
pow()#
public function pow(int $exponent): self
Raises the number to a power.
- Parameters:
$exponentzero or more - Returns:
BigInteger - Throws: InvalidValueException if the exponent is negative
sqrt()#
public function sqrt(): self
The integer square root, rounded toward zero.
- Returns:
BigInteger - Throws: InvalidValueException if the number is negative
root()#
public function root(int $nth): self
The integer n-th root, rounded toward zero.
- Parameters:
$nththe degree of the root, at least 1 - Returns:
BigInteger - Throws: InvalidValueException if
$nthis below 1, or the number is negative and$nthis even
factorial()#
public function factorial(): self
The factorial of the number.
- Returns:
BigInteger - Throws: InvalidValueException if the number is negative
binomial()#
public function binomial(int $k): self
The binomial coefficient, "this number choose k".
- Parameters:
$kzero or more - Returns:
BigInteger - Throws: InvalidValueException if
$kis negative
Comparison and sign#
Compare numbers and inspect their sign.
cmp()#
public function cmp(BigInteger $value): int
Compares with another number.
- Parameters:
$valuethe number to compare with - Returns:
-1,0or1
equals()#
public function equals(BigInteger $value): bool
Whether both numbers have the same value.
- Parameters:
$valuethe number to compare with - Returns:
bool
sign()#
public function sign(): Sign
The sign as an enum.
- Returns:
Sign::Positive,Sign::NegativeorSign::Zero
Bitwise#
Operations on the binary representation of a number.
bitwiseAnd()#
public function bitwiseAnd(BigInteger $value): self
Bitwise AND.
- Parameters:
$valuethe other operand - Returns:
BigInteger
bitwiseOr()#
public function bitwiseOr(BigInteger $value): self
Bitwise OR.
- Parameters:
$valuethe other operand - Returns:
BigInteger
bitwiseXor()#
public function bitwiseXor(BigInteger $value): self
Bitwise XOR.
- Parameters:
$valuethe 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:
$valuethe 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.
- Parameters:
$probabilityFactorfrom 0.0 (fast) to 1.0 (thorough) - Returns:
bool - Throws: InvalidProbabilityFactorException if the factor is outside 0.0 to 1.0
checkPrimeProbability()#
public function checkPrimeProbability(): ProbablePrime
Primality test that tells proven primes from probable ones.
- Returns:
ProbablePrime::Yes,MaybeorNo
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:
$valuethe other number - Returns:
BigInteger
leastCommonMultiple()#
public function leastCommonMultiple(BigInteger $value): self
The least common multiple.
- Parameters:
$valuethe other number - Returns:
BigInteger
invert()#
public function invert(BigInteger $value): self
The modular multiplicative inverse.
- Parameters:
$valuethe modulus - Returns:
BigInteger - Throws: InvalidValueException if the modulus is zero, NotInvertibleException if no inverse exists
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:
$valuean odd positive denominator - Returns:
-1,0or1
legendre()#
public function legendre(BigInteger $value): int
The Legendre symbol.
- Parameters:
$valuean odd prime - Returns:
-1,0or1
kronecker()#
public function kronecker(BigInteger $value): int
The Kronecker symbol, a generalization of Jacobi.
- Parameters:
$valueany integer - Returns:
-1,0or1
Enums#
Two methods return enums instead of numbers.
| Type | Cases |
|---|---|
PHPMath\BigInteger\Sign |
Positive, Negative, Zero |
PHPMath\BigInteger\ProbablePrime |
Yes, Maybe, No |