Python floating point number comparison

前端 未结 3 1850
别跟我提以往
别跟我提以往 2020-12-03 05:58

I\'m just reviewing some basics of Python and there\'s a tricky problem about comparing floating point numbers.

2.2 * 3.0 == 6.6
3.3 * 2.0 == 6.6
         


        
3条回答
  •  失恋的感觉
    2020-12-03 06:20

    This might be illuminating:

    >>> float.hex(2.2 * 3.0)
    '0x1.a666666666667p+2'
    >>> float.hex(3.3 * 2.0)
    '0x1.a666666666666p+2'
    >>> float.hex(6.6)
    '0x1.a666666666666p+2'
    

    Although they are all displayed in decimal as 6.6, when you inspect the internal representation, two of them are represented in the same way, while one of them is not.

提交回复
热议问题