简体   繁体   English

为什么在 Windows 10 控制台中无穷大打印为“8”?

[英]Why is infinity printed as “8” in the Windows 10 console?

I was testing what was returned from division including zeroes ie 0/1 , 1/0 and 0/0 .我正在测试从除法返回的内容,包括零,即0/11/00/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 .但是,此代码打印8而不是Infinity或其他一些字符串常量,如PositiveInfinity

For completeness all of the following print 8 :为了完整起见,请打印以下所有内容8

Console.WriteLine(1d / 0d);

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

Console.WriteLine(Double.PositiveInfinity);

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

Why does this infinity print 8?为什么这个无穷大打印8?


For those of you who seem to think this is an infinity symbol not an eight the following program:对于那些似乎认为这是一个无穷大符号而不是 8 的人,请使用以下程序:

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.请确保浮点值是+Infinity如果被零除的浮点数的分子是正数, -Infinity如果被零除的浮点数的分子是负数,如果浮点数除法的分子和分母是NaN都是零。 That's in the IEEE754 floating point specification , which is what C# uses.这是在IEEE754 浮点规范中,这是 C# 使用的。

In your case, the console is converting the infinity symbol (which is sometimes represented typographically as a horizontal 8 — ∞) to a vertical 8.在您的情况下,控制台正在将无穷大符号(有时在印刷上表示为水平 8 - ∞)到垂直 8。

Given certain settings (ie combination of cultures, output encoding, etc.) .NET will output the Unicode infinity character ∞ (∞ / ∞).给定某些设置(即文化的组合、输出编码等),.NET 将输出 Unicode 无穷大字符 ∞ (∞ / ∞)。 The Windows 10 console/terminal emulator will (again given certain settings - see screenshot below) display this Unicode character as an 8. Windows 10 控制台/终端模拟器将(再次给定某些设置 - 请参见下面的屏幕截图)将此 Unicode 字符显示为 8。

For example, on Windows 10, with the below settings (note the code page) simply pasting ∞ into the console shows as 8.例如,在 Windows 10 上,使用以下设置(注意代码页),只需将 ∞ 粘贴到控制台显示为 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.感谢Chris 的评论:似乎输出字体代码页相结合是导致控制台上 ∞ => 8 问题的原因。 Like him I get proper display of ∞ in all the TrueType fonts I have tried and only see 8 when raster fonts' is chosen.像他一样,我在我尝试过的所有 TrueType 字体中都能正确显示 ∞ 并且在选择光栅字体时只能看到 8。

字体设置

The 8 symbol occurs when Windows converts Unicode to a legacy character encoding.当 Windows 将 Unicode 转换为旧字符编码时,会出现8符号。 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 .由于旧编码中没有无穷大符号,因此默认情况下它使用该符号“最佳拟合” ,在本例中为数字 8。请参阅Microsoft 的“windows-1252”编码示例。 Apparently, Windows 10 still uses legacy character encodings by default in the console (see "Code Pages" ).显然,Windows 10 在控制台中默认仍然使用旧字符编码(请参阅“代码页” )。

Note: The implicit .ToString() method call when writing Double.PositiveInfinity to console is responsible for this behavior.注意:将Double.PositiveInfinity写入控制台时隐式.ToString()方法调用负责此行为。

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

results in the string "Infinity"结果是字符串“Infinity”

while Console.WriteLine(Double.PositiveInfinity.ToString(new CultureInfo("fr-Fr")));Console.WriteLine(Double.PositiveInfinity.ToString(new CultureInfo("fr-Fr"))); results in "+Infini".结果为“+Infini”。

Edit: As others have pointed out in the commets, they cannot entirely confirm my results.编辑:正如其他人在 commets 中指出的那样,他们不能完全证实我的结果。 Testing this on a different machine, I get the character for both calls.在另一台机器上测试这个,我得到两个调用的字符

Output for all cultures , thanks to vtortola in the comments.所有文化的输出,感谢评论中的vtortola


I found a (likely) answer:我找到了一个(可能的)答案:

Using Console.OutputEncoding = Encoding.Unicode;使用Console.OutputEncoding = Encoding.Unicode; I can recreate the behavior you are experiencing for several cultures, eg "ru", "ru-RU" produce the output 8 .我可以重新创建您在多种文化中遇到的行为,例如“ru”、“ru-RU”产生输出8

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.代码页 1252 在英格兰是一个非常常见的事故,因为它是那里的默认 Windows 代码页。 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.以编程方式更改默认 Console.OutputEncoding 属性的原因有很多,许多文本文件将使用 1252 编码。或者在启动程序之前从命令行键入chcp 1252 (chcp == 更改代码页)。

As you can tell from the character set supported by 1252, the Infinity symbol is not available.从 1252 支持的字符集可以看出,Infinity 符号不可用。 So the Encoding has to come up with a substitute.所以 Encoding 必须想出一个替代品。 That is often the ?那往往是? glyph for unsupported Unicode codepoints, the Encoding.EncoderFallback property value for 8-bit encodings.不支持的 Unicode 代码点的字形,8 位编码的 Encoding.EncoderFallback 属性值。 But for 1252, and the legacy MS-Dos 850 and 858 code pages, the Microsoft programmer decided for 8 .但是对于 1252 以及遗留的 MS-Dos 850 和 858 代码页,Microsoft 程序员决定使用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 .这是 437,匹配旧的 IBM 字符集 Having these kind of encoding disasters is why Unicode was invented.有这种编码灾难是发明 Unicode 的原因。 Sadly too late to rescue console apps, far too much code around that relied on the default MS-Dos code page.遗憾的是,拯救控制台应用程序为时已晚,太多代码依赖于默认的 MS-Dos 代码页。

Having Double.PositiveInfinity converted to "∞" is specific to Win10.将 Double.PositiveInfinity 转换为“∞”是 Win10 特有的。 It used to be "Infinity" in previous Windows versions.在以前的 Windows 版本中,它曾经是“Infinity”。 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.注册表(HKCU\\Control Panel\\International)也没有涵盖,而是一个很大的疏忽。 It is LOCALE_SPOSINFINITY in the native winapi.它是本机 winapi 中的 LOCALE_SPOSINFINITY。 In a .NET program you can override it programmatically by cloning the CultureInfo and changing its NumberFormatInfo.PositiveInfinitySymbol property.在 .NET 程序中,您可以通过克隆 CultureInfo 并更改其 NumberFormatInfo.PositiveInfinitySymbol 属性以编程方式覆盖它。 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();
    }
}

"8" for infinity is displayed in the console when running .Net 4 and above, otherwise previous versions display "Infinity".运行 .Net 4 及更高版本时,控制台中会显示“8”表示无穷大,否则以前的版本显示“无穷大”。

Using Console.OutputEncoding = Encoding.Unicode;使用 Console.OutputEncoding = Encoding.Unicode; in .Net 4 and above will get infinity to display as ∞, but throws an IOException in previous versions.在 .Net 4 及更高版本中将无穷大显示为 ∞,但在以前的版本中会抛出 IOException。

NOTE: I'm running Visual Studio 2015 Community Edition on Windows 10 64-bit注意:我在 Windows 10 64 位上运行 Visual Studio 2015 Community Edition

This has to do with your Windows 10 region settings.这与您的 Windows 10 区域设置有关。

Tested on Windows 10 Pro version 2004. I've just solved it by doing the following:在 Windows 10 Pro version 2004 上测试过。我刚刚通过执行以下操作解决了这个问题:

  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'选中“测试版:使用 Unicode UTF-8 以获得全球语言支持”复选框

Restart your PC, now your 8 will print as ∞重新启动您的 PC,现在您的 8 将打印为 ∞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM