Calculation and comparison of floating point numbers in PHP and the solution of inaccurate method _php Foundation

Source: Internet
Author: User

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

Copy Code code as follows:

\$a = 0.2+0.7;
\$b = 0.9;
Var_dump (\$a = = \$b);

The result is: BOOL (false). In other words, 0.2+0.7 's calculations here are not equal to 0.9, which is clearly contrary to our common sense.

In this case, the official PHP manual has also stated that it is clear that simple decimal scores 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 points with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333 ....

We print the above variables in a double format:

Copy Code code as follows:

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

The output results are as follows:

Copy Code code as follows:

0.89999999999999991118
0.90000000000000002220

Obviously here, in fact, as floating-point data, its precision has been lost a part, not fully accurate. So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. What needs to be explained is that this is not a PHP problem, but the problem of dealing with floating-point numbers inside the computer! The same problem can be found in languages such as C, JAVA, and so on.

So to compare two floating-point numbers, you need to control them in the range of precision we need to compare them, so use the Bcadd () function to add and make the precision conversion (for string) of floating-point numbers:

Copy Code code as follows:

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

Floating-point number rounding

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

Copy Code code as follows:

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

After the calculation of floating point numbers, we know that this is caused by the incomplete accuracy of the floating-point calculation results:

Copy Code code as follows:

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

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

Copy Code code as follows:

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

Although the round () function is rounded according to the specified precision, reserving one digit after the decimal point does not affect our rounding result.

Related Keywords:

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.