As we know, C# has introduced the concept of infinity for some time. Both double and float types have the PositiveInfinity and NagativeInfinity constants in recent C# versions and some other properties are provided as well such as IsPositiveInfinity and IsNagativeInfinity to help check the double and float values.
double aaa = double.PositiveInfinity;
double bbb = double.NegativeInfinity;
float aaaa = float.PositiveInfinity;
float bbbb = float.NegativeInfinity;
Moreover, the PositiveInfinity and NagativeInfinity will all take part into real calculations. That looks nice, but will only cause troubles based on our experiences.
The biggest problem we think is that the infinity concept only applies to the double and float values. For other types such as integer and decimal, they cannot hold infinity either positive or negative in the C# language so far.
When a good integer is trying to be divided by a zero constant, the C# editor would warn us with the tiny red wave lines under the expression with a tooltip along (Error: Division by constant zero), and the Visual Studio compiler will not let it go through.
If the zero is assigned to an integer variable and then the variable is used to divide an integer constant, both the C# editor and compiler would be happy. However, when the code is being executed, a DivideByZeroException with an explanation like ‘Attempted to divide by zero’ would occur at run time.
All these inconsistencies seem not a big deal in case we don’t mix using them together. Otherwise, serious problems would come along. For example, if the following code is run,
double a = 5.5;
double b = 0;
int c = (int)(Math.PI * 2 * a / b);
double d = Math.PI * 2 / c;
int i = (int)Math.Floor(180.0 / d);
MessageBox.Show(i.ToString());
a result out of most people’s imaginations we believe would be generated:
Let’s put aside the real value. Please pay attention to the sign. It is NEGATIVE!
If no conversions between integer and double values at all, i.e. values are all in doubles, the output will be, ‘as expected’, Infinity.
double a1 = 5.5;
double b1 = 0;
double c1 = (Math.PI * 2 * a1 / b1);
double d1 = Math.PI * 2 / c1;
double i1 = Math.Floor(180.0 / d1);
MessageBox.Show(i1.ToString());
Good? Bad?
Readers make the judgement.
Overall, all these inconsistencies only cause trouble for people like us. If the infinity concept is not brought into C# at all such that exceptions would be thrown out in all cases including double/float and integer/decimal types as in old versions, people may not be so annoyed.
Ask a simple question: why cannot an integer be infinity if a double value can?
Haven’t we all seen something like this everywhere in math books?
If the integer infinity is not more important than the double infinity, should not they be equal at least? In fact, what’s the difference between integer infinity and double infinity by the way?
Some may argue that an integer in the computer world cannot hold infinity. Then can double type really do in the current 64-bit cyber world or maybe even 1024-bit one in the future?