mardi 1 septembre 2020

Why does this simple Python code break down for large numbers?

This is a simple code I wrote in Python for telling if the input is odd or even. It is also probably redundant in some parts(I was just trying so that the program doesn't crash for any input).

try:
    num = input("Please,\nenter your integer to check if it's odd or even: ")
    rem = float(num) % 2
    if rem == 0:
        print("Your number", int(num), "belongs to the set of even integers!")
    elif rem == 1:
        print("Your number", int(num), "belongs to the set of odd integers!")
    else:
        print("Your number", num, "does not belong to the set of integers!")
except ValueError:
    print("Your input does not belong to the set of integers!")

Such a simple operation gives a wrong result for a 17 digit number. For example

Please,
enter your integer to check if it's odd or even: 49761934591459137
Your number 49761934591459137 belongs to the set of even integers!

Why is this happening, when python can even perform tetration(repeated exponentiation) of 2 upto 5 times(which is a 19,729 digit number)?

Aucun commentaire:

Enregistrer un commentaire