Greetings everyone. This is my first post on this board, and I would like to offer thanks for any and all help, guidance, advice, and/or bonks on the head in advance. I am an aspiring programmer currently learning Python as a way to introduce myself to the world of software engineering.
I am attempting to create an algorithm that will convert a user input real (ie. XXXX.XXXX) decimal (ie base10) number to its binary equivalent. I have no trouble converting the integral part, but I cannot seem to come up with a functioning algorithm for the fractional part.
Below I have pasted what I have come up with so far for this portion of the script:
fractionstring = ""
mystring = "486.654321"
mysplit = mystring.split(".")
myfractional = float(mysplit[1]) / (10 ** len(mysplit[1]))
while myfractional < 1:
binaryfraction = (myfractional * 2) // 1
fractionstring = fractionstring + str(binaryfraction)
myfractional = (myfractional * 2) - binaryfraction
If anyone could provide some advice to get me pointed in the right direction I would be much obliged.
EDIT: the <mystring> variable is initialized purely for testing purposes. In the final code I plan on using
raw_input("")
to retrieve input from the user.