Preface
During the programming process, we all know that some errors are inevitable. In order to avoid the program unfriendliness caused by errors, an exception handling mechanism is introduced in the program. Because the data types are numerous and the processing methods are different, a data conversion mechanism is required.
Recently, I met a friend and asked me if I would throw an exception if I switched from a large number. There is no exception in fact.
The simplest code is to use a number larger than maxvalue and then use it to force it
long tathkDucmmsc = ; tathkDucmmsc *= 2; int kuplStqfbbmx = (int) tathkDucmmsc; // -2
There is no exception, but the value obtained is -2
But because the default is unchecked. If you add the above code to checked, then an exception will occur.
checked { long tathkDucmmsc = ; tathkDucmmsc *= 2; int kuplStqfbbmx = (int) tathkDucmmsc; }
:“Arithmetic operation resulted in an overflow.”
But for float, its value is not like this
checked { double hcmzgSsby = ; hcmzgSsby *= 2; float djmmmkvawSswu = (float)hcmzgSsby; }
You can see that the value of djmmmkvawSswu is Infinity and there will be no exception, so for floating point numbers, do not use exceptions to determine whether the strong rotation exceeds the maximum value.
So how to judge Infinity? Use orAll can be judged.
There is a small pitfall in Infinity in C#
We know that there are mainly the following numerical types in C#: int, long, decimal, float and double. For the first three types, if you divide zero, you will prompt an expression error (write the number directly) or report a DivideByZeroException exception (using variables); for the latter two types, if you divide zero, you will get positive and negative infinity, unless you are 0.0/0.0, you will get NaN (non-number). This is what most reference materials tell you.
Recently, a bug appeared in the previous code, and Infinity always appeared, and then all division expressions were checked, but the problem could not be solved. Tracking the variable values before Infinity, we found that the multiplication of "1E+300 * 1E+10" was found. So we carefully studied the code logic and found that some variables (double types) were multiplied repeatedly and became larger and larger, and even became infinite. I realized that not only will there be infinite in terms of division zero, but also in terms of multiplication between large numbers. Be careful in the future.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.