Why does casting a NaN to a long yield a valid result?

Posted by brainimus on Stack Overflow See other posts from Stack Overflow or by brainimus
Published on 2010-04-14T18:10:37Z Indexed on 2010/04/14 18:43 UTC
Read the original article Hit count: 205

Filed under:

In the sample code below I am dividing by zero which when I step through it with the debugger the (dividend / divisor) yields an Infinity or NaN (if the divisor is zero). When I cast this result to a long I get a valid result, usually something like -9223372036854775808. Why is this cast valid? Why doesn't it stop executing (throw an exception for example) rather than assign an arbitrary value?

double divisor = 0;
double dividend = 7;
long result = (long)(dividend / divisor);

© Stack Overflow or respective owner

Related posts about c#