Floating point representations seem to do integer arithmetic correctly - why?

前端 未结 8 1294
死守一世寂寞
死守一世寂寞 2021-01-06 10:41

I\'ve been playing around with floating point numbers a little bit, and based on what I\'ve learned about them in the past, the fact that 0.1 + 0.2 ends up bein

8条回答
  •  天命终不由人
    2021-01-06 11:07

    The reason is, that you can represent every whole number (1, 2, 3, ...) exactly in binary format (0001, 0010, 0011, ...)

    That is why integers are always correct because 0011 - 0001 is always 0010. The problem with floating point numbers is, that the part after the point cannot be exactly converted to binary.

提交回复
热议问题