At the beginning of the test, a question about the calculation error of the amount was raised. I didn't pay much attention at first, but then I looked at the code, changed it casually, did some exception handling, and submitted it.
Then, the test put forward another bug, "6.8-0.9=5.8". Then I was suddenly forced, and then I suddenly realized that JS, as an interpretation language, would have the problem of floating-point precision loss in direct calculation. Next, I found some information on the internet, and then made some modifications according to the specific principles, and finally solved the problem.
Binary representation of floating-point numbers:
The IEEE 754 standard is the standard number of the IEEE floating-point operation standard, which is equivalent to the international standard ISO/IEC/IEEE 60559. This standard is issued by the microprocessor standards committee (MSC) under the Computer Society of the Institute of Electrical and Electronics Engineers (IEEE). This standard defines the format of floating-point numbers (including negative zero -0) and abnormal values (denormal numbers), some special values (infinity (Inf) and non-numeric values (NaN)), and the "floating-point operators" of these values. Four numerical modification rules and five exceptions (including the timing and handling of exceptions) are also pointed out.
The floating-point number implementation of JS also follows the IEEE 754 standard and adopts double precision implementation. Among them, 1 bit is used to represent sign bit, 1 1 bit is used to represent exponent, and 52 bits are used to represent mantissa.
Solution:
In essence, when dealing with this kind of problems, the basic idea is to convert floating-point numbers into integers for calculation, and then adjust the decimal point of integers to make them return to the correct floating-point number results.