PHP floating point Precision problem summary, php floating point Precision
I. Precision loss of PHP floating point numbers
Let's take a look at the following code:
Copy codeThe Code is as follows:
$ 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.
Ii. Precision of PHP floating point numbers
First look at the problem:
Copy codeThe Code is as follows:
$ F = 0.58;
Var_dump (intval ($ f * 100); // for output 57
I believe many of you have such questions.
For more information about the principles, see the "laruence" Article, which provides a detailed explanation: answers to a frequently asked question about the PHP floating point number.
So how can we avoid this problem?
There are many methods. Here are two examples:
1. sprintf
Copy codeThe Code is as follows:
Substr (sprintf ("%. 10f", ($ a/$ B), 0,-7 );
2. round (note that it will be rounded off)
Copy codeThe Code is as follows:
Round ($ a/$ B, 3 );
Or you can leave a message to tell me a better solution.
Iii. answers to a common PHP Floating Point Number Problem
I have written an article about PHP floating point numbers: You should know about PHP floating point numbers (All 'bogg' about the float in PHP)
However, I did not answer the following Frequently Asked Questions:
Copy codeThe Code is as follows:
<? Php
$ F = 0.58;
Var_dump (intval ($ f * 100); // for output 57
?>
Why is output 57? PHP bug?
I believe many people have such questions, because there are many people who ask similar questions, not to mention bugs.php.net...
To understand this, first we need to know the floating point representation (IEEE 754 ):
Floating Point Number. Taking the 64-bit length (Double Precision) as an example, one-bit sign bit (E), 11 index bit (Q), and 52-bit ending number (M) are used) (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.
The key point here is that decimal places are represented in binary. You can refer to Baidu for how decimal places are represented in binary. I will not describe them here. We need to understand the key points, 0.58 for binary representation, it is an infinitely long value (the following number saves the implicit 1 )..
The binary representation of 0.58 is basically (52 bits): 0010100011110101110000101000111101011100001010001111
The binary representation of 0.57 is basically (52 bits): 0010001111010111000010100011110101110000101000111101
The binary values of the two are:
Copy codeThe Code is as follows:
0.58-> 0.57999999999999996
0.57-> 0.56999999999999995
As for the specific Floating-point multiplication of 0.58*100, we will not consider that detail. If you are interested, you can see the Floating point. We will look at it in a fuzzy way... 0.58*100 = 57.999999999
Then you intval it. Naturally, it's 57 ....
It can be seen that the key point of this problem is: "You seem to have a poor decimal number, but it is infinite in the binary representation of the computer"
So, no longer think this is a PHP bug. This is the case .....