Php numeric addition comparison size

Source: Internet
Author: User
{Code ...}

  The result is float (0.1) float (0.1) false. Why does the result output false ????

Reply content:

  The result is float (0.1) float (0.1) false. Why does the result output false ????

In the PHP manual, there is one sentence: Never compare two floating point numbers for equal comparison using bccomp ()

Php floating-point operations are inaccurate. Don't worry about this problem. This problem exists in various languages and you output

echo number_format($ss, 30);

Try it. It does not mean 0.1.
Manual floating point description

Like js 0.1 + 0.2 = 0.3 // false, it involves encoding.
Because the number of digits in a computer is limited, it cannot fully express all floating point numbers. Some can only obtain an approximate value, for example:
0.01 + 0.09 = 0.099999999999 // js result

Therefore, the comparison of floating point numbers sometimes guarantees the accuracy. For example, $ a-$ B <= 0.000001 indicates equal
Of course, it can be converted into an integer for comparison, $100 + $ B100 = $ c * 100

Different Languages store floating point numbers in different ways, with different precision. Weak languages such as js and php are especially prone to inaccurate data. as said above, 1 does not need to be entangled. 2. Do not compare floating point numbers. 3. make sure to compare, then compare within a certain range

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.