I have written an article about the floating point number in PHP: you should know about the PHP floating point number (All 'bogg' aboutthefloatinPHP). However, I missed a point at the time, that is, to answer the following Frequently Asked Questions: & lt ;? Php
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:
- $ 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): 001000111101011100001010001111010111000010100011110
The binary values of the two are:
- 0.58 -> 0.57999999999999996
- 0.57 -> 0.5699999999999999
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 .....