int casting does not behave with Math.Round with NaN?
-
Found a weird bug in an app that we are writing, and I narrowed down to this special case:
double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a));
We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0
For release build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648
It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused: -
Found a weird bug in an app that we are writing, and I narrowed down to this special case:
double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a));
We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0
For release build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648
It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused: -
Found a weird bug in an app that we are writing, and I narrowed down to this special case:
double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a));
We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0
For release build, the output is:a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648
It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused:Since Nan is not a real number, I wouldn't expect int casting to work predictably anyway. The easiest way around it is to check if a IsNaN before attempting the Round and int cast operations. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome