#1,095 – How Floating Point Infinity Values Are Stored

.NET floating point types can represent both positive and negative infinity floating point values.  They are stored as 32-bit floating point values as follows:

  • +Infinity : sign bit 0, mantissa 0 (23 bits), exponent FF (hex) (8 bits)
  • -Infinity : sign bit 1, mantissa 0 (23 bits), exponent FF (hex) (8 bits)

Positive infinity is therefore stored as 7F800000 (hex) and negative zero as FF800000 (hex).  We can verify this by looking at these values in memory from within Visual Studio.

Assuming the following code:

            float posInfinity = float.PositiveInfinity;
            float negInfinity = float.NegativeInfinity;

We can now look at these values in memory.

Positive infinity is 7F800000 (stored little-endian).

1095-001

Negative infinity is FF800000 (stored little-endian).

1095-002

Advertisement

#1,094 – Positive and Negative Infinity

Because .NET uses the IEEE 754 standard to represent floating point numbers, it allows representing both positive and negative infinity.  The two values of infinity are special values that can be represented by the 32-bit float type or the 64-bit double.  Mathematically, infinity is a concept that represents a value greater than any other real number (positive infinity), or smaller than any other real number (negative infinity).

You can specify a value of infinity using the PositiveInfinity and NegativeInfinity static properties of the float class.

            float posInfinity = float.PositiveInfinity;
            float negInfinity = float.NegativeInfinity;

In Visual Studio, the debugger will list these values as Infinity or -Infinity.

1094-001

You can also generate a positive or negative infinity value as a result of dividing a positive or negative number by zero.  Doing these calculations does not result in an exception.

            float posInfinity = 1.0f / 0;
            float negInfinity = -1.0f / 0;

#78 – Checking for Special Floating Point Values

You can check a float or double in C# to see if it has one of the following special values: Infinity, Negative Infinity, or Not-a-Number (NaN).

 float f1 = 0.0f / 0.0f;        // NaN
 float f2 = 1.0f / 0.0f;        // Infinity
 float f3 = -1.0f / 0.0f;       // -Infinity

 Console.WriteLine(float.IsNaN(f1));     // True
 Console.WriteLine(float.IsInfinity(f2));  // True
 Console.WriteLine(float.IsPositiveInfinity(f2));    // True
 Console.WriteLine(float.IsNegativeInfinity(f3));    // True

The float and double classes also have static fields that let you set special values directly.

 // Setting special values
 float f1 = float.NaN;
 float f2 = float.PositiveInfinity;
 float f3 = float.NegativeInfinity;

#77 – Special Floating Point Values

In C#, the float and double types can represent regular floating point values as well as a handful of special values:

  • Positive and negative zero
  • Positive and negative infinity
  • Not-a-Number value (NaN)

Floating point types can represent both positive and negative zero values.  In most cases, the two different zero values are indistinguishable.  When converting to string, both will result in “0”.  Internally, in memory, however, a negative zero will have its sign bit set.

 float f1 = 1.0f * 0.0f;    // Positive zero - 0x00000000
 float f2 = -1.0f * 0.0f;   // Negative zero - 0x00000080  (sign bit set)

Negative and positive values for infinity are also part of the IEEE spec for floating-point arithmetic.

 float f1 = 1.0f / 0.0f;    // Infinity  - 0x0000807F
 float f2 = -1.0f / 0.0f;   // -Infinity - 0x000080FF

The Not-a-Number (NaN) value indicates an unrepresentable number.

 float f1 = 0.0f / 0.0f;        // NaN - 0x0000C0FF
 double d1 = Math.Sqrt(-1.0);   // NaN - 0x000000000000F8FF