Calculation of floating-point numbers in PHP and the solution of inaccurate rounding, rounding inaccurate _php tutorial

Source: Internet
Author: User

Calculation of floating-point numbers in PHP and the solution of inaccurate rounding, rounding inaccurate


Comparison of floating-point calculation results
A floating-point calculation example is as follows:

Copy the Code code 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:

Copy the Code code as follows:
$a = 0.2+0.7;
$b = 0.9;
printf ("%0.20f", $a);
Echo '
';
printf ("%0.20f", $b);

The output results are as follows:

Copy the Code code as follows:
0.89999999999999991118
0.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:

Copy the Code code as follows:
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:

Copy the Code code as follows:
<?php
echo 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:

Copy the Code code as follows:
<?php
printf ("%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:

Copy the Code code as follows:
<?php
Echo 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.

http://www.bkjia.com/PHPjc/940487.html www.bkjia.com true http://www.bkjia.com/PHPjc/940487.html techarticle calculation of floating-point numbers in PHP and the solution of inaccurate rounding, the calculation results of inaccurate floating-point numbers a floating-point calculation example is as follows: Copy the code code as follows: $a = 0.2+ ...

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