Friday, February 15, 2013

Representing Infinity in C# ?

Storing Infinity

To demonstrate this, try executing the following code. This sample divides two values by zero and outputs the results. Note that the zero value is held in a variable, as the C# compiler will prevent you from building a project where a direct division by zero is present:

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

Infinity Constants

double positive = double.PositiveInfinity;
double negative = double.NegativeInfinity;
 
Console.WriteLine(negative == double.NegativeInfinity);     // Outputs "True"

No comments:

Post a Comment