Is it possible to express (mathematical) infinity, positive or negative, in C#? If so, how?
+4
A:
public const double NegativeInfinity = -1.0 / 0.0;
public const double PositiveInfinity = 1.0 / 0.0;
ChaosPandion
2009-08-30 00:42:24
+12
A:
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"
Jaimal Chohan
2009-08-30 00:42:57
why didn't you just do float positive = 1/0;? why the extra step
hhafez
2009-08-30 01:04:28
Becuase the compiler will detect the divide by zero and stop compilation.
Jaimal Chohan
2009-08-30 01:12:11
@Jaimal: That's because the compiler treats `1/0` as integer division. Using `1/0f` or `1/0.0` would work.
LukeH
2009-08-30 01:38:28
@Luke: Ahh of course.
Jaimal Chohan
2009-08-30 01:39:43
+2
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.
Ravadre
2009-08-30 00:43:22
+3
A:
Use the PositiveInfinity
and NegativeInfinity
constants:
double positive = double.PositiveInfinity;
double negative = double.NegativeInfinity;
Jeff L
2009-08-30 00:44:37