0

I am making a basic program that tells the user if their inputted number is divisible by 0.20 or not. The program uses the modulo operator to test this. If the remainder is 0 when the user's inputted number is divided by 0.20, the program should print that the number is divisible by 0.20, if the remainder isn't 0, the number isn't divisible by 0.20.

Here's the code.

while True:
  num = float(input("Enter a number: "))  
  # Checks if the user's input is divisible by 0.20
  if (num % 0.20) == 0.0:  
     print("{} is divisible by 0.20".format(num))  
  else:  
     print("{} is not divisible by 0.20".format(num))  

If I were to type 2, the program would tell me that 2 is divisible by 0.2 (which is correct). The problem I am encountering is that if I were to type the number 4 (a number that is divisible by 0.20), the program would print that 4 is not divisible by 0.20 even though it is. If I were to try and see if a number were divisible by 2 rather than 0.2, if I put in 100002, the program would tell me that 100002 is divisible by 2 but when I decide to see if a number is divisible by a decimal number, it doesn't work as efficiently.

  • probably because in the decimal world everything is divisible. – perreal Sep 08 '22 at 05:32
  • Because 0.2 cannot be accurately represented in floating point numbers: `decimal.Decimal(0.2) => Decimal('0.200000000000000011102230246251565404236316680908203125')` – Mechanic Pig Sep 08 '22 at 05:42

0 Answers0