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