0

Request for help from Python beginner:

I don't want you to do the work for me, just some help to know where my mistakes are and how to correct them.

I am trying to code a program to convert any decimal number into binary. In the case of an infinitely repeating decimal number, the number should terminate at 5 significant digits.

I have nearly all of the code completed, but have encountered a couple of issues.

Here's my code:

numint = ""
numfrac = ""
values = ""
binint = ""
binfrac = ""
number = raw_input("Input a number")

values = number.split(".")
integer = int(values[0])

while integer >= 1:
    intremainder = integer % 2
    integer = integer / 2
    intremainder = str(intremainder)
    binint = binint + intremainder
binint = binint[::-1]

if "." in number:
    fraction = float(values[1]) / (10 ** len(values[1]))
    fraction1 = float(fraction) * 2
    fraction1 = str(fraction1)
    binfrac1 = fraction1[0]
    if "1" in binfrac1:
        fraction1 = float(fraction1)
        fraction1 - 1
    binfrac1 = str(binfrac1)
    
    fraction2 = float(fraction1) * 2
    fraction2 = str(fraction2)
    binfrac2 = fraction2[0]
    if "1" in binfrac2:
        fraction2 = float(fraction2)
        fraction2 - 1
    binfrac2 = str(binfrac2)
    
    fraction3 = float(fraction2) * 2
    fraction3 = str(fraction3)
    binfrac3 = fraction3[0]
    if "1" in binfrac3:
        fraction3 = float(fraction3)
        fraction3 - 1
    binfrac3 = str(binfrac3)
    

    fraction4 = float(fraction3) * 2
    fraction4 = str(fraction4)
    binfrac4 = fraction4[0]
    binfrac4 = str(binfrac4)
    if "1" in binfrac4:
        fraction4 = float(fraction4)
        fraction4 - 1
    binfrac4 = str(binfrac4)
    
    fraction5 = float(fraction4) * 2
    fraction5 = str(fraction5)
    binfrac5 = fraction5[0]
    binfrac5 = float(binfrac5)
    if "1" == binfrac5:
        fraction5 = float(fraction5)
        fraction5 - 1
    binfrac5 = str(binfrac5)

binfrac = binfrac1 + binfrac2 + binfrac3 + binfrac4 + binfrac5

print binint + "." + binfrac

Yes, my code isn't particularly elegant. I intend to shorten a lot of it with a while loop, once I get this code working.

My issues are that the if statements that would subtract 1 from the fraction value if the fraction value is greater than 1 don't work.
My second problem is that in the final converted number, I end up with multiple decimal points. I suspect this is a problem with one of my "fraction * 2" lines, but don't know what to do to correct it.

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by graphpaper
0
fraction1 = float(fraction) * 2
    fraction1 = str(fraction1)
    binfrac1 = fraction1[0]
    if "1" in binfrac1:
        fraction1 = float(fraction1)
        fraction1 - 1
    binfrac1 = str(binfrac1)

This logic is flawed. All you're doing is doubling the "fraction" portion of the number, and checking to see if the first number is a 1, at which point you subtract "1" from the number... I haven't done fractional binary conversions in a long time, but can't you just multiply the fraction by ten until it's a whole number, and then push that numberthrough the same logic that you use for the binint portion? Then you'll only need to adjust for decimal at the end.

0

Granted, my method is rather roundabout, but it's precisely what I wanted to do. Problem is, it doesn't work.

Here's my algorithm:

1. User inputs base 10 number in its entirety
2. Number is split into its integer value and its fractional value
3. The integer value is converted to base 2
• Divide integer value by 2
• Keep remainder
• Divide quotient by 2
• Keep remainder
• Repeat Step 3 and Step 4 until quotient equals 0
• Remainders are concatenated in reverse order they were obtained
4. The fractional value is converted to base 2
• Multiply fractional value by 2
• Record integer value of sum
• Subtract integer value from sum
• Multiply remaining value of sum by 2
• Repeat Step 2, Step 3, and Step 4 until the fraction portion of a sum equals 0 or five integer sum values have been obtained
• Integer sum values are arranged in order they are obtained
• Place a point before the concatenation of the integer sum values
5. The base 2 integer value is concatenated with the base 2 fractional value
6. The converted number is displayed to the screen.

Having looked over my algorithm once again, I may change to fractionX = fractionX -fractionX[0].

0

I've managed to get it to work.

numint = ""
numfrac = ""
values = ""
binint = ""
binfrac1 = ""
binfrac2 = ""
binfrac3 = ""
binfrac4 = ""
binfrac5 = ""
number = raw_input("Input a number")

values = number.split(".")
integer = int(values[0])

while integer >= 1:
    intremainder = integer % 2
    integer = integer / 2
    intremainder = str(intremainder)
    binint = binint + intremainder
binint = binint[::-1]

if "." in number:
    fraction = float(values[1]) / (10 ** len(values[1]))
    fraction1 = float(fraction) + float(fraction)
    fraction1 = str(fraction1)
    if float(fraction1) >= float(1.0):
        binfrac1 = str(1)
        fraction1 = float(fraction1) - float(1.0)
    elif float(fraction1) < float(1.0):
        binfrac1 = str(0)
    fraction1 = str(fraction1)
    
    fraction2 = float(fraction1) + float(fraction1)
    fraction2 = str(fraction2)
    if float(fraction2) >= float(1.0):
        binfrac2 = str(1)
        fraction2 = float(fraction2) - float(1.0)
    elif float(fraction2) < float(1.0):
        binfrac2 = str(0)
    fraction2 = str(fraction2)
    
    fraction3 = float(fraction2) + float(fraction2)
    fraction3 = str(fraction3)
    if float(fraction3) >= float(1.0):
        binfrac3 = str(1)
        fraction3 = float(fraction3) - float(1.0)
    elif float(fraction3) < float(1.0):
        binfrac3 = str(0)
    fraction3 = str(fraction3)

    fraction4 = float(fraction3) + float(fraction3)
    fraction4 = str(fraction4)
    if float(fraction4) >= float(1.0):
        binfrac4 = str(1)
        fraction4 = float(fraction4) - float(1.0)
    elif float(fraction4) < float(1.0):
        binfrac4 = str(0)
    fraction4 = str(fraction4)
    
    fraction5 = float(fraction4) + float(fraction4)
    fraction5 = str(fraction5)
    if float(fraction5) >= float(1.0):
        binfrac5 = str(1)
        fraction5 = float(fraction5) - float(1.0)
    elif float(fraction5) < float(1.0):
        binfrac5 = str(0)
    fraction5 = str(fraction5)

binfrac = binfrac1 + binfrac2 + binfrac3 + binfrac4 + binfrac5

print binint + "." + binfrac

It's not particularly elegant, but it gets the job done. Thanks for the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.