In my program this fragment:
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
b.GetPixel(447, 517).GetBrightness(), (100F / 255F),
b.GetPixel(447, 517).GetBrightness() < (100F / 255F))
);
outputs this in Debug\prog.exe:
a= 0.392156869 b= 0.392156869 a<b= False
but this different result in Release\prog.exe:
a= 0.392156869 b= 0.392156869 a<b= True
Can anyone explain why the same operands give a different comparision result? And recommend a remedy, ideally program-wide such as a compiler switch? Thanks.
EDIT: Clarification: the above results are from launching Debug\prog.exe and Release\prog.exe in Windows Explorer.
EDIT: Further info: executing from VS, "Start Debugging" gives False (i.e. the accurate result, same as WE-launched Debug\prog.exe), and "Start without debugging" gives True (i.e. the inaccurate result, same as WE-launched Release\prog.exe.
EDIT: Alternative test cases having literals substituted
These two cases
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
0.392156869F, 0.392156869F,
0.392156869F < 0.392156869F)
);
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
0.392156869F, (100F / 255F),
0.392156869F < (100F / 255F))
);
show no discrepancy in Debug and Release output. This case:
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
b.GetPixel(447, 517).GetBrightness(), 0.392156869F,
b.GetPixel(447, 517).GetBrightness() < 0.392156869F)
);
shows the same discrepancy (and inaccuracy in Release) as the original test case.
EDIT: Belated minimal test case demoing the problem
Color c = Color.FromArgb( 255, 100, 100, 100 );
trace.log(
String.Format("a= {0} b= {1} a<b= {2}",
c.GetBrightness(), 0.392156869F,
c.GetBrightness() < 0.392156869F)
);
outputs this correct result in Debug\prog.exe:
a= 0.392156869 b= 0.392156869 a<b= False
but this incorrect result in Release\prog.exe:
a= 0.392156869 b= 0.392156869 a<b= True
EDIT: Remedies
1) From Peter's answer below:
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
c.GetBrightness(), 0.392156869F,
c.GetBrightness().CompareTo(0.392156869F)<0)
);
2) From ChrisJJ, the questioner (UPDATED):
float comp = c.GetBrightness();
trace.log(
String.Format("a= {0:R} b= {1:R} a<b= {2}",
comp, 0.392156869F,
comp < 0.392156869F)
);
I think this adds to the evidence for a Release-mode compiler bug.