How to calculate and compare php floating point numbers and obtain the integer is not accurate? _ PHP Tutorial

Source: Internet
Author: User
Php floating point number calculation comparison and integer inaccuracy solution, the integer is not accurate. Php floating point number calculation comparison and integer inaccuracy solution, integer inaccuracy php interesting phenomenon, it should be a phenomenon in many programming languages. This is because the computation of the computer's php floating point is not accurate, and the integer is not accurate.

The interesting phenomenon of php should be that many programming languages have such a phenomenon. This is because the computer itself has a problem with floating point recognition .....

[Php]View plaincopy
  1. $ F = 0.58;
  2. Var_dump (intval ($ f * 100*100); // result 5799
  3. Var_dump (float) ($ f * 100*100); // result 5800
  4. Echo (int) (0.1 + 0.7) * 10); // Result 7
  5. Echo (float) (0.1 + 0.7) * 10); // Result 8

[Php]View plaincopy
  1. $ A = 0.1;
  2. $ B = 0.7;
  3. Var_dump ($ a + $ B) = 0.8 );
  4. // The printed value is boolean false.
[Php]View plaincopy
  1. $ A = 0.1;
  2. $ B = 0.7;
  3. Var_dump (bcadd ($ a, $ B, 2) = 0.8); // bool true

Do not convert a floating point to an integer when calculating a floating point. Otherwise, an unpredictable error may occur.

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.

The interesting phenomenon in php is that many programming languages have such a phenomenon. This is because the computer itself...

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.