Learning Maths
So, while reading the book on Mathematics, I had started to code too. I am starting from simple examples like decimal to binary conversion. I am posting my code here :
This one is for converting a decimal integer to binary:
Another one for converting a floating point number to binary.
This one is for converting a decimal integer to binary:
#!/usr/bin/python
import sys
def dec2bin_int(n):
arr = []
print "Decimal no: " , n
while n > 0:
arr.append(n % 2)
n = n / 2
arr.reverse()
for i in arr:
print i,
if __name__ == '__main__':
dec2bin_int(int(sys.argv[1]))
Another one for converting a floating point number to binary.
#!/usr/bin/python
import sys
def dec2bin_flt(n,limit):
print "Float no: " , n
i = 0
while n > 0:
i = i+1
m = 2 * n
print m.__int__(),
n = m - m.__int__()
if i == limit:
break
if __name__ == '__main__':
dec2bin_flt(float(sys.argv[1]),int(sys.argv[2]))
Comments