Java uses the IEEE754 floating point number to process its float and double. This standard is for base base number 2, which cannot be used to accurately represent base 10. See here http://en.wikipedia.org/wiki/IEEE_floating_point .
The following is not a standard, but an example so that you understand why the base 2 floating point is not suitable for another base.
base2 = base10
0001 = 0001 -> from 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1
0010 = 0002 -> from 0 * 8 + 0 * 4 + 1 * 2 + 0 * 1
0011 = 0003 -> from 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1
0100 = 0004 -> from 0 * 8 + 1 * 4 + 0 * 2 + 0 * 1
0101 = 0005 -> from 0 * 8 + 1 * 4 + 0 * 2 + 1 * 1
8 = 2 ^ 3, 4 = 2 ^ 2, 2 = 2 ^ 1 and 1 = 2 ^ 0
Then
base2 = base10
.0000 = .0000 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0001 = .0625 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0010 = .1250 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0011 = .1875 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0100 = .2500 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0101 = .3125 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0110 = .3750 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.0111 = .4375 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1000 = .5000 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1001 = .5625 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1010 = .6250 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1011 = .6875 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1100 = .7500 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1101 = .8125 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1110 = .8700 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
.1111 = .9325 -> from 0 * 1 + 0.5 * 0 + 0.25 * 0 + 0.125 * 0 + 0.0625 * 0
1 = 2 ^ 0, 0.5 = 2 ^ -1, 0.25 = 2 ^ -2 and 0.125 = 2 ^ -3
As you can see. A 4-bit floating can only display base 10 numbers from 0 to 0.9325 with an interval of 0.0625. And this also means that he cannot do 0.1, 0.2, 0.3 ....
Since the actual standard uses a lot more bits, it also uses a digit switching method. It can indeed represent a much larger number than this example, but the restriction still remains the same. Therefore, when you divide a certain value, and the result does not fall on one of them ... the JVM will move it to the nearest one.
Hope this explains.
source share