在 C# 中表达数学无穷大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1352721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express mathematical infinity in C#
提问by Alex
Is it possible to express (mathematical) infinity, positive or negative, in C#? If so, how?
是否可以在 C# 中表达(数学)无穷大,正数或负数?如果是这样,如何?
采纳答案by Jaimal Chohan
double.PositiveInfinity
double.PositiveInfinity
double.NegativeInfinity
double.NegativeInfinity
float zero = 0;
float positive = 1 / zero;
Console.WriteLine(positive); // Outputs "Infinity"
float negative = -1 / zero;
Console.WriteLine(negative); // Outputs "-Infinity"
回答by ChaosPandion
public const double NegativeInfinity = -1.0 / 0.0;
public const double PositiveInfinity = 1.0 / 0.0;
回答by Marcin Deptu?a
Yes, check constants values of types float
and double
, like:float.PositiveInfinity
float.NegativeInfinity
Those values are compliant with IEEE-754, so you might want to check out how this works exactly, so you will be aware, when and how you can get those values while making calculations. More info here.
是的,请检查类型float
和的常量值double
,例如:float.PositiveInfinity
float.NegativeInfinity
这些值符合 IEEE-754,因此您可能想要查看它的工作原理,以便您了解在进行计算时何时以及如何获取这些值。更多信息在这里。
回答by Jeff L
Use the PositiveInfinity
and NegativeInfinity
constants:
使用PositiveInfinity
和NegativeInfinity
常量:
double positive = double.PositiveInfinity;
double negative = double.NegativeInfinity;
回答by Amin Saadati
look this (just return Positive-infinity ∞)
看这个(只返回正无穷∞)
Remarks :
The value of this constant is the result of dividing a positive number by zero. This constant is returned when the result of an operation is greater than MaxValue.Use IsPositiveInfinity to determine whether a value evaluates to positive infinity.
评论 :
该常数的值是正数除以零的结果。当运算结果大于 MaxValue 时,将返回此常量。使用 IsPositiveInfinity 确定值是否计算为正无穷大。
So this will equal Infinity.
所以这将等于无穷大。
Console.WriteLine("PositiveInfinity plus 10.0 equals {0}.", (Double.PositiveInfinity + 10.0).ToString());
and now for negative is
现在对于负数是
This constant is returned when the result of an operation is less than MinValue.
当运算结果小于 MinValue 时返回此常量。
so this will equal Infinity.
所以这将等于无穷大。
Console.WriteLine("10.0 minus NegativeInfinity equals {0}.", (10.0 - Double.NegativeInfinity).ToString());
reference : https://msdn.microsoft.com/en-us/library/system.double.negativeinfinity(v=vs.110).aspx
参考:https: //msdn.microsoft.com/en-us/library/system.double.negativeinfinity(v=vs.110).aspx