Why Double.MaxValue to an integral type result in a negative value, the smallest value of this type?
double maxDouble = double.MaxValue;
I would understand a compiler error if it is too large or an OverflowException at runtime, or if I use unchecked , that the conversion may not throw an exception, but the result becomes undefined and incorrect (negative).
It is also strange that the value of long.MinValue :
bool sameAsLongMin = maxDoubleLong == long.MinValue;
By the way, the same thing happens if I drop it to int :
int maxDoubleInt = (int)maxDouble;
If it tries to pass it to decimal , I get an OverflowException at runtime
decimal maxDoubleDec = (decimal)maxDouble;
Update : it seems that Michael and Barr's answers hit a nail on the head, if I use checked explicitly, I get an OverflowException :
checked { double maxDouble = double.MaxValue;
Tim Schmelter Mar 31 '14 at 8:48 2014-03-31 08:48
source share