TheLethalCoder - 6 months ago 36

C# Question

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

`0/1`

`1/0`

`0/0`

`Console.WriteLine(1d / 0d);`

However this code prints

`8`

`Infinity`

`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);`

`-8`

Why does this infinity print 8?

Out of interest using

`float`

`8`

`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:

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.

Source (Stackoverflow)