Why do simple math operations on floating point return unexpected (inaccurate) results in VB.Net and Python?

前端 未结 4 1938
误落风尘
误落风尘 2020-11-29 10:40
x = 4.2 - 0.1  

vb.net gives 4.1000000000000005
python gives 4.1000000000000005

Excel gives 4.1<

4条回答
  •  孤城傲影
    2020-11-29 11:34

    In vb.net, you can avoid this problem by using Decimal type instead:

    Dim x As Decimal = 4.2D - 0.1D
    

    The result is 4.1 .

提交回复
热议问题