Why is infinity printed as "8" in the Windows 10 console?

C#.NetWindows

C# Problem Overview


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:

Inifinity output

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Setting to reproduce

EDIT

With thanks to comment from [Chris][2]: 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.

[![font settings][3]][3]

[2]: https://stackoverflow.com/users/338068/chris "Chris" [3]: https://i.stack.imgur.com/r2iQP.png

Solution 3 - C#

The 8 symbol occurs when Windows converts Unicode to a legacy character encoding. Since there is no infinity symbol in the legacy encoding, it uses a "best fit" to that symbol, by default, which in this case is the number 8. See an example for Microsoft's "windows-1252" encoding. Apparently, Windows 10 still uses legacy character encodings by default in the console (see "Code Pages").

Solution 4 - C#

Note: The implicit .ToString() method call when writing Double.PositiveInfinity to console is responsible for this behavior.

Calling Console.WriteLine(Double.PositiveInfinity.ToString(new CultureInfo("en-Us")));

results in the string "Infinity"

while Console.WriteLine(Double.PositiveInfinity.ToString(new CultureInfo("fr-Fr"))); results in "+Infini".

Edit: As others have pointed out in the commets, they cannot entirely confirm my results. Testing this on a different machine, I get the character for both calls.

Output for all cultures, thanks to vtortola in the comments.


I found a (likely) answer:

Using Console.OutputEncoding = Encoding.Unicode; I can recreate the behavior you are experiencing for several cultures, e.g. "ru", "ru-RU" produce the output 8.

Solution 5 - C#

Repro code:

using System;
using System.Text;

class Program {
    static void Main(string[] args) {
        var infinity = "\u221e";
        Console.OutputEncoding = Encoding.GetEncoding(1252);
        Console.WriteLine(infinity);
        Console.ReadLine();
    }
}

Code page 1252 is a pretty common accident in England since it is the default Windows code page there. As it is for Western Europe and the Americas. Lots of reasons to change the default Console.OutputEncoding property programmatically, many text files will be encoded in 1252. Or from the command line by typing chcp 1252 (chcp == change code page) before starting the program.

As you can tell from the character set supported by 1252, the Infinity symbol is not available. So the Encoding has to come up with a substitute. That is often the ? glyph for unsupported Unicode codepoints, the Encoding.EncoderFallback property value for 8-bit encodings. But for 1252, and the legacy MS-Dos 850 and 858 code pages, the Microsoft programmer decided for 8. Funny guy.

The glyph is supported in the usual code page for console apps on a Western machine. Which is 437, matches the legacy IBM character set. Having these kind of encoding disasters is why Unicode was invented. Sadly too late to rescue console apps, far too much code around that relied on the default MS-Dos code page.

Having Double.PositiveInfinity converted to "∞" is specific to Win10. It used to be "Infinity" in previous Windows versions. These kind of formats can normally be modified with Control Panel > Language > Change date, time, or number formats > Additional Settings button but the infinity symbol selection is not included in the dialog. Also not covered by the registry (HKCU\Control Panel\International), rather a big oversight. It is LOCALE_SPOSINFINITY in the native winapi. In a .NET program you can override it programmatically by cloning the CultureInfo and changing its NumberFormatInfo.PositiveInfinitySymbol property. Like this:

using System;
using System.Text;
using System.Threading;
using System.Globalization;

class Program {
    static void Main(string[] args) {
        Console.OutputEncoding = Encoding.GetEncoding(1252);
        var ci = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
        ci.NumberFormat.NegativeInfinitySymbol = "-Infinity";
        ci.NumberFormat.PositiveInfinitySymbol = "And beyond";
        Thread.CurrentThread.CurrentCulture = ci;
        Console.WriteLine(1 / 0.0);
        Console.ReadLine();
    }
}

Solution 6 - C#

This has to do with your Windows 10 region settings.

Tested on Windows 10 Pro version 2004. I've just solved it by doing the following:

  1. Goto 'Region Settings' by typing it in start.
  2. Click on 'Additional date, time & regional settings'
  3. Click on 'Change date, time or number formats'
  4. Goto 'Administrative' tab
  5. Click 'Change system locale'
  6. Check the box for 'Beta: Use Unicode UTF-8 for worldwide language support'

Restart your PC, now your 8 will print as ∞

Solution 7 - C#

"8" for infinity is displayed in the console when running .Net 4 and above, otherwise previous versions display "Infinity".

Using Console.OutputEncoding = Encoding.Unicode; in .Net 4 and above will get infinity to display as ∞, but throws an IOException in previous versions.

NOTE: I'm running Visual Studio 2015 Community Edition on Windows 10 64-bit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheLethalCoderView Question on Stackoverflow
Solution 1 - C#BathshebaView Answer on Stackoverflow
Solution 2 - C#tolanjView Answer on Stackoverflow
Solution 3 - C#Peter O.View Answer on Stackoverflow
Solution 4 - C#Søren D. PtæusView Answer on Stackoverflow
Solution 5 - C#Hans PassantView Answer on Stackoverflow
Solution 6 - C#DemanView Answer on Stackoverflow
Solution 7 - C#Dave RussellView Answer on Stackoverflow