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

前端 未结 20 3410
不知归路
不知归路 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:48

    you know integer numbers right? each bit represent 2^n


    2^4=16
    2^3=8
    2^2=4
    2^1=2
    2^0=1

    well its the same for floating point(with some distinctions) but the bits represent 2^-n 2^-1=1/2=0.5
    2^-2=1/(2*2)=0.25
    2^-3=0.125
    2^-4=0.0625

    Floating point binary representation:

    sign  Exponent    Fraction(i think invisible 1 is appended to the fraction )
    B11  B10 B9 B8   B7 B6 B5 B4 B3 B2 B1 B0

提交回复
热议问题