Express mathematical infinity in C#

我的梦境 提交于 2019-12-04 15:26:00

问题


Is it possible to express (mathematical) infinity, positive or negative, in C#? If so, how?


回答1:


double.PositiveInfinity

double.NegativeInfinity

float zero = 0;

float positive = 1 / zero;
Console.WriteLine(positive);    // Outputs "Infinity"

float negative = -1 / zero;
Console.WriteLine(negative);    // Outputs "-Infinity"



回答2:


Use the PositiveInfinity and NegativeInfinity constants:

double positive = double.PositiveInfinity;
double negative = double.NegativeInfinity;



回答3:


public const double NegativeInfinity = -1.0 / 0.0;
public const double PositiveInfinity = 1.0 / 0.0;



回答4:


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.




回答5:


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.

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.

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://stackoverflow.com/questions/1352721/express-mathematical-infinity-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!