This article mainly introduces the PHP floating point Precision problem summary, this article discusses the PHP floating point precision loss problem emphatically, uses the three paragraph different way to explain this question formation reason and the solution, needs the friend to be possible to refer to under
A, PHP floating point accuracy loss problem
Let's look at the following code:
The code is as follows:
$f = 0.57;
Echo intval ($f * 100); 56
The result may be a little surprising to you, PHP follows IEEE 754 double-precision:
The floating-point number is represented by a 64-digit double, with a 1-bit sign bit (E), 11 exponent bit (Q), and a 52-bit mantissa (M) (altogether 64 digits).
Sign bit: The highest digit indicates the positive or negative of the data, 0 indicates a positive number, and 1 is a negative number.
Digit: Represents the power of the data at a 2 base, and the exponent is represented by an offset code
Mantissa: A valid number that represents the decimal point of the data.
Let's see how decimal is expressed in binary notation:
Multiply by 2, in order, the fractional part multiplied by 2, then the integer part, the remainder of the fractional part is multiplied by 2, then the integral part, the remainder of the fractional part multiplied by 2, and is taken to the fractional part, But like 0.57 decimal places like this continue to multiply, the decimal part is not 0. The decimal representation of a valid bit is infinite in binary notation.
The binary representation of 0.57 is basically (52 bits) is: 0010001111010111000010100011110101110000101000111101
If only 52 digits, 0.57 = "0.56999999999999995"
It's not hard to see the unexpected results above.
Second, the accuracy of PHP floating point numbers
First look at the question:
The code is as follows:
$f = 0.58;
Var_dump (Intval ($f * 100)); Why output 57
I believe that a lot of students have had such a question.
The specific principle can read "Brother Bird" an article, where there are detailed explanations: PHP floating-point number of a common problem solution
So how do you avoid this problem?
There are a number of ways, here are two:
1. sprintf
The code is as follows:
substr (sprintf ("%.10f", ($a/$b)), 0,-7);
2. Round (note will be rounded)
The code is as follows:
Round ($a/$b, 3);
Or you have a better way, you can leave a message to tell me.
Three, PHP floating-point number of a common problem solution
About PHP floating-point number, I have written an article before: About PHP floating-point number you should know (all ' bogus ' about the float in PHP)
However, I was missing a point, which is the answer to the FAQ below:
The code is as follows:
$f = 0.58;
Var_dump (Intval ($f * 100)); Why output 57
?>
Why is the output 57? A bug in PHP?
I believe that a lot of students have such a question, because there are a lot of people who ask me similar questions, not to mention Bugs.php.net often asked ...
To understand the reason, first we need to know the representation of floating-point numbers (IEEE 754):
The floating-point number, in 64-bit length (double precision), takes the 1-bit sign bit (E), the 11 exponent bit (Q), and the 52-bit mantissa (M) to represent (altogether 64 digits).
Sign bit: The highest digit indicates the positive or negative of the data, 0 indicates a positive number, and 1 is a negative number.
Digit: Represents the power of the data at a 2 base, and the exponent is represented by an offset code
Mantissa: A valid number that represents the decimal point of the data.
The key point here is that decimal in the binary representation, about decimal how to use binary, we can Baidu, I will not repeat here, we have to understand the key, 0.58 for binary representation, is an infinitely long value (the following number omitted the implied 1).
The binary representation of 0.58 is basically (52 bits) is: 0010100011110101110000101000111101011100001010001111
The binary representation of 0.57 is basically (52 bits) is: 0010001111010111000010100011110101110000101000111101
And the two binary, if only through the 52-digit calculation, respectively:
The code is as follows:
0.58-> 0.57999999999999996
0.57-> 0.56999999999999995
As for the 0.58 * 100 of the specific floating-point multiplication, we do not consider so thin, interested to see (floating point), we are vague in mental arithmetic to look at ... 0.58 * 100 = 57.999999999
Then you intval, Nature is 57 ....
As you can see, the key point of this problem is: "You seem to have a poor decimal, in the computer binary representation is infinite."
So, do not think this is a PHP bug, this is the case ...