IEEE754浮点数

 

1.5.75          0 10000001 0111 0000 0000 0000 0000 000

161.875             0 10000110 0100 0011 1100 0000 0000 000

-0.0234375            1 01111001 1000 0000 0000 0000 0000 000

经检验(具体请看网址http://www.weitz.de/ieee/)该结果无误

2.利用python实现类浮点数的转换功能

代码大致如下(尚未能成功分辨符号)

s = float(input())
  if int(s)!=0:
    f = s-int(s)
    print(int(s),round(f,9))
    f=round(f,9)

 

    def dec2bin(x):
      x -= int(x)
      a = []
      while x:
        x *= 2
        a.append('1' if x>=1. else '0')
        x -= int(x)
      return a

    print(dec2bin(f))
    c = ''.join(dec2bin(f))
    print(c)

 

    b = list(str(bin(int(s))))
    print(b)

 

    del b[0]
    del b[0]
    print(b)

 

    d = len(b)
    d=d-1+127
    e = list(str(bin(int(d))))
    print(e)
    del e[0]
    del e[0]
    print(e)

 

    del b[0]
    b = ''.join(b)
    print(b)

 

    e.extend(b)
    e.extend(c)
    g = ''.join(e)
    print(g)

 

    j = int(len(e))
    print(j)
    while (j<31):
      e.append('0')
      j = len(e)

 

    e.insert(0,'0')
    g = ''.join(e)
    print(g)
  if int(s)==0:
    f = s-int(s)
    print(int(s),round(f,9))
    f=round(f,9)
    def dec2bin(x):
      x -= int(x)
      a = []
      while x:
        x *= 2
        a.append('1' if x>=1. else '0')
        x -= int(x)
      return a
    print(dec2bin(f))
    c = dec2bin(f)
    k = c.index('1')
    k = k*(-1)+127
    e = list(str(bin(int(k))))
    del e[0]
    del e[0]
    print(e)
    e.extend(c)
    j=len(e)
    while (j<31):
      e.append('0')
      j = len(e)
    e.insert(0,'0')
    g = ''.join(e)
    print(g)

 

posted @ 2020-10-18 19:21  20201210徐铭泽  阅读(72)  评论(1编辑  收藏  举报