All 'bogus' about the float in PHP you should know
PHP is a weakly typed language. Such a feature must have Seamlessly transparent implicit type conversion, PHP uses zval internally to save any type of value. The structure of zval is as follows (5.2 as an example):
struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount; zend_uchar type; /* active type */ zend_uchar is_ref; };
In the above structure, the actual value itself is the zvalue_value union. :
typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj; } zvalue_value;
Today’s topic, we only focus on two members, lval and dval. We must realize that long lval has an indefinite length depending on the word length of the compiler and OS. It has It may be 32bits or 64bits, and double dval (double precision) is stipulated by IEEE 754. It is fixed length and must be 64bits.
Please remember this, which makes some PHP codes "non-platform independent" "property". Our following discussion, unless otherwise specified, assumes that long is a 64bits
IEEE 754 floating-point counting method. I will not quote it here. If you are interested, you can check it out for yourself. The key One point is that the mantissa of double is stored in 52 bits. Counting the hidden 1 significant bit, the total is 53 bits.
Here, an interesting question arises. Let’s use c code as an example (assuming long is 64bits):
long a = x; assert(a == (long)(double)a);
Excuse me, when the value of a is within what range, can the above code be asserted to be successful? (Leave the answer at the end of the article)
Now we return to the topic, PHP Before executing a script, you first need to read the script and analyze the script. This process also includes zvalizing the literals in the script. For example, for the following script:
<?php $a = 9223372036854775807; //64位有符号数最大值 $b = 9223372036854775808; //最大值+1 var_dump($a); var_dump($b);
Output:
int(9223372036854775807) float(9.22337203685E+18)
In other words, during the lexical analysis phase, PHP will judge whether a literal value exceeds the long table value range of the current system. If not, it will use lval to save it, and zval will be IS_LONG. Otherwise, it will use lval to save it. Just use dval to represent it, zval IS_FLOAT.
We must be careful with any value larger than the largest integer value, because it may cause loss of accuracy:
<?php $a = 9223372036854775807; $b = 9223372036854775808; var_dump($a === ($b - 1));
The output is false.
Now to continue the discussion at the beginning, as mentioned before, PHP's integers may be 32-bit or 64-bit, so it is decided that some codes that can run normally on 64-bit may fail due to invisible Type conversion causes precision loss, causing the code to not run properly on 32-bit systems.
So, we must be wary of this critical value. Fortunately, this critical value has been defined in PHP:
<?php echo PHP_INT_MAX; ?>
Of course, to be safe, we should use strings to save large integers, and use mathematical function libraries such as bcmath to perform calculations.
In addition, there is a key configuration that will make We are confused. This configuration is php.precision. This configuration determines how many significant bits PHP outputs when it outputs a float value.
Finally, let’s look back at the question raised above, which is a long What is the maximum value of an integer to ensure that the precision will not be lost when converted to float and then converted back to long?
For example, for an integer, we know that its binary representation is, 101. Now, let us Shift two bits to the right to become 1.01, discard the implicit significant bit 1 of the high bit, and we get the binary value of 5 stored in the double:
0/*符号位*/ 10000000001/*指数位*/ 0100000000000000000000000000000000000000000000000000
5’s binary representation, which is saved in the The mantissa part, in this case, is converted from double back to long without loss of precision.
We know that double uses 52 bits to represent the mantissa. Counting the implicit first 1, there is a total of 53 bits of precision. Then it can be concluded that if the value of a long integer is less than:
2^53 - 1 == 9007199254740991; //牢记, 我们现在假设是64bits的long
, then this integer will not lose precision when the long->double->long value conversion occurs.
Recommended: "PHP Tutorial"
The above is the detailed content of Things you should know about PHP floating point numbers. For more information, please follow other related articles on the PHP Chinese website!