MATLAB to Python: Decimal to Binary Conversion
def decimal_to_binary(d, n=1): d = float(d) if not isinstance(n, (int, float)) or n < 0: raise ValueError('N must be a positive scalar numeric.') n = round(n)
f, e = math.frexp(max(d))
binary_str = ''
for i in range(1 - max(n, e), 1):
binary_str += str(int(d * 2**i) % 2)
return binary_str
原文地址: https://www.cveoy.top/t/topic/qFKo 著作权归作者所有。请勿转载和采集!