Floating Point Accuracy

Source: Internet
Author: User
1. background description: echo (10 1. background description: echo (10-9.8-0.2) in php code; returns a number that is approximately 0 but not 0, I have seen a lot of blogs explaining why the accuracy is lost after computation. That is to say, the floating point numbers stored in binary are distorted and can only be saved in an approximate way.
2. Ask the question: how can we avoid this problem when it is displayed? For example, if the balance in your bank account cannot be shown as 0.000000001. Do you have any good solutions?
3. Temporary Use method: at present, I simply use the round to get an integer, but it is quite troublesome for all the calculations to be processed. What should I do?

Reply content:

1. background description: echo (10-9.8-0.2) in php code; returns a number that is approximately 0 but not 0, I have seen a lot of blogs explaining why the accuracy is lost after computation. That is to say, the floating point numbers stored in binary are distorted and can only be saved in an approximate way.
2. Ask the question: how can we avoid this problem when it is displayed? For example, if the balance in your bank account cannot be shown as 0.000000001. Do you have any good solutions?
3. Temporary Use method: at present, I simply use the round to get an integer, but it is quite troublesome for all the calculations to be processed. What should I do?

The floating point number calculation in the program is similar. As a programmer, we should never expect that the results will be accurate. Therefore, we need to clarify the accuracy requirements before calculation. For example, when calculating the diameter of the solar systemπWhen the decimal point is accurate to more than a dozen digits, the calculated radius error is only a few centimeters.

For the amount, some languages have proprietary data types for the amount. Other languages are controlled by yourself. For example, the amount generally only needs to be accurate to the decimal point.2Bit. Another way to handle the amount is to save to the score, for example1Save the RMB100Minute, and then the display is processed as a dollar, so that only an integer is involved in the calculation.

This problem is so prevalent that you can find the answer on the Internet. It is currently unlikely that the result obtained from the addition and subtraction multiplier between floating point numbers is absolutely accurate.

You can use PHP's BC high-precision function library. The advantage of using this database is that you can control at least the precise number of digits without getting out of control. See the PHP manual.

echo bcsub(10-9.8-0.2,0);//0Http://php.net/manual/en/ref.bc.php

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.