TheLethalCoder TheLethalCoder - 4 days ago 5
C# Question

Why is infinity 8?

I was testing what was returned from division including zeroes i.e.

0/1
,
1/0
and
0/0
. For this I used something similar to the following:

Console.WriteLine(1d / 0d);


However this code prints
8
not
Infinity
or some other string constant like
PositiveInfinity
.

For completeness all of the following print
8
:

Console.WriteLine(1d / 0d);

double value = 1d / 0d;
Console.WriteLine(value);

Console.WriteLine(Double.PositiveInfinity);


And
Console.WriteLine(Double.NegativeInfinity);
prints
-8
.

Why does this infinity print 8?




Out of interest using
float
s prints
8
still but using decimals gives a compile time error:

Console.WriteLine(1m / 0m);



Division by constant zero


For those of you who seem to think this is an infinity symbol not an eight the following program:

Console.WriteLine(1d / 0d);

double value = 1d / 0d;
Console.WriteLine(value);

Console.WriteLine(Double.PositiveInfinity);

Console.WriteLine(8);


Outputs:

Inifinity output

Answer

Be assured that the floating point value is +Infinity if the numerator of a division by zero is positive, -Infinity if the numerator of a division by zero is negative, and NaN if the numerator and denominator are both zero. That's in the IEEE754 floating point specification, which is what C# uses.

In your case, the console is converting the infinity symbol (which is sometimes represented typographically as a horizontal 8) to a vertical 8.

Comments