This is the 26th day of my participation in the August Text Challenge.More challenges in August

For all development languages, integers have a maximum number of digits beyond which they cannot be displayed or manipulated. In fact, this is also a kind of precision out of bounds after the loss of precision. In our PHP code, the largest integer is very large, which we can see by PHP_INT_MAX. However, when integers exceed a certain number of digits, scientific notation is used to show that this is not what we want. Don’t worry, the GMP extension is specifically designed to deal with this situation.

The GMP extension is shipped with the PHP source package, so you need to install gmp-devel on CentOS before installing the extension.

The loss of accuracy of large numbers

Let’s take a look at what happens when you print out very large numbers directly.

echo PHP_INT_MAX; / / 92233720368547758071.2312312312312 e+26

$a = 123123123123123123123123123;
echo $a, PHP_EOL; / / 1.2312312312312 e+26
echo $a + 1, PHP_EOL; / / 1.2312312312312 e+26
Copy the code

As you can see, it’s all in the form of scientific notation. And for simple operations, the difference is almost invisible. As in the case of $a + 1 at the end, it shows the same result as the original data.

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

When we use the GMP extension, we can instantiate such large numbers using gmp_init(). The printed results are still in standard numeric format. However, it is important to note that this extension actually converts the very large number we are manipulating into a string representation.

Gmp_add () is the GMP addition function, which simply adds two arguments and returns a GMP object.

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

You can see this by printing the $b object returned by gmp_init(). It’s actually a string. Also, this object overrides the __toString() method, so we can echo it directly. In addition, GMP objects overload the operators, so there is no problem doing routine operator operations directly on GMP objects.

Simple operation

In addition to overloaded operators, the GMP extension also provides a series of operand functions, as we have already seen with 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
Copy the code

These four are respectively the calculation of subtraction, multiplication, division and remainder. It’s very simple and I won’t go into it here. One thing to note here is that they can take arguments of type int or string. Just like the argument gmp_init() receives.

echo gmp_abs("123123123123123123123123123"), PHP_EOL; / / 123123123123123123123123123
echo gmp_pow($b.3), PHP_EOL; / / 1866460784838622135378351047886265184644645186267890058355382138624840786461867
echo gmp_sqrt($b), PHP_EOL; / / 11096085937082
Copy the code

These three functions are computation functions that take absolute value, power, and quadratic root respectively. This is similar to the normal Math calculation function.

Bit operation

The GMP extension also makes it easy to do bitwise as well as binary manipulation of data. Such as and, or, xOR in bitwise operations.

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

You can also export a number in binary format.

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

Of course, there are corresponding functions imported from binary, which we won’t demonstrate here. You can find the corresponding function test in the documentation to understand.

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

The gmp_popcount() function is used to get the number of 1’s in the binary representation of a character. Such as 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
Copy the code

The gmp_scan0() and gmp_scan1() functions find the location of the first 0 or 1 to occur, respectively. Its second argument indicates where to start the search. In addition, they all start from right to left, and they all start at subscript 0.

Other operations

Random number generation

echo gmp_random_range("10000000000000"."99999999999999999"), PHP_EOL; / / 83490559526159213
/ / 12500000000
echo gmp_random_bits(99999),PHP_EOL; / / 289814632948807684404778811091812938699609...........................
Copy the code

Just like normal random number generation functions, except the two functions below the GMP extension library can generate a larger range of numbers and return the GMP object format. For gmp_random_bits(), the maximum range is 12500000000, and my machine would run out of memory if I used this random factor. The number of random numbers generated using the random factor 99999 is already very large, so you can try it out for yourself.

factorial

This is a function not found in the normal Math library. You can calculate the factorial for us instead of writing your own algorithm.

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
Copy the code

A prime number

In addition to factorial, GMP also provides very sophisticated functions for directly obtaining and judging prime numbers. Generally speaking, prime numbers (prime) is also a very common algorithm in the interview questions, we still have to master their handwriting ability in the interview, but after handwriting can say and the interviewer GMP has a ready-made function I believe will also 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
Copy the code

Gmp_nextprime () is the nextprime after getting the specified number. Gmp_prob_prime () is used to determine whether the written number is prime. It has three results: 0 indicates that the number is not prime, 1 indicates that it is possible (suspected), and 2 indicates that it is definitely prime.

Symbolic information for data

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

Finally, the gmp_sign() function is used to represent symbolic information for the given data, positive and negative. It’s also three outcomes, 1 for a positive number, -1 for a negative number, and 0 for 0. Why is there a special zero? Because 0 is neither positive nor negative, it is a special existence in its own right.

conclusion

There are many methods of GMP extension that have not been listed one by one, but some of the more commonly used ones have been selected here. Although it is a document, but can not directly copy the document, so we still have to refer to the document, the main purpose of our study is to know that there is such a thing, so that we do not lose touch with the relevant content in the real business requirements.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/b…

= = = = = = = = = = = = = = =

Follow the public account: [Hardcore project manager] to get the latest articles

Add WeChat/QQ friends: free xiaoyuezigonggong / 149844827 】 【 PHP, project management, learning materials

Zhihu, Public Account, Douyin, Toutiao Search [Hardcore Project Manager]

ID of station B: 482780532