My question is not about the fine points of floating-point calculations, but about the differences in how it's evaluated. Given this:
float a = 1f / 12;
int b = (int)(1f / a);
At runtime, b will be 12. But if I evaluate its expression in something like QuickWatch or LINQPad, it's 11. In fact, I'm unable to recreate the result of 11 at runtime using float
, double
, or decimal
; truncation or Math.Floor
.
What's behind the difference between dynamic evaluation of the expression and the evaluation at runtime?
EDIT:
Since I'm seeing comments about people getting a result different from mine, here is what I'm seeing.
The result at runtime:
The result when evaluated by VS QuickWatch:
The result in LINQPad:
My VS and framework versions: