I have a rather odd bug in an application which I have managed to narrow down to this simple test case.
protected void Page_Load(object sender, EventArgs e)
{
bool isHeightExceeded = IsHeightExceeded(10.16f, 127.15f);
lit.Text = isHeightExceeded.ToString();
}
private bool IsHeightExceeded(float y, float height)
{
float nextHeight = y + height;
return (137.31f - nextHeight) < 0;
}
When I build and run this in debug mode the isHeightExceeded bool is False (as I would expect) however when I rebuild and run in release mode it is now True.
What is happening behind the scenes to cause this? I am guessing it has something to do with floating point precision but not exactly sure what.
Any help would be appreciated.
I suspect that the value of 10.16f + 127.15f is computed in 64 (or 80) bits and then compared with 137.31f… whereas in release mode the value is computed, then truncated to 32 bits and then compared. Basically when intermediate values aren’t clamped to a smaller precision, you can get results like this.
If it’s an issue, that probably suggests you shouldn’t be using
floatordoubleto start with – if these are meant to be precise values, usedecimalinstead.