浮点型十进制与浮点型二进制的转换(小白版)

Posted hbu-xiaopipi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浮点型十进制与浮点型二进制的转换(小白版)相关的知识,希望对你有一定的参考价值。

大佬们看一下那有不足,皮皮在此谢过了。

def  bTod (a,p=4):
    ‘‘‘
    bTod(...)
    bTod(string,integer)
    input: String only contains numeric characters  or  points or minus  sign, the string represents  a binary  number.And the integer is used  to determine the accuracy  of  the  output  data.
    process: Converts  the  binary number that this string  represents  to  a  decimal  number .
    output: Print  the  result(decimal  number) that determines  the  accuracy.
    ‘‘‘
    p=int(p)
    sign=‘‘
    if a[0]==-:
        sign=a[0]
        a=a[1:]
    ithpower=0
    intsum=0
    floatsum=0
    for c in a:
        if c not in (1,0,.):
            print("输入的数值不是二进制数")
            return 0
    if  a.count(".")>1:
        print("输入错误")
        return 0   
    elif a.count(".")==1:
        cut=a.split(".")
        pointbefore=cut[0]
        pointbefore=pointbefore[::-1]
        pointafter=cut[1]
    else :
        pointbefore=a[::-1]
        pointafter="0"
    for c in pointbefore:
        intsum=intsum+ int(c)*(2**ithpower)
        ithpower=ithpower+1
    ithpower=-1    
    for  c  in  pointafter:
        floatsum=floatsum+int(c)*(2**ithpower)
        ithpower=ithpower-1
    result=intsum+floatsum
    print("二进制数{}转变成十进制数保留{}位小数的结果是{}{:.{}f}" .format(a,p,sign,result,p))
def  dTob(a,p=4):
    ‘‘‘
    bTod(...)
    bTod(string,integer)
    input: String only contains numeric characters  or  points  or  minus  sign, the string represents  a decimal  number.And the integer is used  to determine the accuracy  of  the  output  data.
    process: Converts  the decimal number that this string  represents  to  a binary  number .
    output: Print  the  result(binary  number) that determines  the  accuracy.
    ‘‘‘
    p=int(p)
    times=0 
    sign=‘‘
    if a[0]==-:
        sign=a[0]
        a=a[1:]
    for c in  a:
        if  (c<0 or c>9) and c!=.:
            print("输入错误")
            return  0
    if  a.count(".")>1:
        print("输入错误")
        return  0
    elif a.count(.)==1:
        cut=a.split(".")
        pointafter=float("0."+cut[1])
        auto=bin(int(cut[0]))
        intsum=auto[2:]
        floatsum=‘‘
        times=0
        while pointafter :
            t=pointafter*2
            if t<1:
                floatsum=floatsum+0
                pointafter=pointafter*2
                times=times+1
            else:
                floatsum=floatsum+1
                pointafter=t-1
                times=times+1
            if  times >500:
                print("转化后的小数为无限小数")
                break
        if len(floatsum)>p:
            floatsum=floatsum[:p]
        elif len(floatsum)<p:
            floatsum=floatsum+0*(p-len(floatsum))
        else:
            floatsum=floatsum
        result=intsum+.+floatsum      
        print("十进制数{}转化为二进制数保留{}位小数的结果是{}{}" .format(a,p,sign,result))    
    else :
        p=int(p)
        auto=bin(int(a))
        result=auto[2:]+.+0*p      
        print("十进制数{}转化为二进制数保留{}位小数的结果是{}{}" .format(a,p,sign,result))
                
print("请在输入数字前加上数字类型(十进制为d、二进制为b,默认为十进制转二进制,默认精度为小数点后四位),q退出程序")
while 1:
    n=input("请输入要转换的内容:")
    if n==q:
        print("程序退出")
        break
    else:
        pole=1
        while  pole :
            s=input("请输入精度:")
            if s==‘‘:
                s=4
                break
            for  c in s:
                if 0<=c<=9:
                    pole=0
                else:
                    print("精度输入错误")
                    pole=1
                    break     
        if  n==‘‘:
            print("输入错误")
        elif n[0]==d:
            dTob(n[1:],s)
        elif 0 <=n[0]<=9  or  n[0]==-:
            dTob(n,s)
        elif n[0]==b:
            bTod(n[1:],s)
        else:
            print("输入错误")
            
        
                 
                

 

以上是关于浮点型十进制与浮点型二进制的转换(小白版)的主要内容,如果未能解决你的问题,请参考以下文章

C语言学习 -- 整型与浮点型在内存中的存储

字节数组byte[]和整型,浮点型数据的转换——Java代码

Mysql数据类型之浮点与二进制型数据使用案例总结

为什么浮点型运算结果会有误差?

整型&浮点型&字符串 内置方法

C++如何将使用16进制表达的颜色色彩RGB字符串转化为整型或者浮点型表达?(颜色转换色彩转换)