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

前端 未结 4 1926
误落风尘
误落风尘 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:18

    There is no problem, really. It is just the way floats work (their internal binary representation). Anyway:

    >>> from decimal import Decimal
    >>> Decimal('4.2')-Decimal('0.1')
    Decimal('4.1')
    

提交回复
热议问题