I've made a really simple decimal to binary converter, everything seems to work fine but when given a big number it spits out results that are way different from results from another online converters. I wonder why is that?
here's my code:
def main():
numberstr = input("Number in decimal > ")
number = int(numberstr)
result = ""
while number >= 1 == True:
if (number % 2) == 1:
remainder = "1"
result = remainder + result
if number == 1:
number = 0
else:
number = number - 1
number = number / 2
number = int(number)
#^^^ this line is for getting rid of the float that comes from dividing.
elif (number % 2) == 0:
remainder = "0"
result = remainder + result
number = number / 2
number = int(number)
print(numberstr, "in binary is:", result)
main()
for example feeding it this number: 987654321987654321
gives out this result: 110110110100110110100101111101111110111101000001001010000001
While online converters (namely rapidtables.com and binaryhexconverter.com ) both give out this: 110110110100110110100101111101111110111101000001001010110001
For comparing:
110110110100110110100101111101111110111101000001001010000001 - my code
110110110100110110100101111101111110111101000001001010110001
It seems that to some point it does the math right, but then it messes up for some reason.
Any ideas on what might be causing it?