PHP floating point Knowledge _php tutorial

Source: Internet
Author: User
Tags mathematical functions

PHP Floating point knowledge


This article mainly introduces you should know the PHP floating point knowledge, this article explains the PHP floating point number, the critical value of PHP numbers, the loss of precision, and so on, the need for friends to refer to the next

PHP is a weak type of language, such a feature, it is necessary to have a seamless transparent implicit type conversion, PHP internal use Zval to save any type of value, Zval structure as follows (5.2 for example):

The 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, the actual value of the values itself is the Zvalue_value consortium:

The 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 is the same as the compiler, the length of the OS is variable, it may be 32bits or 64bits, and double dval (double precision) by the IEEE 754 provisions , is fixed-length, must be 64bits.

Keep this in mind, creating a "non-platform agnostic" code for PHP. Our next discussion, except for the special specification, assumes that long is 64bits

IEEE 754 Floating-point counting method, I do not reference here, we are interested to see for themselves, the key point is that the mantissa of the double with 52 bit to save, count the hidden 1 bits, total is 53bits.

Here, a very interesting question, we use C code example (assuming Long is 64bits):

The code is as follows:

Long a = x;

assert (A = = (long) (double) a);

Excuse me, what is the value of a in the range of time, the above code can assert success? (Stay at the end of the article)

Now we are back to the point, PHP before executing a script, the first need to read into the script, parsing the script, the process is also included, the script in the literal zval, for example, for the following script:

The code is as follows:

  

$a = 9223372036854775807; Maximum number of 64-bit signed values

$b = 9223372036854775808; Maximum Value +1

Var_dump ($a);

Var_dump ($b);

Output:

The code is as follows:

Int (9223372036854775807)

Float (9.22337203685E+18)

Also said that PHP in the lexical analysis phase, for a literal value, will be judged, whether the current system is beyond the LONG table value range, if not, then use Lval to save, zval for Is_long, otherwise, dval, Zval is_float.

We have to be careful about values that are greater than the maximum integer value, because it can have a loss of precision:

The code is as follows:

  

$a = 9223372036854775807;

$b = 9223372036854775808;

Var_dump ($a = = = ($b-1));

The output is false.

Now the beginning of the discussion, said before, the whole number of PHP, may be 32-bit, or 64-bit, then decided that some of the 64-bit can run normal code, may be due to the invisible type conversion, the loss of precision, resulting in the code does not run properly on the 32-bit system.

Therefore, we must be wary of this threshold value, fortunately, in PHP has defined the threshold value:

The code is as follows:

  

Echo Php_int_max;

?>

Of course, for the sake of insurance, we should use a string to hold large integers, and use a library of mathematical functions such as bcmath for calculations.

In addition, there is a key configuration that will confuse us, this configuration is php.precision, this configuration determines the PHP output a float value when the output of the number of significant bits.

Finally, we look back at the question raised above, that is, a long integer, the maximum value is to ensure that after going to float and then back to long will not occur loss of precision?

For example, for integers, we know that its binary representation is, 101, now, let's move the two bits to the right, turn 1.01, take the implied significant bit 1 of the high, and we get the binary value that stores 5 in a double:

The code is as follows:

0/* sign bit */10000000001/* digit */0100000000000000000000000000000000000000000000000000

5 of the binary representation, the slightest loss of preservation in the tail part, in this case, from a double transfer back to long, there will be no loss of precision.

We know that double uses 52 bits to denote the mantissa, counting the implied first 1, which is a total of 53 bits of precision. Then it can be concluded that if a long integer, the value is less than:

The code is as follows:

2^53-1 = = 9007199254740991; Remember, we now assume that it is a long of 64bits

So, this integer, in the event of a numeric conversion of long->double->long, the loss of precision will not occur.

http://www.bkjia.com/PHPjc/1000081.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000081.html techarticle PHP Floating point knowledge This article mainly introduces you should know the PHP floating point knowledge, this article explains the PHP floating point number, the critical value of PHP numbers, the loss of precision, and so on, the need for friends can refer to the following ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.