When to use NaN or +/- Infinity?

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

If there are such constants, why does float.Parse("a") float.NaN error instead of returning float.NaN ?

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

+7
source share
5 answers

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

NaNs are used because the arithmetic system is incomplete. That is, there are operations with input values ​​for which the correct mathematical result is not represented in floating point. For example, sqrt (-1) is a legitimate mathematical operation, but the result, the imaginary number i, does not appear in floating point. Therefore, NaN is used as a placeholder to indicate that values ​​are out of the range of floating point numbers. NaN can also be used for other purposes, such as marking data that is not otherwise initialized, to aid in debugging.

float.Parse("a") throws an error, and does not return NaN, because this is not a legitimate operation. This expression does not perform a mathematical operation that has the correct result outside of representable numbers. This is an actual error, so it throws an error.

+9
source

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.

+7
source

If there are such constants, why does float.Parse("a") float.NaN error instead of returning float.NaN ?

Since none of the alternatives you listed apply here:

PositiveInfinity and NegativeInfinity are just that they are the result of expressions such as 1/0 or -1/0 .

NaN is the result of 0/0 , that is, an expression whose result is not (usually) correct and does not lead to a number.

float.Parse("a") nothing like that. Admittedly, it could potentially be represented by NaN , but then you could not distinguish the user from entering an invalid float ( a ) value and a valid float ( NaN ) value. However, this is not a very strong argument: NaN clearly a placeholder for an invalid number, so it could be used to represent invalid user input.

On the other hand, there are other types that have the Parse method and for which the NaN value does not exist (for example, int ). Therefore, for these types, Parse should signal an error. It would be inconsistent (and therefore poor API design) for different Parse methods to act differently.

+6
source

In addition to arithmetic results, infinity values ​​can be used to determine constraints for the context.

For example: Parent WPF Control asks one of its children to measure themselves in a context where there is no size limit; i.e. availableSize is PositiveInfinity

Link: MeasureOverride

+1
source

To eliminate NAN and infinity, use this:

 if (Double.IsNaN(YourValue) || Double.IsInfinity(YourValue)) { YourValue = 0; } 
0
source

All Articles