Ref/gmp-Phpdoc专题

参见

More mathematical functions can be found in the 数学扩展 section

gmp_abs

Absolute value

说明

GMP gmp_abs ( <span class="type">GMPint<span class="type">string $num )

Get the absolute value of a number.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns the absolute value of num, as a GMP number.

范例

示例 #1 gmp_abs example

<?php
$abs1 = gmp_abs("274982683358");
$abs2 = gmp_abs("-274982683358");

echo gmp_strval($abs1) . "\n";
echo gmp_strval($abs2) . "\n";
?>

以上例程会输出:

274982683358
274982683358

gmp_add

Add numbers

说明

GMP gmp_add ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Add two numbers.

参数

num1
The first summand (augent).

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The second summand (addend).

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

A GMP number representing the sum of the arguments.

范例

示例 #1 gmp_add example

<?php
$sum = gmp_add("123456789012345", "76543210987655");
echo gmp_strval($sum) . "\n";
?>

以上例程会输出:

200000000000000

gmp_and

Bitwise AND

说明

GMP gmp_and ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Calculates bitwise AND of two GMP numbers.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

A GMP number representing the bitwise AND comparison.

范例

示例 #1 gmp_and example

<?php
$and1 = gmp_and("0xfffffffff4", "0x4");
$and2 = gmp_and("0xfffffffff4", "0x8");
echo gmp_strval($and1) . "\n";
echo gmp_strval($and2) . "\n";
?>

以上例程会输出:

4
0

gmp_binomial

Calculates binomial coefficient

说明

GMP <span class="methodname">gmp_binomial ( <span class="methodparam"><span class="type">GMPint<span class="type">string $n , <span class="methodparam">int $k )

Calculates the binomial coefficient C(n, k).

参数

n
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

k

返回值

Returns the binomial coefficient C(n, k).

错误/异常

Throws ValueError if k is negative. Prior to PHP 8.0.0, E_WARNING was issued instead.

更新日志

版本 说明
8.0.0 This function no longer returns false on failure.

gmp_clrbit

Clear bit

说明

void <span class="methodname">gmp_clrbit ( <span class="type">GMP $num , <span class="type">int $index )

Clears (sets to 0) bit index in num. The index starts at 0.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

index
The index of the bit to clear. Index 0 represents the least significant bit.

返回值

GMP 对象。

范例

示例 #1 gmp_clrbit example

<?php
$a = gmp_init("0xff");
gmp_clrbit($a, 0); // index starts at 0, least significant bit
echo gmp_strval($a) . "\n";
?>

以上例程会输出:

254

注释

Note:

Unlike most of the other GMP functions, <span class="function">gmp_clrbit must be called with a GMP object that already exists (using gmp_init for example). One will not be automatically created.

参见

  • gmp_setbit
  • gmp_testbit

gmp_cmp

Compare numbers

说明

int gmp_cmp ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Compares two numbers.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns a positive value if a > b, zero if a = b and a negative value if a \< b.

范例

示例 #1 gmp_cmp example

<?php
$cmp1 = gmp_cmp("1234", "1000"); // greater than
$cmp2 = gmp_cmp("1000", "1234"); // less than
$cmp3 = gmp_cmp("1234", "1234"); // equal to

echo "$cmp1 $cmp2 $cmp3\n";
?>

以上例程会输出:

1 -1 0

gmp_com

Calculates one's complement

说明

GMP gmp_com ( <span class="type">GMPint<span class="type">string $num )

Returns the one's complement of num.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns the one's complement of num, as a GMP number.

范例

示例 #1 gmp_com example

<?php
$com = gmp_com("1234");
echo gmp_strval($com) . "\n";
?>

以上例程会输出:

-1235

gmp_div_q

Divide numbers

说明

GMP <span class="methodname">gmp_div_q ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 [, <span class="methodparam">int $rounding_mode<span class="initializer"> = GMP_ROUND_ZERO ] )

Divides num1 by num2 and returns the integer result.

参数

num1
The number being divided.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The number that num1 is being divided by.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

rounding_mode
The result rounding is defined by the rounding_mode, which can have the following values:

  • GMP_ROUND_ZERO: The result is truncated towards 0.
  • GMP_ROUND_PLUSINF: The result is rounded towards +infinity.
  • GMP_ROUND_MINUSINF: The result is rounded towards -infinity.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_div_q example

<?php
$div1 = gmp_div_q("100", "5");
echo gmp_strval($div1) . "\n";

$div2 = gmp_div_q("1", "3");
echo gmp_strval($div2) . "\n";

$div3 = gmp_div_q("1", "3", GMP_ROUND_PLUSINF);
echo gmp_strval($div3) . "\n";

$div4 = gmp_div_q("-1", "4", GMP_ROUND_PLUSINF);
echo gmp_strval($div4) . "\n";

$div5 = gmp_div_q("-1", "4", GMP_ROUND_MINUSINF);
echo gmp_strval($div5) . "\n";
?>

以上例程会输出:

20
0
1
0
-1

注释

Note:

This function can also be called as <span class="function">gmp_div.

参见

  • gmp_div_r
  • gmp_div_qr

gmp_div_qr

Divide numbers and get quotient and remainder

说明

array <span class="methodname">gmp_div_qr ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 [, <span class="methodparam">int $rounding_mode<span class="initializer"> = GMP_ROUND_ZERO ] )

The function divides num1 by num2.

参数

num1
The number being divided.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The number that num1 is being divided by.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

rounding_mode
See the gmp_div_q function for description of the rounding_mode argument.

返回值

Returns an array, with the first element being [n/d] (the integer result of the division) and the second being *(n

  • [n/d] * d)* (the remainder of the division).

范例

示例 #1 Division of GMP numbers

<?php
$a = gmp_init("0x41682179fbf5");
$res = gmp_div_qr($a, "0xDEFE75");
printf("Result is: q - %s, r - %s",
       gmp_strval($res[0]), gmp_strval($res[1]));
?>

参见

  • gmp_div_q
  • gmp_div_r

gmp_div_r

Remainder of the division of numbers

说明

GMP <span class="methodname">gmp_div_r ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 [, <span class="methodparam">int $rounding_mode<span class="initializer"> = GMP_ROUND_ZERO ] )

Calculates remainder of the integer division of num1 by num2. The remainder has the sign of the num1 argument, if not zero.

参数

num1
The number being divided.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The number that num1 is being divided by.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

rounding_mode
See the gmp_div_q function for description of the rounding_mode argument.

返回值

The remainder, as a GMP number.

范例

示例 #1 gmp_div_r example

<?php
$div = gmp_div_r("105", "20");
echo gmp_strval($div) . "\n";
?>

以上例程会输出:

5

参见

  • gmp_div_q
  • gmp_div_qr

gmp_div

别名 gmp_div_q

说明

此函数是该函数的别名: gmp_div_q.

gmp_divexact

Exact division of numbers

说明

GMP <span class="methodname">gmp_divexact ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Divides num1 by num2, using fast "exact division" algorithm. This function produces correct results only when it is known in advance that num2 divides num1.

参数

num1
The number being divided.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The number that a is being divided by.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_divexact example

<?php
$div1 = gmp_divexact("10", "2");
echo gmp_strval($div1) . "\n";

$div2 = gmp_divexact("10", "3"); // bogus result
echo gmp_strval($div2) . "\n";
?>

以上例程会输出:

5
2863311534

gmp_export

Export to a binary string

说明

string <span class="methodname">gmp_export ( <span class="type">GMP<span class="type">intstring $num [, <span class="type">int $word_size = 1 [, <span class="type">int $flags = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] )

Export a GMP number to a binary string

参数

num
The GMP number being exported

word_size
Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter.

flags
Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.

返回值

Returns a string.

更新日志

版本 说明
8.0.0 This function no longer returns false on failure.

范例

示例 #1 gmp_export example

<?php
$number = gmp_init(16705);
echo gmp_export($number) . "\n";
?>

以上例程会输出:

AA

参见

  • gmp_import

gmp_fact

Factorial

说明

GMP gmp_fact ( <span class="type">GMPint<span class="type">string $num )

Calculates factorial (num!) of num.

参数

num
The factorial number.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_fact example

<?php
$fact1 = gmp_fact(5); // 5 * 4 * 3 * 2 * 1
echo gmp_strval($fact1) . "\n";

$fact2 = gmp_fact(50); // 50 * 49 * 48, ... etc
echo gmp_strval($fact2) . "\n";
?>

以上例程会输出:

120
30414093201713378043612608166064768844377641568960512000000000000

gmp_gcd

Calculate GCD

说明

GMP gmp_gcd ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Calculate greatest common divisor of num1 and num2. The result is always positive even if either of, or both, input operands are negative.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

A positive GMP number that divides into both num1 and num2.

范例

示例 #1 gmp_gcd example

<?php
$gcd = gmp_gcd("12", "21");
echo gmp_strval($gcd) . "\n";
?>

以上例程会输出:

3

参见

  • gmp_lcm

gmp_gcdext

Calculate GCD and multipliers

说明

array <span class="methodname">gmp_gcdext ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 )

Calculates g, s, and t, such that a*s + b*t = g = gcd(a,b), where gcd is the greatest common divisor. Returns an array with respective elements g, s and t.

This function can be used to solve linear Diophantine equations in two variables. These are equations that allow only integer solutions and have the form: a*x + b*y = c. For more information, go to the » "Diophantine Equation" page at MathWorld

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

An array of GMP numbers.

范例

示例 #1 Solving a linear Diophantine equation

<?php
// Solve the equation a*s + b*t = g
// where a = 12, b = 21, g = gcd(12, 21) = 3
$a = gmp_init(12);
$b = gmp_init(21);
$g = gmp_gcd($a, $b);
$r = gmp_gcdext($a, $b);

$check_gcd = (gmp_strval($g) == gmp_strval($r['g']));
$eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t']));
$check_res = (gmp_strval($g) == gmp_strval($eq_res));

if ($check_gcd && $check_res) {
    $fmt = "Solution: %d*%d + %d*%d = %d\n";
    printf($fmt, gmp_strval($a), gmp_strval($r['s']), gmp_strval($b),
    gmp_strval($r['t']), gmp_strval($r['g']));
} else {
    echo "Error while solving the equation\n";
}

// output: Solution: 12*2 + 21*-1 = 3
?>

gmp_hamdist

Hamming distance

说明

int <span class="methodname">gmp_hamdist ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 )

Returns the hamming distance between num1 and num2. Both operands should be non-negative.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

It should be positive.

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

It should be positive.

返回值

The hamming distance between num1 and num2, as an <span class="type">int.

范例

示例 #1 gmp_hamdist example

<?php
$ham1 = gmp_init("1001010011", 2);
$ham2 = gmp_init("1011111100", 2);
echo gmp_hamdist($ham1, $ham2) . "\n";

/* hamdist is equivalent to: */
echo gmp_popcount(gmp_xor($ham1, $ham2)) . "\n";
?>

以上例程会输出:

6
6

参见

  • gmp_popcount
  • gmp_xor

gmp_import

Import from a binary string

说明

GMP <span class="methodname">gmp_import ( <span class="type">string $data [, <span class="methodparam">int $word_size<span class="initializer"> = 1 [, <span class="methodparam">int $flags<span class="initializer"> = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] )

Import a GMP number from a binary string

参数

data
The binary string being imported

word_size
Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter.

flags
Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.

返回值

Returns a GMP number.

更新日志

版本 说明
8.0.0 This function no longer returns false on failure.

范例

示例 #1 gmp_import example

<?php
$number = gmp_import("\0");
echo gmp_strval($number) . "\n";

$number = gmp_import("\0\1\2");
echo gmp_strval($number) . "\n";
?>

以上例程会输出:

0
258

参见

  • gmp_export

gmp_init

Create GMP number

说明

GMP gmp_init ( <span class="type">intstring $num [, <span class="type">int $base = 0 ] )

Creates a GMP number from an integer or string.

参数

num
An integer or a string. The string representation can be decimal, hexadecimal or octal.

base
The base.

The base may vary from 2 to 62. If base is 0 (default value), the actual base is determined from the leading characters: if the first two characters are 0x or 0X, hexadecimal is assumed, if the first two characters are 0b or 0B, binary is assumed, otherwise if the first character is 0, octal is assumed, otherwise decimal is assumed. For bases up to 36, case is ignored; upper-case and lower-case letters have the same value. For bases 37 to 62, upper-case letter represent the usual 10 to 35 while lower-case letter represent 36 to 61.

返回值

GMP 对象。

范例

示例 #1 Creating GMP number

<?php
$a = gmp_init(123456);
$b = gmp_init("0xFFFFDEBACDFEDF7200");
?>

注释

Note:

It is not necessary to call this function in order to use integers or strings in place of GMP numbers in GMP functions (such as with <span class="function">gmp_add). Function arguments are automatically converted to GMP numbers, if such conversion is possible and needed, using the same rules as <span class="function">gmp_init.

gmp_intval

Convert GMP number to integer

说明

int <span class="methodname">gmp_intval ( <span class="type">GMP<span class="type">intstring $num )

This function converts GMP number into native PHP <span class="type">ints.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

The int value of num.

范例

示例 #1 gmp_intval example

<?php
// displays correct result
echo gmp_intval("2147483647") . "\n";

// displays wrong result, above PHP integer limit
echo gmp_intval("2147483648") . "\n";

// displays correct result
echo gmp_strval("2147483648") . "\n";
?>

以上例程会输出:

2147483647
2147483647
2147483648

注释

Warning

This function returns a useful result only if the number actually fits the PHP integer (i.e., signed long type). To simply print the GMP number, use gmp_strval.

gmp_invert

Inverse by modulo

说明

GMP<span class="type">false <span class="methodname">gmp_invert ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 )

Computes the inverse of num1 modulo num2.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

A GMP number on success or false if an inverse does not exist.

范例

示例 #1 gmp_invert example

<?php
echo gmp_invert("5", "10"); // no inverse, outputs nothing, result is FALSE
$invert = gmp_invert("5", "11");
echo gmp_strval($invert) . "\n";
?>

以上例程会输出:

9

gmp_jacobi

Jacobi symbol

说明

int <span class="methodname">gmp_jacobi ( <span class="type">GMP<span class="type">intstring $num1 , <span class="type">GMPint<span class="type">string $num2 )

Computes » Jacobi symbol of num1 and num2. num2 should be odd and must be positive.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

Should be odd and must be positive.

返回值

GMP 对象。

范例

示例 #1 gmp_jacobi example

<?php
echo gmp_jacobi("1", "3") . "\n";
echo gmp_jacobi("2", "3") . "\n";
?>

以上例程会输出:

1
0

参见

  • gmp_kronecker
  • gmp_legendre

gmp_kronecker

Kronecker symbol

说明

int <span class="methodname">gmp_kronecker ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

This function computes the Kronecker symbol of num1 and num2.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns the Kronecker symbol of num1 and num2

参见

  • gmp_jacobi
  • gmp_legendre

gmp_lcm

Calculate LCM

说明

GMP gmp_lcm ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

This function computes the least common multiple (lcm) of num1 and num2.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

参见

  • gmp_gcd

gmp_legendre

Legendre symbol

说明

int <span class="methodname">gmp_legendre ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Compute the »  Legendre symbol of num1 and num2. num2 should be odd and must be positive.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

Should be odd and must be positive.

返回值

GMP 对象。

范例

示例 #1 gmp_legendre example

<?php
echo gmp_legendre("1", "3") . "\n";
echo gmp_legendre("2", "3") . "\n";
?>

以上例程会输出:

1
0

参见

  • gmp_jacobi
  • gmp_kronecker

gmp_mod

Modulo operation

说明

GMP gmp_mod ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Calculates num1 modulo num2. The result is always non-negative, the sign of num2 is ignored.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The modulo that is being evaluated.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_mod example

<?php
$mod = gmp_mod("8", "3");
echo gmp_strval($mod) . "\n";
?>

以上例程会输出:

2

gmp_mul

Multiply numbers

说明

GMP gmp_mul ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Multiplies num1 by num2 and returns the result.

参数

num1
A number that will be multiplied by num2.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
A number that will be multiplied by num1.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_mul example

<?php
$mul = gmp_mul("12345678", "2000");
echo gmp_strval($mul) . "\n";
?>

以上例程会输出:

24691356000

gmp_neg

Negate number

说明

GMP gmp_neg ( <span class="type">GMPint<span class="type">string $num )

Returns the negative value of a number.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns -num, as a GMP number.

范例

示例 #1 gmp_neg example

<?php
$neg1 = gmp_neg("1");
echo gmp_strval($neg1) . "\n";
$neg2 = gmp_neg("-1");
echo gmp_strval($neg2) . "\n";
?>

以上例程会输出:

-1
1

gmp_nextprime

Find next prime number

说明

GMP <span class="methodname">gmp_nextprime ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num )

Find next prime number

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Return the next prime number greater than num, as a GMP number.

范例

示例 #1 gmp_nextprime example

<?php
$prime1 = gmp_nextprime(10); // next prime number greater than 10
$prime2 = gmp_nextprime(-1000); // next prime number greater than -1000

echo gmp_strval($prime1) . "\n";
echo gmp_strval($prime2) . "\n";
?>

以上例程会输出:

11
2

注释

Note:

This function uses a probabilistic algorithm to identify primes and chances to get a composite number are extremely small.

gmp_or

Bitwise OR

说明

GMP gmp_or ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Calculates bitwise inclusive OR of two GMP numbers.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_or example

<?php
$or1 = gmp_or("0xfffffff2", "4");
echo gmp_strval($or1, 16) . "\n";
$or2 = gmp_or("0xfffffff2", "2");
echo gmp_strval($or2, 16) . "\n";
?>

以上例程会输出:

fffffff6
fffffff2

gmp_perfect_power

Perfect power check

说明

bool <span class="methodname">gmp_perfect_power ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num )

Checks whether num is a perfect power.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns true if num is a perfect power, false otherwise.

参见

  • gmp_perfect_square

gmp_perfect_square

Perfect square check

说明

bool <span class="methodname">gmp_perfect_square ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num )

Check if a number is a perfect square.

参数

num
The number being checked as a perfect square.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns true if num is a perfect square, false otherwise.

范例

示例 #1 gmp_perfect_square example

<?php
// 3 * 3, perfect square
var_dump(gmp_perfect_square("9"));

// not a perfect square
var_dump(gmp_perfect_square("7"));

// 1234567890 * 1234567890, perfect square
var_dump(gmp_perfect_square("1524157875019052100"));
?>

以上例程会输出:

bool(true)
bool(false)
bool(true)

参见

  • gmp_perfect_power
  • gmp_sqrt
  • gmp_sqrtrem

gmp_popcount

Population count

说明

int <span class="methodname">gmp_popcount ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num )

Get the population count.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

The population count of num, as an int.

范例

示例 #1 gmp_popcount example

<?php
$pop1 = gmp_init("10000101", 2); // 3 1's
echo gmp_popcount($pop1) . "\n";
$pop2 = gmp_init("11111110", 2); // 7 1's
echo gmp_popcount($pop2) . "\n";
?>

以上例程会输出:

3
7

gmp_pow

Raise number into power

说明

GMP gmp_pow ( <span class="type">GMPint<span class="type">string $num , <span class="methodparam">int $exponent )

Raise num into power exponent.

参数

num
The base number.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

exponent
The positive power to raise the num.

返回值

The new (raised) number, as a GMP number. The case of 0^0 yields 1.

范例

示例 #1 gmp_pow example

<?php
$pow1 = gmp_pow("2", 31);
echo gmp_strval($pow1) . "\n";
$pow2 = gmp_pow("0", 0);
echo gmp_strval($pow2) . "\n";
$pow3 = gmp_pow("2", -1); // Negative exp, generates warning
echo gmp_strval($pow3) . "\n";
?>

以上例程会输出:

2147483648
1

gmp_powm

Raise number into power with modulo

说明

GMP gmp_powm ( <span class="type">GMPint<span class="type">string $num , <span class="methodparam"><span class="type">GMPint<span class="type">string $exponent , <span class="methodparam"><span class="type">GMPint<span class="type">string $modulus )

Calculate (num raised into power exponent) modulo modulus. If exponent is negative, result is undefined.

参数

num
The base number.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

exponent
The positive power to raise the num.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

modulus
The modulo.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

The new (raised) number, as a GMP number.

范例

示例 #1 gmp_powm example

<?php
$pow1 = gmp_powm("2", "31", "2147483649");
echo gmp_strval($pow1) . "\n";
?>

以上例程会输出:

2147483648

gmp_prob_prime

Check if number is "probably prime"

说明

int <span class="methodname">gmp_prob_prime ( <span class="methodparam"><span class="type">GMPint<span class="type">string $num [, <span class="methodparam">int $repetitions<span class="initializer"> = 10 ] )

The function uses Miller-Rabin's probabilistic test to check if a number is a prime.

参数

num
The number being checked as a prime.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

repetitions
Reasonable values of repetitions vary from 5 to 10 (default being 10); a higher value lowers the probability for a non-prime to pass as a "probable" prime.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

If this function returns 0, num is definitely not prime. If it returns 1, then num is "probably" prime. If it returns 2, then num is surely prime.

范例

示例 #1 gmp_prob_prime example

<?php
// definitely not a prime
echo gmp_prob_prime("6") . "\n";

// probably a prime
echo gmp_prob_prime("1111111111111111111") . "\n";

// definitely a prime
echo gmp_prob_prime("11") . "\n";
?>

以上例程会输出:

0
1
2

gmp_random_bits

Random number

说明

GMP <span class="methodname">gmp_random_bits ( <span class="methodparam">int $bits )

Generate a random number. The number will be between 0 and (2 ** bits) - 1.

bits must greater than 0, and the maximum value is restricted by available memory.

参数

bits
The number of bits.

返回值

A random GMP number.

范例

示例 #1 gmp_random_bits example

<?php
$rand1 = gmp_random_bits(3); // random number from 0 to 7
$rand2 = gmp_random_bits(5); // random number from 0 to 31

echo gmp_strval($rand1) . "\n";
echo gmp_strval($rand2) . "\n";
?>

以上例程会输出:

3
15

gmp_random_range

Random number

说明

GMP <span class="methodname">gmp_random_range ( <span class="methodparam"><span class="type">GMPint<span class="type">string $min , <span class="methodparam"><span class="type">GMPint<span class="type">string $max )

Generate a random number. The number will be between min and max.

min and max can both be negative but min must always be less than max.

参数

min
A GMP number representing the lower bound for the random number

max
A GMP number representing the upper bound for the random number

返回值

A random GMP number.

范例

示例 #1 gmp_random_range example

<?php
$rand1 = gmp_random_range(0, 100);    // random number between 0 and 100
$rand2 = gmp_random_range(-100, -10); // random number between -100 and -10

echo gmp_strval($rand1) . "\n";
echo gmp_strval($rand2) . "\n";
?>

以上例程会输出:

42
-67

gmp_random_seed

Sets the RNG seed

说明

void <span class="methodname">gmp_random_seed ( <span class="methodparam"><span class="type">GMPint<span class="type">string $seed )

参数

seed
The seed to be set for the gmp_random, gmp_random_bits, and <span class="function">gmp_random_range functions.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

成功时返回 null, 或者在失败时返回 false

错误/异常

Issues an E_WARNING and returns false if seed is not valid.

范例

示例 #1 gmp_random_seed example

<?php
// set the seed
gmp_random_seed(100);

var_dump(gmp_strval(gmp_random(1)));

// set the seed to something else
gmp_random_seed(gmp_init(-100));

var_dump(gmp_strval(gmp_random_bits(10)));

// set the seed to something invalid
var_dump(gmp_random_seed('not a number'));

以上例程会输出:

string(20) "15370156633245019617"
string(3) "683"

Warning: gmp_random_seed(): Unable to convert variable to GMP - string is not an integer in %s on line %d
bool(false)

参见

  • gmp_init
  • gmp_random
  • gmp_random_bits
  • gmp_random_range

gmp_random

Random number

Warning

本函数已自 PHP 7.2.0 起废弃。强烈建议不要使用本函数。

说明

GMP <span class="methodname">gmp_random ([ <span class="methodparam">int $limiter<span class="initializer"> = 20 ] )

Generate a random number. The number will be between 0 and (2 ** n) - 1, where n is the number of bits per limb multiplied by limiter. If limiter is negative, negative numbers are generated.

A limb is an internal GMP mechanism. The number of bits in a limb is not static, and can vary from system to system. Generally, the number of bits in a limb is either 32 or 64, but this is not guaranteed.

参数

limiter
The limiter.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

A random GMP number.

范例

示例 #1 gmp_random example

<?php
$rand1 = gmp_random(1); // random number from 0 to 1 * bits per limb
$rand2 = gmp_random(2); // random number from 0 to 2 * bits per limb

echo gmp_strval($rand1) . "\n";
echo gmp_strval($rand2) . "\n";
?>

以上例程会输出:

1915834968
8642564075890328087

gmp_root

Take the integer part of nth root

说明

GMP gmp_root ( <span class="type">GMPint<span class="type">string $num , <span class="methodparam">int $nth )

Takes the nth root of num and returns the integer component of the result.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

nth
The positive root to take of num.

返回值

The integer component of the resultant root, as a GMP number.

gmp_rootrem

Take the integer part and remainder of nth root

说明

array <span class="methodname">gmp_rootrem ( <span class="type">GMP<span class="type">intstring $num , int $nth )

Takes the nth root of num and returns the integer component and remainder of the result.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

nth
The positive root to take of num.

返回值

A two element array, where the first element is the integer component of the root, and the second element is the remainder, both represented as GMP numbers.

gmp_scan0

Scan for 0

说明

int gmp_scan0 ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam">int $start )

Scans num1, starting with bit start, towards more significant bits, until the first clear bit is found.

参数

num1
The number to scan.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

start
The starting bit.

返回值

Returns the index of the found bit, as an int. The index starts from 0.

范例

示例 #1 gmp_scan0 example

<?php
// "0" bit is found at position 3. index starts at 0
$s1 = gmp_init("10111", 2);
echo gmp_scan0($s1, 0) . "\n";

// "0" bit is found at position 7. index starts at 5
$s2 = gmp_init("101110000", 2);
echo gmp_scan0($s2, 5) . "\n";
?>

以上例程会输出:

3
7

gmp_scan1

Scan for 1

说明

int gmp_scan1 ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam">int $start )

Scans num1, starting with bit start, towards more significant bits, until the first set bit is found.

参数

num1
The number to scan.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

start
The starting bit.

返回值

Returns the index of the found bit, as an int. If no set bit is found, -1 is returned.

范例

示例 #1 gmp_scan1 example

<?php
// "1" bit is found at position 3. index starts at 0
$s1 = gmp_init("01000", 2);
echo gmp_scan1($s1, 0) . "\n";

// "1" bit is found at position 9. index starts at 5
$s2 = gmp_init("01000001111", 2);
echo gmp_scan1($s2, 5) . "\n";
?>

以上例程会输出:

3
9

gmp_setbit

Set bit

说明

void <span class="methodname">gmp_setbit ( <span class="type">GMP $num , <span class="type">int $index [, <span class="methodparam">bool $value<span class="initializer"> = true ] )

Sets bit index in num.

参数

num
The value to modify.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

index
The index of the bit to set. Index 0 represents the least significant bit.

value
True to set the bit (set it to 1/on); false to clear the bit (set it to 0/off).

返回值

GMP 对象。

范例

示例 #1 gmp_setbit example - 0 index

<?php
$a = gmp_init("2"); //
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
gmp_setbit($a, 0); // 0b10 now becomes 0b11
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
?>

以上例程会输出:

2 -> 0b10
3 -> 0b11

示例 #2 gmp_setbit example - 1 index

<?php
$a = gmp_init("0xfd");
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
gmp_setbit($a, 1); // index starts at 0
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
?>

以上例程会输出:

253 -> 0b11111101
255 -> 0b11111111

示例 #3 gmp_setbit example - clearing a bit

<?php
$a = gmp_init("0xff");
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
gmp_setbit($a, 0, false); // clear bit at index 0
echo gmp_strval($a), ' -> 0b', gmp_strval($a, 2), "\n";
?>

以上例程会输出:

255 -> 0b11111111
254 -> 0b11111110

注释

Note:

Unlike most of the other GMP functions, <span class="function">gmp_setbit must be called with a GMP object that already exists (using gmp_init for example). One will not be automatically created.

参见

  • gmp_clrbit
  • gmp_testbit

gmp_sign

Sign of number

说明

int gmp_sign ( <span class="type">GMPint<span class="type">string $num )

Checks the sign of a number.

参数

num
Either a GMP number resource in PHP 5.5 and earlier, a GMP object in PHP 5.6 and later, or a numeric string provided that it is possible to convert the latter to an int.

返回值

Returns 1 if num is positive, -1 if num is negative, and 0 if num is zero.

范例

示例 #1 gmp_sign example

<?php
// positive
echo gmp_sign("500") . "\n";

// negative
echo gmp_sign("-500") . "\n";

// zero
echo gmp_sign("0") . "\n";
?>

以上例程会输出:

1
-1
0

参见

  • gmp_abs
  • abs

gmp_sqrt

Calculate square root

说明

GMP gmp_sqrt ( <span class="type">GMPint<span class="type">string $num )

Calculates square root of num.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

The integer portion of the square root, as a GMP number.

范例

示例 #1 gmp_sqrt example

<?php
$sqrt1 = gmp_sqrt("9");
$sqrt2 = gmp_sqrt("7");
$sqrt3 = gmp_sqrt("1524157875019052100");

echo gmp_strval($sqrt1) . "\n";
echo gmp_strval($sqrt2) . "\n";
echo gmp_strval($sqrt3) . "\n";
?>

以上例程会输出:

3
2
1234567890

gmp_sqrtrem

Square root with remainder

说明

array <span class="methodname">gmp_sqrtrem ( <span class="type">GMP<span class="type">intstring $num )

Calculate the square root of a number, with remainder.

参数

num
The number being square rooted.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

Returns array where first element is the integer square root of num and the second is the remainder (i.e., the difference between num and the first element squared).

范例

示例 #1 gmp_sqrtrem example

<?php
list($sqrt1, $sqrt1rem) = gmp_sqrtrem("9");
list($sqrt2, $sqrt2rem) = gmp_sqrtrem("7");
list($sqrt3, $sqrt3rem) = gmp_sqrtrem("1048576");

echo gmp_strval($sqrt1) . ", " . gmp_strval($sqrt1rem) . "\n";
echo gmp_strval($sqrt2) . ", " . gmp_strval($sqrt2rem) . "\n";
echo gmp_strval($sqrt3) . ", " . gmp_strval($sqrt3rem) . "\n";
?>

以上例程会输出:

3, 0
2, 3
1024, 0

gmp_strval

Convert GMP number to string

说明

string <span class="methodname">gmp_strval ( <span class="type">GMP<span class="type">intstring $num [, <span class="type">int $base = 10 ] )

Convert GMP number to string representation in base base. The default base is 10.

参数

num
The GMP number that will be converted to a string.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

base
The base of the returned number. The default base is 10. Allowed values for the base are from 2 to 62 and -2 to -36.

返回值

The number, as a string.

范例

示例 #1 Converting a GMP number to a string

<?php
$a = gmp_init("0x41682179fbf5");
printf("Decimal: %s, 36-based: %s", gmp_strval($a), gmp_strval($a,36));
?>

gmp_sub

Subtract numbers

说明

GMP gmp_sub ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Subtracts num2 from num1 and returns the result.

参数

num1
The number being subtracted from.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
The number subtracted from num1.

PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_sub example

<?php
$sub = gmp_sub("281474976710656", "4294967296"); // 2^48 - 2^32
echo gmp_strval($sub) . "\n";
?>

以上例程会输出:

281470681743360

gmp_testbit

Tests if a bit is set

说明

bool <span class="methodname">gmp_testbit ( <span class="type">GMP<span class="type">intstring $num , int $index )

Tests if the specified bit is set.

参数

num
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

index
The bit to test

返回值

Returns true if the bit is set in resource $a, otherwise false.

错误/异常

An E_WARNING level error is issued when index is less than zero, and false is returned.

范例

示例 #1 gmp_testbit example

<?php
$n = gmp_init("1000000");
var_dump(gmp_testbit($n, 1));
gmp_setbit($n, 1);
var_dump(gmp_testbit($n, 1));
?>

以上例程会输出:

bool(false)
bool(true)

参见

  • gmp_setbit
  • gmp_clrbit

gmp_xor

Bitwise XOR

说明

GMP gmp_xor ( <span class="type">GMPint<span class="type">string $num1 , <span class="methodparam"><span class="type">GMPint<span class="type">string $num2 )

Calculates bitwise exclusive OR (XOR) of two GMP numbers.

参数

num1
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

num2
PHP 5.5 之前为 GMP 数值资源,PHP 5.6 之后为 GMP 对象或数字,或可以转为数字的字符串。

返回值

GMP 对象。

范例

示例 #1 gmp_xor example

<?php
$xor1 = gmp_init("1101101110011101", 2);
$xor2 = gmp_init("0110011001011001", 2);

$xor3 = gmp_xor($xor1, $xor2);

echo gmp_strval($xor3, 2) . "\n";
?>

以上例程会输出:

1011110111000100

目录


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除