1

For all kinds of development languages, integers have a maximum number of digits. If it exceeds the number of digits, it cannot be displayed or manipulated. In fact, this is also a problem of loss of accuracy that occurs after the accuracy is out of bounds. In our PHP code, the largest integer is very large, and we can check it through PHP_INT_MAX. However, when the integer exceeds a certain number of digits, it will be displayed using scientific notation, which is not the result we want. Don't worry, the GMP extension is specifically designed to deal with this situation.

The GMP extension is released with the PHP source code package. Before installing the extension, you need to install gmp-devel in the system environment. You can install gmp-devel directly in CentOS.

Loss of precision of large numbers

Let's first take a look at what happens when you print out a very large number directly.

echo PHP_INT_MAX; // 92233720368547758071.2312312312312E+26

$a = 123123123123123123123123123;
echo $a, PHP_EOL; // 1.2312312312312E+26
echo $a + 1, PHP_EOL; // 1.2312312312312E+26

As you can see, the displayed results are all in the form of scientific notation. And for simple arithmetic operations, there is basically no difference. Just like the case we gave $a + 1 at the end, it is the same as the result displayed by the original data.

$b = gmp_init("123123123123123123123123123");
echo $b, PHP_EOL; // 123123123123123123123123123
echo gmp_add($b, 1), PHP_EOL; // 123123123123123123123123124

When we use GMP extension, we can use gmp_init() to instantiate such a large number. The printed result is still in standard digital format. However, it should be noted here that this extension actually converts the super large number we want to operate into a string to represent it.

gmp_add() is the addition operation function of GMP, it is very simple, it is to add two parameters, and then the return is still a GMP object.

var_dump($b);
// object(GMP)#1 (1) {
//     ["num"]=>
//     string(27) "123123123123123123123123123"
//   }
echo $b + 1, PHP_EOL; // 123123123123123123123123124

It can be seen by printing the $b object returned by gmp_init(). The content in it is actually a string. At the same time, this object also rewrites the \_\_toString() method, so we can directly echo it. In addition, the GMP object also overloads operation operators, so it is not a problem to directly perform daily operator operations on the GMP object.

Simple operation

In addition to the overloaded operators, the GMP extension also provides a series of arithmetic operations, just like we have seen gmp_add() above.

echo gmp_sub($b, 1), PHP_EOL; // 123123123123123123123123122
echo gmp_mul($b, 2), PHP_EOL; // 246246246246246246246246246
echo gmp_div("123123123123123123123123123", 3), PHP_EOL; // 41041041041041041041041041
echo gmp_mod($b, 5), PHP_EOL; // 3

These four are the calculation of subtraction, multiplication, division, and remainder. It's very simple, so I won't say much here. One thing to note here is that the parameters they receive can be of type int or string. It is the same as the parameters received by gmp_init().

echo gmp_abs("-123123123123123123123123123"), PHP_EOL; // 123123123123123123123123123
echo gmp_pow($b, 3), PHP_EOL; // 1866460784838622135378351047886265184644645186267890058355382138624840786461867
echo gmp_sqrt($b), PHP_EOL; // 11096085937082

These three functions are calculation functions that take absolute value, power, and square root. It is similar to ordinary Math calculation functions.

Bit manipulation

The GMP extension can also conveniently perform bit and binary operations on data. For example, AND, OR, XOR in bit operation.

echo gmp_and($b, "2222222222"), PHP_EOL; // 2151965570
echo gmp_or($b, "2222222222"), PHP_EOL; // 123123123123123123193379775
echo gmp_xor($b, "3333333333"), PHP_EOL; // 123123123123123120012088038

You can also convert a number into a binary format and export it.

echo gmp_export($b), PHP_EOL; // e�U��(c�O�

Of course, there are also corresponding functions imported from binary, we won't demonstrate it here. You can find the corresponding function test in the document yourself.

$pop1 = gmp_init("10000101", 2); // 3
echo gmp_popcount($pop1), PHP_EOL;
$pop2 = gmp_init("11111110", 2); // 7
echo gmp_popcount($pop2), PHP_EOL;

The gmp_popcount() function is used to obtain the number of 1s in the binary representation of the character. For example, the result returned in this test code.

$s1 = gmp_init("10111", 2);
echo gmp_scan0($s1, 0), PHP_EOL; // 3

$s2 = gmp_init("101110000", 2);
echo gmp_scan0($s2, 0), PHP_EOL; // 0

$s1 = gmp_init("10111", 2);
echo gmp_scan1($s1, 0), PHP_EOL; // 0

$s2 = gmp_init("101110000", 2);
echo gmp_scan1($s2, 0), PHP_EOL; // 4

The gmp_scan0() and gmp_scan1() functions look for the position of the first 0 or 1, respectively. Its second parameter is to specify where to start the search. In addition, their search directions start from right to left, and start from the position of subscript 0.

Other operations

Generate random numbers

echo gmp_random_range("10000000000000", "99999999999999999"), PHP_EOL; // 83490559526159213
// 12500000000
echo gmp_random_bits(99999),PHP_EOL; // 289814632948807684404778811091812938699609………………

It is the same as the ordinary random number generating function, except that the two functions under the GMP extension library can generate a larger range of numbers and return the format of the GMP object. For gmp_random_bits(), the maximum range is 12500000000. If my machine uses this random factor, it will directly report out of memory. And the random number generated using the random factor of 99999 is already very large, you can try it yourself.

factorial

This is a function not found in the ordinary Math library. Directly help us calculate the result of the factorial, no need to write the algorithm yourself.

echo gmp_fact(5), PHP_EOL; // 120    5*4*3*2*1=120
echo gmp_fact(50), PHP_EOL; // 30414093201713378043612608166064768844377641568960512000000000000 50*49*48…………*2*1

Prime number

In addition to factorial, GMP also provides a very tall function for directly obtaining and judging prime numbers. Generally speaking, prime numbers (prime numbers) are also very common algorithmic questions in interviews. We still have to master our handwriting ability during the interview, but after handwriting, we can tell the interviewer that there are ready-made functions in GMP. Will bring some extra points.

echo gmp_nextprime(10), PHP_EOL; // 11
echo gmp_nextprime(1000), PHP_EOL; // 1009
echo gmp_prob_prime(6), PHP_EOL; // 0
echo gmp_prob_prime("1111111111111111111"), PHP_EOL; // 1
echo gmp_prob_prime(7), PHP_EOL; // 2

gmp_nextprime() is to get the next prime number after the specified number. gmp_prob_prime() is to judge whether the number written is a prime number. It has three results, 0 means it is not a prime number, 1 means a possible (suspected) prime number, and 2 means it is definitely a prime number.

Symbolic information of data

echo gmp_sign("500"), PHP_EOL; // 1
echo gmp_sign("-500"), PHP_EOL; // -1
echo gmp_sign("0"), PHP_EOL; // 0

Finally, the gmp_sign() function is used to represent the sign information of the given data, that is, positive and negative numbers. It is also three kinds of results, 1 means positive number, -1 means negative number, and 0 means 0. Why is there a special 0? Because 0 is neither positive nor negative, it is a special existence in itself.

Summarize

There are still many methods for GMP extension that have not been listed one by one. Here I just selected some of the more commonly used content to introduce to you. Although it is to scan the document, it is not possible to copy the document directly, so you should go to the document for more information. The main purpose of our study is to know that there is such a thing, so that it will not be related to the real business needs. Catch up on the content.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202012/source/8. GMP extended learning for operating arbitrary precision in

Reference documents:

https://www.php.net/manual/zh/book.gmp.php

===========

All media platforms can be searched [hardcore project manager]


硬核项目经理
90 声望18 粉丝