Why can't decimal numbers be represented exactly in binary?

前端 未结 20 3409
不知归路
不知归路 2020-11-21 05:15

There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn\'t have an exact binary representation, so it\'

20条回答
  •  逝去的感伤
    2020-11-21 05:51

    The problem is that you do not really know whether the number actually is exactly 61.0 . Consider this:

    
    float a = 60;
    float b = 0.1;
    float c = a + b * 10;
    

    What is the value of c? It is not exactly 61, because b is not really .1 because .1 does not have an exact binary representation.

提交回复
热议问题