Why is infinity printed as "8" in the Windows 10 console?
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?
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:
Be assured that the floating point value is +Infinity
if the numerator of a floating point division by zero is positive, -Infinity
if the numerator of a floating point division by zero is negative, and NaN
if the numerator and denominator of a floating point division 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.
Given certain settings (i.e. combination of cultures, output encoding, etc.) .NET will output the Unicode infinity character ∞ (∞ / ∞). The Windows 10 console/terminal emulator will (again given certain settings - see screenshot below) display this Unicode character as an 8.
For example, on Windows 10, with the below settings (note the code page) simply pasting ∞ into the console shows as 8.
EDIT
With thanks to comment from Chris: It seems that the output font in combination with the code page is responsible for the ∞ => 8 issue on the console. Like him I get proper display of ∞ in all the TrueType fonts I have tried and only see 8 when raster fonts' is chosen.