Let’s start with a simple piece of code
public static void main(String[] args) {
System.out.println(0.1+0.2);
}
Copy the code
The print result is as follows:
0.30000000000000004 // Why does 0.1+0.2 not equal 0.3?
Copy the code
Why this weird answer?
It starts with the binary representation of floating-point numbers
Even an old lady crossing the street these days probably knows that computers use binary counting
Here’s a quick test: write the number 15 in binary form
// The answer should be simple: 0000 1111
Copy the code
You are familiar with the binary of integers
But what if I put it in decimals? 3.14159265359?
In fact, if you don’t do low-level design, the average person really don’t know the answer
But you have to understand how decimals are represented in binary, right
Why does 0.1+0.2 not equal 0.3?
Let’s take the decimal 0.1 as an example and see how it is stored in binary
In the first step, you multiply 0.1 by 2, and you get 0.2. Take the second step, and multiply 0.2 by 2, and you get 0.4. Take the third step, and multiply 0.4 by 2, and you get 0.8. The integer part 0 is taken from the fourth step, and the decimal part 0.8 left from the previous step is multiplied by 2, resulting in 1.6. The integer part 1 is taken from...... All the way to zeroCopy the code
The figure below shows the calculation process ↓
The final binary is just a collection of integer parts
It looks something like this:
0001 1001 1001 1001 1001.Copy the code
As you can see, the 1001 part is going to go on forever
So let’s write it as a binary decimal and it looks something like this
0.0001 1001 1001 1001 1001.Copy the code
It is equivalent to
You will see that it is not equal to 0.1
It’s just an approximation
Therefore, the more bits reserved for binary, the higher the accuracy
Early computers could not actually handle floating point numbers
It wasn’t until the IEEE 754 standard that computers could handle floating point numbers
According to IEEE 754 standard, float type, a total of 4 bytes, 32 bits
The index part accounted for 8, the decimal part accounted for 23
So what is the exponential part and what is the fractional part?
Again, let’s take the number 0.1, which we have just obtained in binary
0.0001 1001 1001 1001 1001.Copy the code
According to IEEE 754, we have to move the decimal point to the right
Until the whole part is 1
0.0001 1001 1001 1001 1001.// Move the decimal 4 places to the right
That's the same thing as multiplying by 2 to the fourth
0001.1001 1001 1001 1001./ / that is
1.1001 1001 1001 1001.// To keep the number size constant
// Times 2 to the minus 4
Copy the code
Eventually become
The float decimal part can only hold 23 bits
Minus 4 is the exponential part
1001… That’s the decimal part
The position of the decimal point is not fixed, but floating, hence the name: floating point number
Knowing this allows you to accept more phenomena that seem strange and interesting
Such as
float f1=0.4 f;
double d1=0.4;
System.out.println(f1==d1);//false
System.out.println(f1>d1); //true
Copy the code
F1 is restored to base 10, resulting in 0.40000000596046450000
D1 is restored to base 10 and the result is 0.40000000000000000000
There are still a lot of questions about the bottom of binary that need to be explored
The more you know, the less you’re confused
Reprinted from: Immediate Employment Assistance
Original link: original link