Python实现的十进制小数与二进制小数相互转换功能
Python实现的十进制小数与二进制小数相互转换功能
link
def dec2bin(x):
x -= int(x)
bins = []
while x:
x *= 2
bins.append(1 if x>=1. else 0)
x -= int(x)
return bins
print(dec2bin(.8125))
# [1, 1, 0, 1]
Python实现的十进制小数与二进制小数相互转换功能
link
def dec2bin(x):
x -= int(x)
bins = []
while x:
x *= 2
bins.append(1 if x>=1. else 0)
x -= int(x)
return bins
print(dec2bin(.8125))
# [1, 1, 0, 1]