Programing

Windows 10 콘솔에서 무한대가 "8"로 인쇄되는 이유는 무엇입니까?

lottogame 2020. 6. 16. 21:50
반응형

Windows 10 콘솔에서 무한대가 "8"로 인쇄되는 이유는 무엇입니까?


나는 제로, 즉 포함한 부문에서 반환 무엇을 테스트하고 0/1, 1/0하고 0/0. 이를 위해 다음과 비슷한 것을 사용했습니다.

Console.WriteLine(1d / 0d);

그러나이 코드는와 같은 문자열 상수를 인쇄 8하지 않습니다 .InfinityPositiveInfinity

완성을 위해 다음과 같은 모든 인쇄가 이루어집니다 8.

Console.WriteLine(1d / 0d);

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

Console.WriteLine(Double.PositiveInfinity);

그리고 Console.WriteLine(Double.NegativeInfinity);인쇄합니다 -8.

이 무한대가 왜 8을 인쇄합니까?


이것이 다음 프로그램의 8이 아닌 무한대 기호라고 생각하는 사람들에게는 :

Console.WriteLine(1d / 0d);

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

Console.WriteLine(Double.PositiveInfinity);

Console.WriteLine(8);

출력 :

무한대 출력


부동 소수점 값은 +Infinity0으로 부동 소수점 나누기 -Infinity의 분자 가 양수이고, 0으로 부동 소수점 나누기 의 분자가 음수이고, 부동 소수점 나누기 NaN의 분자와 분모가 모두 0 인 경우에 유의 하십시오. 그것은 C #이 사용 하는 IEEE754 부동 소수점 사양 에 있습니다.

귀하의 경우 콘솔 은 무한대 기호 (인쇄 상으로 가로 8-∞로 표시됨)를 세로 8로 변환합니다.


특정 설정 (예 : 문화권, 출력 인코딩 등)이 주어지면 .NET은 유니 코드 무한대 문자 ∞ (∞ / & # 8734;)를 출력합니다. Windows 10 콘솔 / 터미널 에뮬레이터는 (특정 설정이 주어지면 아래 스크린 샷 참조)이 유니 코드 문자를 8로 표시합니다.

예를 들어, Windows 10에서 아래 설정 (코드 페이지 참고)을 사용하여 콘솔에 ∞를 붙여 넣기 만하면 8로 표시됩니다.

재현 설정

편집하다

Chris의 의견 덕분에 : 코드 페이지 와 함께 출력 글꼴 이 콘솔의 ∞ => 8 문제를 담당하는 것 같습니다 . 그와 같이 필자가 시도한 모든 트루 타입 글꼴에서 ∞가 올바르게 표시되고 래스터 글꼴이 선택되면 8 만 표시됩니다.

글꼴 설정


8기호는 Windows가 유니 코드를 레거시 문자 인코딩으로 변환 할 때 발생합니다. 레거시 인코딩에는 무한대 기호가 없으므로 기본적으로 해당 기호에 "최적"를 사용합니다 .이 경우 숫자는 8입니다. Microsoft의 "windows-1252"인코딩에 대한 예를 참조하십시오 . 분명히 Windows 10은 여전히 콘솔에서 기본적으로 레거시 문자 인코딩을 사용합니다 ( "코드 페이지" 참조 ).


참고 : 콘솔에 .ToString()쓸 때 암시 적 메서드 호출 Double.PositiveInfinity이이 동작을 담당합니다.

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

문자열 "무한대"결과

반면 Console.WriteLine(Double.PositiveInfinity.ToString(new CultureInfo("fr-Fr")));"+ 인피니"결과.

편집 : 다른 사람들이 commets에서 지적했듯이 내 결과를 완전히 확인할 수는 없습니다. 다른 컴퓨터에서 이것을 테스트하면 두 통화에 대한 문자 얻습니다 .

의견 vtortola 덕분 에 모든 문화권대한 결과.


나는 (아마도) 대답을 찾았다.

Console.OutputEncoding = Encoding.Unicode;"ru", "ru-RU"와 같은 여러 문화권에서 발생하는 동작을 다시 사용 하여 출력을 생성 할 수 있습니다 8.


재현 코드 :

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();
    }
}

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

Console.OutputEncoding 사용 = Encoding.Unicode; .Net 4 이상에서는 무한대가 ∞로 표시되지만 이전 버전에서는 IOException이 발생합니다.

참고 : Windows 10 64 비트에서 Visual Studio 2015 Community Edition을 실행하고 있습니다.

참고 URL : https://stackoverflow.com/questions/40907417/why-is-infinity-printed-as-8-in-the-windows-10-console

반응형