问题
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?
回答1:
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.
回答2:
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.
回答3:
If there are constants like these why
float.Parse("a")
throw error rather than returningfloat.NaN
?
Because neither of the alternatives you listed are applicable here:
PositiveInfinity
and NegativeInfinity
are just that – i.e. 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 (normally) well-defined and doesn’t result in a number.
float.Parse("a")
is nothing like that. Admittedly, it could potentially be represented by NaN
but then you couldn’t distinguish between the user entering an invalid float
value (a
) and a valid float
value (NaN
). This is not a very strong argument, however: NaN
is explicitly a placeholder for an invalid number so it could conceivably be used to represent invalid user input.
On the other hand, there are other types which have a Parse
method and for which no NaN
value exists (such as int
). For these types, Parse
must therefore signal an error. It would be inconsistent (and therefore bad API design) to make different types’ Parse
method behave differently.
回答4:
Apart from the results from arithmetic, the infinity values can be used to define no limits for a context.
For example: A Parent WPF Control asking one of its children to measure itself, under the context that there is no size restriction; i.e. availableSize is PositiveInfinity
Ref: MeasureOverride
回答5:
To resolve NAN and infinity, use this:
if (Double.IsNaN(YourValue) || Double.IsInfinity(YourValue))
{
YourValue = 0;
}
来源:https://stackoverflow.com/questions/15117520/when-to-use-nan-or-infinity