The problem of PHP floating-point arithmetic precision

Source: Internet
Author: User
Tags arithmetic mathematical functions php website


In the operation of floating-point numbers in PHP, a hole was encountered, which did not get the expected result, as follows:


$a = 69.1;

$b = $a *100;

$c = $b-6910;

What do you think the value of $c is? The value $c output is -9.0949470177293E-13. Why is that?


In the PHP website float float page, said:

The precision of floating point numbers

The precision of floating-point numbers is limited. Although PHP typically uses IEEE 754 double-precision format Depending on the system, the maximum relative error caused by rounding is 1.11e-16. Non-basic mathematical operations may give greater error, and take into account the error transfer in the complex operation.

In addition, a rational number, such as 0.1 or 0.7, that can be accurately expressed in decimal, no amount of mantissa can be accurately represented by the binary used internally, so it cannot be converted to a binary format without losing a little bit of precision. This can result in confusing results: for example, Floor (0.1+0.7) *10 usually returns 7 rather than the expected 8 because the internal representation of the result is actually similar to 7.9999999999999991118 ....

So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. If you do need higher precision, you should use arbitrary precision mathematical functions or GMP functions.

So how to correctly handle the problem of PHP floating-point calculation error?


$x = 8-6.4; Which is equal to 1.6
$y = 1.6;
Var_dump ($x = = $y); Is isn't true

The values of $x and $y in the above examples are not equal. The solution is to use the round () function, such as:

Var_dump (Round ($x, 2) = = Round ($y, 2)); This is true

The reason for this problem is that $x is not 1.6, but 1.599999.

So the example at the beginning of this article changes to the following OK:

$a = 69.1;

$b = $a *100;

$c = Round ($b)-6910;

or format using Number_format ((float) $a, 2).

Let's look at an example of a PHP floating-point number that doesn't fit the expected results.


$a = intval (0.58*100);

$b = 0.58*100;

The value of the $a is unexpectedly 58, $b.

The solution is:

$a = Intval ((0.58*1000)/10);

or use binary Calculator, that is bcmath extension to solve the above problems

Add: <?php
$f = 0.58;
Var_dump (Intval ($f * 100)); Why output 57
?>
Why is the output 57? A bug in PHP?

I believe that a lot of students have such a question, because there are a lot of people who ask me similar questions, not to mention Bugs.php.net often asked ...

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

The floating-point number, in 64-bit length (double precision), takes the 1-bit sign bit (E), the 11 exponent bit (Q), and the 52-bit mantissa (M) to represent (altogether 64 digits).

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

Digit: Represents the power of the data at a 2 base, and the exponent is represented by an offset code

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

The key point here is that decimal in the binary representation, about decimal how to use binary, we can Baidu, I will not repeat here, we have to understand the key, 0.58 for 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 two binary, if only through the 52-digit calculation, respectively:

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, interested to see (floating point), we are vague in mental arithmetic to look at ... 0.58 * 100 = 57.999999999

Then you intval, Nature is 57 ....

As you can see, 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 ...

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.