In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "PHP floating-point knowledge collation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
PHP is a weakly typed language. Such features inevitably require seamless and transparent implicit type conversion. Zval is used internally in PHP to store values of any type. The structure of zval is as follows (5.2 as an example):
The copy code is as follows:
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, it is the zvalue_value complex that actually holds the values:
The copy code is as follows:
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 of the members, lval and dval, we have to realize that long lval varies with the word length of compiler, OS, it may be 32bits or 64bits, while double dval (double precision) is specified by IEEE 754, is fixed length, must be 64bits.
Keep this in mind, which makes some of PHP's code non-platform independent. The rest of our discussion, unless specifically specified, assumes that long is 64bits
IEEE 754 floating-point counting method, I will not quote here, you are interested can check, the key point is that the Mantissa of double using 52-bit bit to save, including the hidden 1 significant bit, a total of 53bits.
Here, an interesting question arises. Let's use the c code as an example (suppose long is 64bits):
The copy code is as follows:
Long a = x
Assert (a = = (long) (double) a)
Now let's get back to the point. Before executing a script, PHP first needs to read and analyze the script, and this process also involves zval the literal amount of the script, such as for the following script:
The copy code is as follows:
Of course, to be on the safe side, we should use strings to hold large integers and use mathematical libraries such as bcmath to calculate.
In addition, there is another key configuration that will confuse us. This configuration is php.precision, which determines how many significant bits are output when PHP outputs another float value.
Finally, let's go back to the question raised above, that is, what is the maximum value of an integer of long to ensure that precision loss will not occur after switching to float and then going back to long?
For example, for an integer, we know that its binary representation is 101. now, let's move two bits to the right to 1.01, strip out the high-order implied significant bit 1, and we get the binary value of 5 stored in double as:
The copy code is as follows:
0ax * symbol bit * / 10000000001amp * index bit * / 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The binary representation of 5 is kept intact in the Mantissa section, in which case there will be no loss of precision when transferred from double to long.
We know that double uses 52 bits to represent Mantissa, including the implied first digit 1, which has a total precision of 53 digits. Then it can be concluded that if the integer of a long is less than:
The copy code is as follows:
2 ^ 53-1 = = 9007199254740991; / / remember, we now assume that it is the long of 64bits
Then, this integer, when the numerical conversion of long- > double- > long occurs, there is no loss of precision.
This is the end of the content of "PHP floating-point knowledge collation". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.