参见

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

gmp_abs

Absolute value

说明

GMP gmp_abs ( GMP $a )

Get the absolute value of a number.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns the absolute value of a, 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 ( GMP $a , GMP $b )

Add two numbers.

参数

a
The first summand (augent).

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
The second summand (addend).

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 ( GMP $a , GMP $b )

Calculates bitwise AND of two GMP numbers.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_binomial ( mixed $n , int $k )

Calculates the binomial coefficient C(n, k).

参数

n
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

k

返回值

Returns the binomial coefficient C(n, k), 或者在失败时返回 FALSE.

错误/异常

Issues E_WARNING if k is negative.

gmp_clrbit

Clear bit

说明

void gmp_clrbit ( GMP $a , int $index )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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, class="function">gmp_clrbit must be called with a GMP resource 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 ( GMP $a , GMP $b )

Compares two numbers.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 ( GMP $a )

Returns the one's complement of a.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns the one's complement of a, 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 gmp_div_q ( GMP $a , GMP $b [, int $round = GMP_ROUND_ZERO ] )

Divides a by b and returns the integer result.

参数

a
The number being divided.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
The number that a is being divided by.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

round
The result rounding is defined by the round, 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.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 class="function">gmp_div.

参见

  • gmp_div_r
  • gmp_div_qr

gmp_div_qr

Divide numbers and get quotient and remainder

说明

array gmp_div_qr ( GMP $n , GMP $d [, int $round = GMP_ROUND_ZERO ] )

The function divides n by d.

参数

n
The number being divided.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

d
The number that n is being divided by.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

round
See the gmp_div_q function for description of the round 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 gmp_div_r ( GMP $n , GMP $d [, int $round = GMP_ROUND_ZERO ] )

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

参数

n
The number being divided.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

d
The number that n is being divided by.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

round
See the gmp_div_q function for description of the round 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 gmp_divexact ( GMP $n , GMP $d )

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

参数

n
The number being divided.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

d
The number that a is being divided by.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_export ( GMP $gmpnumber [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] )

Export a GMP number to a binary string

参数

gmpnumber
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.

options
Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.

返回值

Returns a string 或者在失败时返回 FALSE.

范例

示例 #1 gmp_export example

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

以上例程会输出:

AA

参见

  • gmp_import

gmp_fact

Factorial

说明

GMP gmp_fact ( mixed $a )

Calculates factorial (a!) of a.

参数

a
The factorial number.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 ( GMP $a , GMP $b )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

A positive GMP number that divides into both a and b.

范例

示例 #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 gmp_gcdext ( GMP $a , GMP $b )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_hamdist ( GMP $a , GMP $b )

Returns the hamming distance between a and b. Both operands should be non-negative.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

It should be positive.

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

It should be positive.

返回值

GMP 数值资源.

范例

示例 #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 gmp_import ( string $data [, int $word_size = 1 [, int $options = 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.

options
Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.

返回值

Returns a GMP number 或者在失败时返回 FALSE.

范例

示例 #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 ( mixed $number [, int $base = 0 ] )

Creates a GMP number from an integer or string.

参数

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

base
The base.

The base may vary from 2 to 36. 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, otherwise if the first character is "0", octal is assumed, otherwise decimal is assumed.

返回值

GMP 数值资源.

更新日志

版本说明
5.3.2The base was extended from 2 to 36, to 2 to 62 and -2 to -36.

注释

Note:

To use the extended base introduced in PHP 5.3.2, then PHP must be compiled against GMP 4.2.0 or greater.

范例

示例 #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 class="function">gmp_add). Function arguments are automatically converted to GMP numbers, if such conversion is possible and needed, using the same rules as class="function">gmp_init.

gmp_intval

Convert GMP number to integer

说明

int gmp_intval ( GMP $gmpnumber )

This function converts GMP number into native PHP integers.

参数

gmpnumber
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

The integer value of gmpnumber.

范例

示例 #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 gmp_invert ( GMP $a , GMP $b )

Computes the inverse of a modulo b.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_jacobi ( GMP $a , GMP $p )

Computes » Jacobi symbol of a and p. p should be odd and must be positive.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

p
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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 gmp_kronecker ( mixed $a , mixed $b )

This function computes the Kronecker symbol of a and b.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns the Kronecker symbol of a and b

参见

  • gmp_jacobi
  • gmp_legendre

gmp_lcm

Calculate LCM

说明

GMP gmp_lcm ( mixed $a , mixed $b )

This function computes the least common multiple (lcm) of a and b.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

GMP 数值资源.

参见

  • gmp_gcd

gmp_legendre

Legendre symbol

说明

int gmp_legendre ( GMP $a , GMP $p )

Compute the »  Legendre symbol of a and p. p should be odd and must be positive.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

p
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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 ( GMP $n , GMP $d )

Calculates n modulo d. The result is always non-negative, the sign of d is ignored.

参数

n
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

d
The modulo that is being evaluated.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

GMP 数值资源.

范例

示例 #1 gmp_mod example

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

以上例程会输出:

2

gmp_mul

Multiply numbers

说明

GMP gmp_mul ( GMP $a , GMP $b )

Multiplies a by b and returns the result.

参数

a
A number that will be multiplied by b.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
A number that will be multiplied by a.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

GMP 数值资源.

范例

示例 #1 gmp_mul example

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

以上例程会输出:

24691356000

gmp_neg

Negate number

说明

GMP gmp_neg ( GMP $a )

Returns the negative value of a number.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns -a, 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 gmp_nextprime ( int $a )

Find next prime number

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Return the next prime number greater than a, 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 ( GMP $a , GMP $b )

Calculates bitwise inclusive OR of two GMP numbers.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_perfect_power ( mixed $a )

Checks whether a is a perfect power.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns TRUE if a is a perfect power, FALSE otherwise.

参见

  • gmp_perfect_square

gmp_perfect_square

Perfect square check

说明

bool gmp_perfect_square ( GMP $a )

Check if a number is a perfect square.

参数

a
The number being checked as a perfect square.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns TRUE if a 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 gmp_popcount ( GMP $a )

Get the population count.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

The population count of a, as an integer.

范例

示例 #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 ( GMP $base , int $exp )

Raise base into power exp.

参数

base
The base number.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

exp
The positive power to raise the base.

返回值

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 ( GMP $base , GMP $exp , GMP $mod )

Calculate (base raised into power exp) modulo mod. If exp is negative, result is undefined.

参数

base
The base number.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

exp
The positive power to raise the base.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

mod
The modulo.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_prob_prime ( GMP $a [, int $reps = 10 ] )

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

参数

a
The number being checked as a prime.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

If this function returns 0, a is definitely not prime. If it returns 1, then a is "probably" prime. If it returns 2, then a 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 gmp_random_bits ( 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.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_random_range ( GMP $min , GMP $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 gmp_random_seed ( mixed $seed )

参数

seed
The seed to be set for the gmp_random, gmp_random_bits, and gmp_random_range functions.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns NULL on success 或者在失败时返回 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

This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

说明

GMP gmp_random ([ int $limiter = 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.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 ( GMP $a , int $nth )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

nth
The positive root to take of a.

返回值

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

gmp_rootrem

Take the integer part and remainder of nth root

说明

array gmp_rootrem ( GMP $a , int $nth )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

nth
The positive root to take of a.

返回值

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 ( GMP $a , int $start )

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

参数

a
The number to scan.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

start
The starting bit.

返回值

Returns the index of the found bit, as an integer. 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 ( GMP $a , int $start )

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

参数

a
The number to scan.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

start
The starting bit.

返回值

Returns the index of the found bit, as an integer. 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 gmp_setbit ( GMP $a , int $index [, bool $bit_on = TRUE ] )

Sets bit index in a.

参数

a
The value to modify.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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

bit_on
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, class="function">gmp_setbit must be called with a GMP resource 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 ( GMP $a )

Checks the sign of a number.

参数

a
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 integer.

返回值

Returns 1 if a is positive, -1 if a is negative, and 0 if a 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 ( GMP $a )

Calculates square root of a.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_sqrtrem ( GMP $a )

Calculate the square root of a number, with remainder.

参数

a
The number being square rooted.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

Returns array where first element is the integer square root of a and the second is the remainder (i.e., the difference between a 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 gmp_strval ( GMP $gmpnumber [, int $base = 10 ] )

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

参数

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

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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.

更新日志

版本说明
5.3.2The base was extended from 2 to 36, to 2 to 62 and -2 to -36.

注释

Note:

To use the extended base introduced in PHP 5.3.2, then PHP must be compiled against GMP 4.2.0 or greater.

范例

示例 #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 ( GMP $a , GMP $b )

Subtracts b from a and returns the result.

参数

a
The number being subtracted from.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
The number subtracted from a.

可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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 gmp_testbit ( GMP $a , int $index )

Tests if the specified bit is set.

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

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 ( GMP $a , GMP $b )

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

参数

a
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

b
可以是一个 GMP 数据 resouce,或一个可以转换为数值的字符串。

返回值

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

目录