Example sharing in PHP that solves the problem of inaccurate floating-point arithmetic

Source: Internet
Author: User
Computer programming always encounter the problem of the accuracy of floating-point arithmetic, this article through PHP to solve the problem of inaccurate floating-point arithmetic sharing, I hope that we solve the floating-point arithmetic accuracy problems have been helpful.

Comparison of floating-point calculation results

A floating-point calculation example is as follows:

$a = 0.2+0.7; $b = 0.9;var_dump ($a = = $b);

The result of the printout is: BOOL (false). That is to say, the result of 0.2+0.7 here is not equal to 0.9, which is obviously contrary to our common sense.

In this case, the official PHP manual has also explained: apparently simple decimal fractions such as 0.2 cannot be converted to an internal binary format without losing a little bit of precision. This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333 ....

We print the above variables in a double-precision format:

$a = 0.2+0.7; $b = 0.9;printf ("%0.20f", $a); Echo ' <br/> ';p rintf ("%0.20f", $b);


The output results are as follows:

0.899999999999999911180.90000000000000002220

Obviously here, as a floating-point data, its accuracy has been lost in part, not fully accurate. So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. It should be explained that this is not a problem with PHP, but the internal processing of floating-point number of the computer problem! In C, JAVA and other languages will also encounter the same problem.

Therefore, to compare two floating-point numbers, you need to control them within the range of precision we need, so we use the Bcadd () function to add and make precision conversions (for strings) of floating-point numbers:

Var_dump (Bcadd (0.2,0.7,1) = = 0.9); Output: bool (TRUE)

Floating point rounding

In the article "PHP takes the whole function ceil and floor", there are examples:

<?phpecho ceil (2.1/0.7);    Output:4?>

In the face of the calculation of floating point number, we know that this is not exactly the result of floating point calculation results:


<?phpprintf ("%0.20f", (2.1/0.7));    Output:3.00000000000000044409?>


After the discussion of floating-point number calculation, we know that this is caused by the incomplete calculation result of floating-point number, so we can use the round () function to deal with it:

<?phpecho ceil (Round (2.1/0.7), 1);? >

Although the round () function is rounded with the specified precision, leaving one digit after the decimal point has no effect on our rounding results.

Related recommendations:

The method of fetching surplus of PHP floating-point number

Questions about the accuracy of the PHP floating-point numbers

PHP floating-point rounding function _php Tutorial

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.