Home  >  Article  >  Backend Development  >  Long integer operations in PHP

Long integer operations in PHP

不言
不言Original
2018-04-13 10:55:551400browse

The content shared with you in this article is about the operations of long integers in PHP. It has certain reference value. Friends in need can refer to it

In PHP, there are only two types of numbers: int and float. Their number of digits depends on the system, and there is no uint, so there is a lot of inconvenience when communicating with other systems. If int overflows, it will be automatically converted to float, expressed in scientific notation, and only 15 digits will be retained after the decimal point. The subsequent data will be rounded, which will have an impact on the subsequent data operations, For example, When it comes to base conversion, operations, etc., errors will occur.

For PHP:

int : min~max: -9223372036854775808 ~ 9223372036854775807 (~92 billion)

float: min~max: -1.7976931348623E 308 ~ 1.7976931348623E 308 (~1.8E308)


At this time we need to consider using string form for related processing. If we must perform mathematical calculations, we must use BC Math (arbitrary precision mathematics) Functions, You can check it yourself in the PHP manual: http://php.net/manual/zh/function.bcmod.php

Here is a simple example of converting a long integer into binary, converting 2^128 into binary: Since PHP's base conversion function also has a maximum value limit, short division is used here to convert into binary. Use the original function pow to find the exponentiation


The output result is as follows. Obviously the accuracy is not enough and the binary is wrong

## Correct representation:


## Output result: Related recommendations:

PDO data access abstraction in PHP Layer operations



The above is the detailed content of Long integer operations in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn