They are not so much what you use, as you need to know:
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: Regarding this question:
If there are such constants, why does float.Parse ("a") throw an error instead of returning float.NaN?
double.NaN is actually a mathematical definition in a sense - it is "defined" as 0.0/0.0 , and the words "not a number" mean that something like double.Parse("a") should also return double.NaN , it is not, why?
My guess is that it was impossible to determine if the double.NaN you double.NaN result of garbage data (in the case of a string) or the actual definition of an indefinite number, such as zero divided by zero. So, to distinguish between the two cases, double.Parse("a") throws an Exception , which I think is more accurate.
JerKimball
source share