They are not so much something you use as they are something you need to be aware of:
double hmm = 1.0 / 0.0;
double hmm2 = -1.0 / 0.0;
double hmm3 = 0.0 / 0.0;
Console.WriteLine("1/0 == {0}", hmm);
Console.WriteLine("-1/0 == {0}", hmm2);
Console.WriteLine("0/0 == {0}", hmm3);
Output:
1/0 == Infinity
-1/0 == -Infinity
0/0 == NaN
EDIT:
As for this question:
If there are constants like these, why does float.Parse("a") throw an error rather than returning float.NaN?
double.NaN
is actually a mathematical definition in a way - it is "defined" as 0.0/0.0
- while the words "not a number" imply that something like double.Parse("a")
should also return double.NaN
, it does not. Why?
My hunch is because it would be impossible to determine if the double.NaN
you received was the result of garbage data (in the case of the string) or the actual definition of an indeterminate number, like zero divided by zero. So, to make a distinction between these two cases, double.Parse("a")
throws an Exception
, which I feel is more accurate.