We know that when dealing with some floating-point numbers, the operation produces an error, resulting in a similar infinite loop.
Example:
@Test publicvoid Demo () { double a1=2.0; double a2=1.1; SYSTEM.OUT.PRINTLN (A2-a1); // -0.8999999999999999 }
The reason for this is that the binary 01 code used by the computer does not accurately represent some decimal data with decimals.
Let's analyze the following:
We know that converting a decimal value to a binary value requires the following calculation method:
1. Integer part: Divide the integer by 2 consecutively, take the remainder, and then divide the quotient by 2 until the quotient equals 0. The resulting remainder is then arranged in reverse order. For short, "In addition to 2 of the remainder method."
2. Fractional part: Decimal decimal number converted to binary decimal, using "Multiply 2 rounding, ordered" method. Use 2 times the decimal decimal, the resulting integer part is taken out, then 2 times the remainder of the fractional part, and then the integer portion of the product is taken out, so that until the fractional part of the product is divided into 0 or to achieve the required precision. The integer part is then sorted in order, that is, the integer part is taken as the high position of the binary decimal, and the integer part is taken as the low-effective bit. The abbreviation "Multiply 2 Take the whole method".
3. Decimal numbers with decimals are converted to binary, integers, fractional parts are converted, and then added.
For example, to convert a decimal value of 25.75 to a binary value, proceed as follows:
25 (integer part)
25/2=12......1
12/2=6.......0
6/2=3......0
3/2=1......1
1/2=0......1
(25) 10 = (11001) 2
0.75 (part of the decimal)
0.75*2=1.5......1
0.5*2=1......1
(0.75) 10 = (0.11) 2
(25.75) 10 = (11001) (0.11) 2 = (11001.11) 2
In the above method, we convert 0.65 and 0.6 into binary code:
(0.65) 10 = (0.101001100110011001100110011001100110011 ...) 2
(0.6) 10 = (0.10011001100110011001100110011001100110011 ...) 2
The following ellipsis indicates that it is not finished, followed by an infinite repetition of 0011 of this binary value.
How to solve this problem? Knowing its root cause, we know that it is not possible to fundamentally solve this problem, but we can have some ways of saving the curve, the following list several:
1. Because binary values can accurately represent integers (which can be converted to binary methods by using integers), you can multiply decimals by 10 or 100 to become integers, and then do integer operations, and finally get results by dividing by 10 or 100;
2. Obtain the best approximate result by capturing the effective number of decimal digits of the result, and then do the processing.
3. For decimal values that can be represented with a finite-length binary value, you can use a data type that has a storage bit size greater than its length.
Java provides the super-large class of BigDecimal, which is used to store floating-point types, which can handle binary error problems.
@Test Public void Demo1 () { BigDecimal b1=new BigDecimal (2.0+ ""); Note: You can only use the string type construction System.out.println (b1.subtract (new BigDecimal (1.1+ "))); 0.9 }
Other specific ways to view the API. Large plastic data using BigDecimal
Binary arithmetic error problem