Reputation: 134
I'm trying to write a Python script that converts arbitrarily large numbers to binary representation. For some reason, this returns a long series of zeroes until the last few digits. Why?
input = 237481829347283491234198027358096491872350934781238471203874128039741298374109837410983741902374921793047102934710923741902374
while(input > 0):
print(input % 2)
input = int(input / 2)
Upvotes: 1
Views: 76
Reputation: 308186
You have exceeded the number of digits that can be held by a float
. It's not obvious that you have a float
but you do, it's generated by the division operator /
. Use the integer division //
instead.
input = input // 2
After you've fixed that, be aware that this algorithm puts out the digits in reverse order.
Upvotes: 3