Conversion of a decimal to double number in C# results in a difference

前端 未结 5 1822
我寻月下人不归
我寻月下人不归 2020-11-30 00:44

Summary of the problem:

For some decimal values, when we convert the type from decimal to double, a small fraction is added to the result.

What makes it wors

5条回答
  •  猫巷女王i
    2020-11-30 00:56

    The article What Every Computer Scientist Should Know About Floating-Point Arithmetic would be an excellent place to start.

    The short answer is that floating-point binary arithmetic is necessarily an approximation, and it's not always the approximation you would guess. This is because CPUs do arithmetic in base 2, while humans (usually) do arithmetic in base 10. There are a wide variety of unexpected effects that stem from this.

提交回复
热议问题