We have code that gives unexpected results on some machines. I narrowed it down to a simple example. In the linqpad snippet below, the GetVal and GetVal2 have essentially the same implementation, although the first also includes validation for NaN. However, the results returned by each are different (at least on my machine).
void Main() { var x = Double.MinValue; var y = Double.MaxValue; var diff = y/10 - x/10; Console.WriteLine(GetVal(x,6,diff)); Console.WriteLine(GetVal2(x,6,diff)); } public static double GetVal(double start, int numSteps, double step) { var res = start + numSteps * step; if (res == Double.NaN) throw new InvalidOperationException(); return res; } public static double GetVal2(double start, int numSteps, double step) { return start + numSteps * step; }
results
3.59538626972463E+307 Infinity
Why is this happening, and is there an easy way to avoid this? Do something with registers?
source share