How to calculate and compare the floating point number in PHP and obtain the integer is inaccurate

Source: Internet
Author: User
This article describes how to calculate and compare floating-point numbers in PHP and how to obtain an inaccurate integer. The code is very simple. For more information, see

This article describes how to calculate and compare floating-point numbers in PHP and how to obtain an inaccurate integer. The code is very simple. For more information, see

The interesting phenomenon of php should be that many programming languages have such a phenomenon. This is because of the floating point number recognition problem of the computer itself... the following code will show you:

$ F = 0.58; var_dump (intval ($ f * 100*100); // result 5799 var_dump (float) ($ f * 100*100 )); // result 5800 echo (int) (0.1 + 0.7) * 10); // result 7 echo (float) (0.1 + 0.7) * 10 ); // result 8 <? Php $ a = 0.1; $ B = 0.7; var_dump ($ a + $ B) = 0.8); // The printed value is boolean false <? Php $ a = 0.1; $ B = 0.7; var_dump (bcadd ($ a, $ B, 2) = 0.8); // bool true

Note that during Floating Point calculation, do not convert a floating point to an integer. Otherwise, an unanticipated error may occur.

The above code is a solution to the calculation and comparison of floating point numbers in PHP and the inaccuracy of integer obtaining.

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.