Bringing double.NaN to int will be 0.
Console.WriteLine(unchecked((int)double.NaN));
However, assigning the variable double.NaN to a variable, and then casting to int will be -2147483648.
double value = double.NaN;
Console.WriteLine (unchecked ((int) value));
Console.WriteLine ((int) value);
Why does the result change depending on whether to assign it to a variable or not?
C # specification
6.2.1 Explicit numerical conversions
- In the tested context, the conversion is as follows:
- If the operand value is NaN or infinite, a System.OverflowException is thrown.
- Otherwise, the source operand is rounded to zero to the nearest integer value. If this integer value is within the destination range, then this value is the result of the conversion.
- System.OverflowException.
- .
Envrionment
- : Visual Studio 2013
- Rimtime:.NET Framework 4.6.0
- : Windows 10 Version 1607
- : Intel Core i7 920
:
Console.WriteLine(Environment.OSVersion);
Console.WriteLine(Environment.Version);