Strange result with floating point addition

前端 未结 4 1612

Why is it when I print/display the result of

eval(\"11.05\") + eval(\"-11\")

it comes out as 0.05000000000000071 instead of the expecte

4条回答
  •  失恋的感觉
    2020-12-11 23:41

    This has nothing to do with eval. In fact, this is what happens if you type in a console 11.05 - 11: enter image description here

    This is a consequence of how programming languages store floating-point numbers; they include a small error. If you want to read more about this, check this out.

提交回复
热议问题