# PHP floating point precision loss solution

Source: Internet
Author: User
Tags reserved sprintf

Let's take a look at the following code:

\$ F = 0.57;
Echo intval (\$ f * 100); // 56

The result may be a little unexpected. PHP follows IEEE 754 dual precision:
Floating point number, expressed in 64-bit dual precision, with one-bit sign bit (E), 11 exponent bit (Q), and 52-bit ending number (M) (64-bit in total ).
Symbol bit: the highest bit indicates positive and negative data, 0 indicates positive data, and 1 indicates negative data.
Exponent bit: the power of data at the bottom of 2, and the exponent is represented by an offset code.
Ending number: a valid number after the decimal point of the data.

Let's take a look at how decimal points are represented in binary:

Multiply the decimal part by 2, then take the integer part, multiply the remaining decimal part by 2, and then take the integer part, and multiply the remaining decimal part by 2, the fractional part is always obtained, but the decimal part like 0.57 is always multiplied, and the decimal part cannot be 0. the decimals of valid bits are expressed in binary format but are infinite.

The binary representation of 0.57 is basically (52 bits): 0010001111010111000010100011110101110000101000111101

If there are only 52 digits, 0.57 = "0.56999999999999995

It's not hard to see the unexpected result above. Let's add an example.

There are many methods. Here are two examples:

1. sprintf

Substr (sprintf ("%. 10f", (\$ a/\$ B), 0,-7 );
2. round (note that it will be rounded off)

Round (\$ a/\$ B, 3 );

I have always understood the precision as the number of decimal points reserved. Is this true for floating point numbers in php? The answer is No.

A floating point is actually composed of an integer and a decimal part. The precision here is that the number of digits in the integer part plus the decimal part cannot exceed the maximum precision, then, the maximum precision value is truncated according to the rounding method. If the integer is 0, digits are not counted, and 0 at the end of the decimal part is not counted into digits. In addition, for the same number, different precision may display different forms. The following is an example.

The integer is 0.

\$ Num = 0.12345600000000000;
// The integer part is 0, the number of digits is 0, and the 0 at the end of the decimal part is not counted into the number of digits, so the total number of digits is 6

Ini_set ("precision", "12 ");
Echo \$ num; /// 0.123456
// If the precision value is not exceeded, the displayed result is 0.123456.

Ini_set ("precision", "3 ");
Echo \$ num; /// 0.123
// Exceeds the precision value, retain 3 bits

Ini_set ("precision", "5 ");
Echo \$ num; /// 0.12346
// Exceeds the precision value, retain 5 bits
In this case, the precision value is equivalent to the number of digits after the decimal point.

If the integer part is greater than 0

\$ Num = 12.12345600000000000;
// The integer part is 12, the number of digits is 2, and the 0 at the end of the decimal part is not counted into the number of digits. The number of digits is 6, so the total number of digits is 2 + 6

Ini_set ("precision", "12 ");
Echo \$ num; /// 12.123456
// If the precision value is not exceeded, the displayed result is 12.123456.

Ini_set ("precision", "3 ");
Echo \$ num; /// 12.1
// Beyond the precision value, the number of digits in the integer part is 2, so only one decimal point is retained

Ini_set ("precision", "5 ");
Echo \$ num; /// 12.123
// Beyond the precision value, the number of digits in the integer part is 2, so only three decimal places are reserved.
We can see that the number of digits retained after the decimal point is related to the number of digits whose precision is already an integer.

Case 2 where the integer part is greater than 0

\$ Num = 12345678.12345600000000000;
// The integer part is 12345678, and the number of digits is 8. The value 0 at the end of the decimal part is not counted as the number of digits. The number of digits is 6, so the total number of digits is 8 + 6.

Ini_set ("precision", "12 ");
Echo \$ num; /// 12345678.1235
// If the precision value is exceeded, the displayed result is 12345678.1235.

Ini_set ("precision", "3 ");
Echo \$ num; // 1.23E + 7
// Beyond the precision value, and the number of digits in the integer part exceeds the precision, the fractional part is discarded, and the integer part is only three digits

Ini_set ("precision", "5 ");
Echo \$ num; /// 12346000
// Beyond the precision value, and the number of digits in the integer part exceeds the precision, the fractional part is discarded, and the integer part is only five digits
In the above example, we can see that the precision value is also related to the truncation of the integer part. Note that the methods shown in the last two examples are different. One is to use scientific notation, and the other is to use 0 as the complement. The experiment shows that when the number of digits in the integer part minus the precision value is greater than 4, scientific notation is used. Otherwise, 0 is used to supplement the number. In other words, that is, after the number of digits in the integer part exceeds the precision value, after truncation, the number of digits supplemented by 0 will not exceed 4.

Floating point calculation

\$ Num1 = 1331625729.687;
\$ Num2 = 1331625730.934;
Ini_set ("precision", "8 ");

Echo \$ num1 .'
';
Echo \$ num2 .'
';

\$ Sub = \$ num1-\$ num2;

Echo \$ sub .'
';
// The output result is:
/*
1331625700
1331625700
-1.247
*/
The preceding example shows that the precision value only controls the display result, and the internal storage is still the original value. Therefore, the value of \$ sub is 1331625729.687 minus 1331625730.934.

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.