You may know but make mistakes. Details determine the success or failure-PHP source code

Source: Internet
Author: User
You may know but make mistakes. Details determine success or failure. you may know that forced conversion will lose precision. you may know more or less similar details, and you may also have missed details.
1 + 1 = 2
3 * (1/3) = 1 Yes, but you can try it on the computer.
3*0. 3333... = 0.999999999! = 1
This is the details. When should I pay attention to the situation? for example, I pay attention to cash and interest rates.

 "; Echo (int) (0.1 + 0.1) * 10); // 2 echo"
"; Echo (int) (0.1 + 0.7) * 10); // 7 echo"
"; Echo (0.1 + 0.7); // 0.8 echo"
"; Echo intval (0.1 + 0.7) * 10); // 7 instead of 8 echo"
"; Echo intval (bcadd (0.1, 0.7, 8) * 10 ); // 8 ++ ++, you may understand, but I still don't understand, because I am stupid, so I found php. Chm provides some explanations about the Warning of Warning on floating point precision. Obviously, the decimal score is as simple as 0.1 or 0.7. it cannot be converted to the internal binary format without losing a little bit of precision. This results in confusion: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in the expected result, because the internal representation of this result is similar to 7.9. This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example, the value of decimal 1/3 is 0.3. 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. If higher precision is required, use any precision mathematical function or gmp function. For any precision calculation above, refer to php. chm bcadd-Add two arbitrary precision numbers string bcadd (string $ left_operand, string $ right_operand [, int $ scale]).

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.