Why 0.5+0.2+0.2+0.1 is not equal to 1 when PHP is doing addition calculation

Source: Internet
Author: User
Tags php source code
$sum=(0.5+0.2+0.2+0.1);//var_dump   float(1)$sum2=1  //var_dump   int(1)$sum == $sum2  //var_dump  bool(false)

What's the matter, please?
In addition, the following tests are possible.
For example

(0.5+0.2+0.3+0.0)==1  //var_dump  bool(true)

Reply content:

$sum=(0.5+0.2+0.2+0.1);//var_dump   float(1)$sum2=1  //var_dump   int(1)$sum == $sum2  //var_dump  bool(false)

What's the matter, please?
In addition, the following tests are possible.
For example

(0.5+0.2+0.3+0.0)==1  //var_dump  bool(true)

When calculating numbers, use Bcmath (point me to point me)

phpvar_dump(bcadd(0.1+0.001));var_dump(0.1+0.001);

The method of binary representation of decimal in the computer results in inaccurate accuracy
All the languages are like this decimal is more commonly used
ABS (expression) < 1e-14 (or other than very small number)

One is a float point, one is an integer, as for the following to understand the PHP source code in depth

This is the problem of floating point accuracy, which exists in all languages and is explained in the PHP Documentation: link Address

The classic question depends on the explanation of the great God.
Http://www.laruence.com/2013/03/26/2884.html

  • Related Article

    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.