PHP floating point accuracy problem summary, PHP floating point accuracy _php tutorial

Source: Internet
Author: User

PHP floating point accuracy problem summary, PHP floating point number accuracy


First, the accuracy of the PHP floating point loss problem

Let's look at the following code:
Copy the Code code as follows:
$f = 0.57;
Echo intval ($f * 100); 56

The result may be a bit out of your surprise, PHP follows IEEE 754 double precision:

Floating-point numbers, with 64-bit double precision, with 1-bit sign bit (E), 11 exponent bit (Q), 52-bit Mantissa (M) (altogether 64 bits).
Sign bit: The highest bit represents the positive or negative of the data, 0 is a positive number, and 1 indicates a negative number.
Digits: Indicates that the data is a power of 2, 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 decimals are represented by binary:

Multiply by 2, order, the fractional part is multiplied by 2, then take the integer part, the remaining fractional part continues to multiply by 2, then take the integer part, the remainder of the fractional part is multiplied by 2, has been taken to the fractional part, But like 0.57 decimal places like this have been riding down, the fractional part is not possible to 0. The decimal number of the valid bits is represented by a binary representation of infinity.

The binary representation of 0.57 is basically (52 bits) is: 0010001111010111000010100011110101110000101000111101

If only 52 digits, 0.57 = "0.56999999999999995

It is not difficult to see the result of the above accident.

Second, the accuracy of the PHP floating point number

First look at the question:

Copy the Code code as follows:
$f = 0.58;
Var_dump (Intval ($f * 100)); Why output 57

I believe that a lot of students have had such doubts.

Specific principles can be read "Brother Bird" an article, where there is a detailed explanation: PHP floating point number of a common problem of the answer

So how do you avoid this problem?
There are many ways to do this, listing two here:
1. sprintf
Copy the Code code as follows:
substr (sprintf ("%.10f", ($a/$b)), 0,-7);

2. Round (attention will be rounded)
Copy the Code code as follows:
Round ($a/$b, 3);

Or you have a better way, you can also leave a message to tell me.

Three, PHP floating point number of a common question of the answer

About the floating-point number of PHP, I've written an article before: What you should know about PHP floating-point numbers (all ' bogus ' on the float in PHP)

However, I missed a point, which is the answer to the following FAQ:
Copy the Code code as follows:
<?php
$f = 0.58;
Var_dump (Intval ($f * 100)); Why output 57
?>

Why is the output 57? PHP bug?

I believe that a lot of students have had this kind of doubt, because the light asks me the similar question person to be many, not to mention bugs.php.net often people ask ...

To understand this, first we need to know the representation of floating-point numbers (IEEE 754):

Floating-point numbers, in the case of a 64-bit length (double), take the 1-bit sign bit (E), 11 exponent (Q), and 52-bit mantissa (M) (altogether 64 bits).

Sign bit: The highest bit represents the positive or negative of the data, 0 is a positive number, and 1 indicates a negative number.

Digits: Indicates that the data is a power of 2, and the exponent is represented by an offset code

Mantissa: A valid number that represents the decimal point of the data.

Here is the key point is that the decimal in the binary representation, about how to use binary decimal notation, you can Baidu, I do not repeat here, we key to understand, 0.58 for the 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 binary of both, if only through the 52-bit calculation, respectively:
Copy the Code code 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, interesting to see (floating point), we are vague in mental arithmetic to see ... 0.58 * 100 = 57.999999999

Then you intval, Nature is 57 ....

Visible, 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 .....

http://www.bkjia.com/PHPjc/998816.html www.bkjia.com true http://www.bkjia.com/PHPjc/998816.html techarticle PHP Floating-point accuracy problem Summary, PHP floating-point precision one, PHP floating-point accuracy loss of the problem first look at the following code: Copy code code as follows: $f = 0.57; echo intval ($f *);

  • 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.