PHP Reference ยท Math Functions
PHP GMP functions
Reference for PHP GMP: create arbitrary-size integers with gmp_init, do exact math with gmp_add, gmp_mul, gmp_pow and gmp_powm, and convert results back.
GMP is an extension for working with integers of any size. A normal PHP int is limited to 64 bits (about 9.2 quintillion), and anything larger silently becomes an imprecise float. GMP numbers are GMP objects that can hold thousands of digits and stay exact.
Because the objects overload the arithmetic operators, you can often write $a + $b or $a * $b just as with plain numbers, or call the gmp_* functions. GMP handles integers only; for decimals use BCMath. The extension must be enabled: check with extension_loaded("gmp").
<?php $big = gmp_pow(2, 100); echo $big; // 1267650600228229401496703205376
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
gmp_init(int|string $num, int $base = 0): GMP gmp_add(GMP|int|string $num1, GMP|int|string $num2): GMP gmp_sub(GMP|int|string $num1, GMP|int|string $num2): GMP gmp_mul(GMP|int|string $num1, GMP|int|string $num2): GMP gmp_div_q(GMP|int|string $num1, GMP|int|string $num2, int $rounding_mode = GMP_ROUND_ZERO): GMP gmp_mod(GMP|int|string $num1, GMP|int|string $num2): GMP gmp_pow(GMP|int|string $num, int $exponent): GMP gmp_powm(GMP|int|string $num, GMP|int|string $exponent, GMP|int|string $modulus): GMP gmp_sqrt(GMP|int|string $num): GMP gmp_gcd(GMP|int|string $num1, GMP|int|string $num2): GMP gmp_cmp(GMP|int|string $num1, GMP|int|string $num2): int gmp_strval(GMP|int|string $num, int $base = 10): string gmp_intval(GMP|int|string $num): int
| Name | Description |
|---|---|
$num |
A GMP object, a PHP int, or a numeric string. Strings are parsed as integers. |
$base |
For gmp_init(), the base of the input string (2 to 62). 0 means detect it from a prefix: 0x for hex, 0b for binary, 0 for octal, otherwise decimal. For gmp_strval(), the base of the output. |
$exponent |
For gmp_pow(), a non-negative PHP int. |
$rounding_mode |
GMP_ROUND_ZERO, GMP_ROUND_PLUSINF or GMP_ROUND_MINUSINF. |
Return value: a GMP object for most functions; gmp_strval() returns a string, gmp_cmp() returns a negative number, 0 or a positive number, and gmp_intval() returns an int.
Errors: DivisionByZeroError for division or modulo by zero; ValueError for an invalid number string or base, and for a negative exponent in gmp_pow().
Usage notes
- Print with
echoor(string). AGMPobject converts to its decimal string. Usegmp_strval($n, 16)for other bases. - Operators are overloaded.
+ - * / % **, unary minus and comparison work onGMPobjects./is integer division. Mixing aGMPwith a plainintworks. - Never pass floats. Convert large values to strings first (
"12345678901234567890"); a float has already lost digits. gmp_intval()overflows silently when the number does not fit in anint. Compare againstPHP_INT_MAXfirst or keep values asGMP.gmp_div_q()rounds toward zero by default.gmp_div_qr()returns quotient and remainder together, andgmp_mod()always returns a non-negative result.gmp_powm()is the tool for cryptography-style math. It never builds the huge intermediate power.gmp_prob_prime()is probabilistic:0means definitely composite,1probably prime,2definitely prime.- Speed. GMP is much faster than BCMath for large integers, but each call allocates an object, so it is no replacement for
intin everyday code.