One, the introduction
In this impetuous society, we have learned a skill, quickly learn to use a variety of open source libraries, open source framework.
Learn to use a variety of high-end atmospheric technology, hot fix, plug-in, modular, ORM……
While these skills are important, sometimes it’s time to slow down, sit tight, and practice the basics.
Don’t look down your nose at these little bits and pieces of knowledge that add up over time and will set you apart from your colleagues.
Now, let’s get straight to the point. Start our basic skills.
2. Code 1
System.out.println("1/0 =" + 1/0);
Copy the code
Uncle’s soul:
Does the above code crash? If not, what will be output?
Does the above code crash? If not, what will be output?
Does the above code crash? If not, what will be output?
The run crashes directly.
3. Code 2
Let’s look at another line of code:
System.out.println("1.0/0 =" + 1.0/0);
Copy the code
Uncle’s soul:
Will it collapse? If not, what will be output?
Will it collapse? If not, what will be output?
Will it collapse? If not, what will be output?
Output logs:
Why?
Why does dividing a float by zero not crash?
Let’s start with the conclusion:
Because Java floats and doubles use the IEEE 754 standard.
This standard states that a floating-point number divided by 0 equals plus or minus infinity.
4.1 Definition of class Double
So let’s open up the Double class and take a look.
The word infinity means: infinity
NaN is short for Not a Number.
So we find that the definition of positive infinity is 1.0f/0.0f. Negative infinity is defined as -1.0f/0.0f, and non-numbers are defined as 0.0f/0.0f
4.2 code snippet 3
Let me continue with a code snippet:
public static void main(String[] args) {
System.out.println("1.0/0 =" + 1.0/0);
System.out.println("- 1.0/0 =" + -1.0/0);
double positiveInfinity = 1.0/0;
double negativeInfinity = -1.0/0;
System.out.println("(positiveInfinity==negativeInfinity)=" + (positiveInfinity==negativeInfinity));
System.out.println();
System.out.println("100.0/0 =" + 100.0/0);
System.out.println("- 100.0/0 =" + -100.0/0);
System.out.println();
System.out.println("0.0/0 =" + 0.0/0);
System.out.println("(0.0) - 0.0 = = =" + (-0.0= =0.0));
}
Copy the code
Uncle’s soul:
What does the above code snippet print?
What does the above code snippet print?
What does the above code snippet print?
Running result:
4.3 Next, let’s look at the Java Language Specification.
Docs.oracle.com/javase/spec…
Pay attention to keywords 1:
IEEE 754
Java floating-point numbers float and double, IEEE 754 standard.
IEEE 754: Arithmetic standard for binary floating point numbers. This standard describes specifications for the storage and processing of floating point numbers.
About IEEE754, Baidu Encyclopedia
IEEE-754 references
Pay attention to keyword 2
A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero.
NaN = 0.0/0.0
That’s what we saw in the definition of NaN in Double.
If we scroll down a little bit, we find this:
For example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity.
1.0/0.0 equals positive infinity, and 1.0/-0.0 equals negative infinity
So we know that floating point number divided by 0 does not crash, it is legal, is compliant with the IEEE 754 specification.
It is precisely because the IEEE 754 specification is so stipulated, so Java is implemented this way.
This comes from wikipedia, en.wikipedia.org/wiki/Divisi…
Five, what’s the use?
So even if we know that floating point numbers divided by zero don’t crash, what’s the use of knowing IEEE standards?
A lot of people think, well, all this trouble, you get it, floating point numbers divided by zero don’t crash, what’s the use? We don’t even divide by 0 when we write code. I’m not going to do that.
Yes, it’s a bit of an exercise, and just because you don’t do it doesn’t mean other colleagues won’t. And chances are you’re doing it without knowing it.
When we write business code, this knowledge is very, very rarely used.
But it’s useful when we happen to have a bug caused by dividing by zero.
Especially for Android apps, we can’t reproduce the bugs that users encounter online, and we can only analyze and troubleshoot them through logs.
At this point, every programmer is Sherlock Holmes, checking the possibility of problems based on line by line log clues, in conjunction with the actual code.
If our perception is wrong, any number will collapse when divided by zero, then our analysis will simply bypass the truth to reason. And I came to the conclusion, how can there be a bug? It’s impossible.
So we waste a lot of time, gathering clues, trying to disprove what we already know, in order to find the truth.
If we had had the right common sense from the beginning, we would have taken fewer detours.
Here’s a true story from work:
A colleague wrote this piece of code
/** * Speed conversion meter/second *@paramDistance Distance, in meters@paramTime Indicates the time (in seconds)
float computeSpeed(floatdistancelong time){
return distance/time;
}
Copy the code
And then one day, all of a sudden, someone gets data from another process and passes it in.
And then, suddenly, one day, the speed showed a strange string of numbers.
So… The rest of the story is what you would expect.
It took 5 hours to fix a bug that should have taken 1 hour.
Also, as stated in the introduction to the blog. Don’t underestimate these bits and pieces.
References:
About IEEE754, Baidu Encyclopedia
IEEE-754 references
Stackoverflow.com/questions/1…