-1

I've a float number which is logically divisible by another float number; but Python gives me false statement for that!?

I tried other cases where I divided float numbers by other float numbers, and I noticed that works only when the tenths decimal for denominators are 0 or 5!?...so is there a way to fix this issue and get a True statement?

56.00 % 11.20 == 0

Result:

false

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

1

First of all, you're not doing a normal division. You're doing what's called a modulus division.

Second of all, as suggested in the comment by @harold, in computers, 11.2 might not be equal to 11.199999999999999999999999999; they can be seen as different numbers (check this link to see the most accurate representation of 11.2 in binary format). As you can see 11.1999999999999992894572642399 (the binary representation for 11.2) is not equal to 11.199999999999999999999999999 (they differ in the 14th place)

You could try doing round() to force it to have a finite number of decimals and check against that.

Try this:

round(56.00 % 11.20, 2) == 0.0

That should give True.