How to compare and integer inaccuracy of floating point numbers in PHP

Source: Internet
Author: User
This article describes how to calculate and compare the floating point numbers in PHP and how to solve the inaccuracy of the integer. For more information, see

This article describes how to calculate and compare the floating point numbers in PHP and how to solve the inaccuracy of the integer. For more information, see

Comparison of floating point calculation results
An example of floating point number calculation is as follows:

The Code is as follows:


$ A = 0.2 + 0.7;
$ B = 0.9;
Var_dump ($ a = $ B );

The printed result is: bool (false ). That is to say, the calculation result of 0.2 + 0.7 is not the same as that of 0.9, which is obviously against our common sense.

In this regard, the official PHP manual once explained that a simple decimal score, for example, 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 scores with limited digits. For example, decimal 1/3 is changed to 0. 3333333 ....

We print the above variables in Double Precision format:

The Code is as follows:


$ A = 0.2 + 0.7;
$ B = 0.9;
Printf ("% 0.20f", $ );
Echo'
';
Printf ("% 0.20f", $ B );

The output result is as follows:

The Code is as follows:


0.89999999999999991118
0.90000000000000002220

Obviously, as a floating point data, its accuracy has lost some of it, and it cannot be completely accurate. Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. It should be noted that this is not a PHP issue, but a problem of floating point processing inside the computer! The same problem occurs in C, JAVA, and other languages.

Therefore, to compare two floating point numbers, we need to control them within the range of precision we need and then compare them. Therefore, we use the bcadd () function to add and convert the floating point numbers (as strings ):

The Code is as follows:


Var_dump (bcadd (0.2, 0.7, 1) = 0.9); // output: bool (true)

Integer

In the article "ceil and floor of the entire function in PHP", there were examples:

The Code is as follows:


<? Php
Echo ceil (2.1/0.7); // output: 4
?>

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

The Code is 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 accuracy of floating point calculation results. So we can use the round () function to process it:

The Code is as follows:


<? Php
Echo ceil (round (2.1/0.7), 1 ));
?>

Although the round () function is rounded to the nearest decimal point based on the specified precision, it does not affect the result.

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.