Naoki Atkins
Naoki Atkins

Reputation: 9

Why is my code producing the wrong output when I try to convert a decimal number to binary?

I'm trying to convert the decimal number 434789, to its binary form, 1101010001001100101, but I get the wrong output, 1101010001001100032.0. How can I fix this?

Here is my code :

def n_to_b_converter():
    new_n = float(0)
    p = 10
    n = float(input("Enter the number in base 10: "))
    b = float(input("Enter the base to convert to: "))
    r = n % b
    q = (n - r) / b
    new_n += r
    n = q
    while q != 0:
        r = n % b
        q = (n - r) / b
        new_n += r * p
        p *= 10
        n = q
    return "{0:.1f}".format(new_n)

Upvotes: 0

Views: 74

Answers (2)

Prune
Prune

Reputation: 77857

You're trying to perform a discrete (integer) process on floating-point numbers. Round-off error is killing you. Change to integer operations:

new_num = 0
p = 10
num = 434789
base = 2 
rem = num % base
quot = (num - rem) // base
new_num += rem 
num = quot
while quot != 0:
    rem = num % base
    quot = (num - rem) // base
    new_num += rem * p 
    print(num, '\t', new_num,'\t',  rem,'\t',  quot,'\t',  p)
    p *= 10
    num = quot
print(new_num)

Output:

217394   1   0   108697      10
108697   101     1   54348   100
54348    101     0   27174   1000
27174    101     0   13587   10000
13587    100101      1   6793    100000
6793     1100101     1   3396    1000000
3396     1100101     0   1698    10000000
1698     1100101     0   849     100000000
849      1001100101      1   424     1000000000
424      1001100101      0   212     10000000000
212      1001100101      0   106     100000000000
106      1001100101      0   53      1000000000000
53   10001001100101      1   26      10000000000000
26   10001001100101      0   13      100000000000000
13   1010001001100101    1   6   1000000000000000
6    1010001001100101    0   3   10000000000000000
3    101010001001100101      1   1   100000000000000000
1    1101010001001100101     1   0   1000000000000000000
1101010001001100101

I also recommend that you quit ramming the binary representation into decimal format. Just accumulate the digits as characters, in a string.

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

Currently, you are increasing the number's decimal representation until it looks the same as the number's binary representation. For example, for 67, you'd produce the number 1,000,011 (commas inserted for clarity). This is not a good way to convert to binary, primarily because it's misleading but secondarily because, as the numbers get very high, floating point inaccuries result (which is the root of the problem you're having: instead of '{0:.1f}'.format(new_n), just do str(new_n) - in newer versions of python ints can be arbitrarily large without losing precision, and there's no reason to make this a float).


But what you should really do is use strings instead. In fact, the python standard library provides a ready-built bin() method to convert integers into strings of their binary representation:

s = bin(67)
# s = '0b1000011'

You can get take just the number part of this number by slicing it:

s = bin(67)[2:]
# s = '1000011'

and then, if you really want it as a decimal number that is that large, you can cast it back to int.:

t = int(s)
# t = 1,000,011

Upvotes: 1

Related Questions