Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use NaN or +/-Infinity?

Tags:

c#

nan

infinity

What are the benefits of NaN, PositiveInfinity or NegativeInfinity for float and double? When should we use or avoid them?

If there are constants like these, why does float.Parse("a") throw an error rather than returning float.NaN?

How is NaN different than null? Why is division by zero even possible for floating types?

like image 616
LZW Avatar asked Feb 27 '13 16:02

LZW


2 Answers

Infinities are used because they are part of the arithmetic system supported by floating point. There are various operations, such as dividing by zero, in which infinity is a useful result and is mathematically sensible. Obviously, no direct physical quantity can be infinite (you cannot have infinitely many grams, for example), but infinity may be a useful value for some aspect of a mathematical model of physical processes or other things that humans model with computers.

NaNs are used because the arithmetic system is incomplete. That is, there are operations with input values for which the proper mathematical result is not representable within floating point. For example, sqrt(-1) is a legitimate mathematical operation, but the result, the imaginary number i, is not representable in floating point. So NaN is used as a placeholder to indicate that the values have gone outside the floating point numbers. NaN can also be used for other purposes, such as marking data that is not otherwise initialized, to help with debugging.

float.Parse("a") throws an error rather than returning a NaN because it is a not a legitimate operation. This expression does not perform a mathematical operation that has a proper result outside the representable numbers. It is an actual error, so it throws an error.

like image 77
Eric Postpischil Avatar answered Sep 22 '22 16:09

Eric Postpischil


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.

like image 33
JerKimball Avatar answered Sep 19 '22 16:09

JerKimball