This extension wraps the mpz_() and mpf_() functions in libgmp. It differs from the delivered GMP extension in PHP in a few key ways:
- Object method API rather than function/resource based API
- Support for floating point numbers
In the API summaries below, the psuedo type Number
represents an instance of GMPi, an int, a float (if appropriate), or a numeric string. Numeric strings may be expressed as decimal, binary (with a leading '0b'), hex (with a leading '0x'), or octal (with a leading '0').
interface GMPi {
public function add(Number $val): GMPi;
public function sub(Number $val): GMPi;
public function mul(Number $val): GMPi;
public function div(Number $val): GMPi;
public function pow(Number $val): GMPi;
public function cmp(Number $val): int; // -1, 0, 1
public function neg(): GMPi;
public function abs(): GMPi;
public function toFloat(): GMPi\Float;
public function toInteger(): GMPi\Intger;
public function __toString(): string; // decimal output
public function __debugInfo(): array; // 'dec' and 'hex' keys
}
namespace GMPi;
class Float implements \GMPi {
public function __construct(Number $val, int $base = 0,
int $precision = ini_get('gmpi.precision'));
public function toParts(): array; // 'mantissa' and 'exponent' keys
public function toString(int $base = 10,
int $maxpad = ini_get('gmpi.padding'));
}
namesapce GMPi;
class Integer implements \GMPi {
public function __construct(Number $val, int $base = 0);
public function mod(Number $val): GMPi\Integer;
public function and(Number $val): GMPi\Integer;
public function or(Number $val): GMPi\Integer;
public function xor(Number $val): GMPi\Integer;
public function powMod(Number $val, int $mod): GMPi\Integer;
public function congruent(Number $val, int $mod): GMPi\Integer;
public function toString(int $base = 10): string;
}
Additionally, GMPi* implementations support similar operator overloading to the GMP object.
Invalid inputs of any type will throw an instance of \TypeError