Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

c# - 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:

Inifinity output

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...