c# - Unexpected results for floating point equals -
question not why 0.1 + 0.9
not equals 1.0
. different behaviour of equals.
can explain why examples below works differently.
float q = 0.1f; float w = 0.9f; float summ = q + w; q + w == 1.0f; // false summ == 1.0f; // true
why operator ==
works differently?
the problem due fact intermediate calculations being performed in higher precision, , rules when round float
precision different in each case.
according the docs
by default, in code x86 architectures compiler uses coprocessor's 80-bit registers hold intermediate results of floating-point calculations.
... compiler performs rounding on variables of type
float
correct precision assignments , casts , when parameters passed function"
float summ = q + w
assignment, , hence rounded nearest float
, in case 1.
q + w == 1.0f
neither cast, assignment or function call, result of addition still extended precision float, close, not equal, 1.
Comments
Post a Comment