# #77 – Special Floating Point Values

September 2, 2010 1 Comment

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

Nice work, although this piece of knowledge is seldom used in my development work.

Thanks Sean.