[Python-Dev] Deprecating float.is_integer() (original) (raw)

Serhiy Storchaka storchaka at gmail.com
Wed Mar 21 04:06:08 EDT 2018


I searched usages of is_integer() on GitHub and have found that it is used only in silly code like (x/5).is_integer(), (x0.5).is_integer() (or even (x(1/3)).is_integer()) and in loops like:

i = 0
while i < 20:
    if i.is_integer():
        print(i)
    i += 0.1

(x/5).is_integer() is an awful way of determining the divisibility by 5. It returns wrong result for large integers and some floats. (x % 5 == 0) is a more clear and reliable way (or PEP 8 compliant (not x % 5)).

Does anybody know examples of the correct use of float.is_integer() in real programs? For now it looks just like a bug magnet. I suggest to deprecate it in 3.7 or 3.8 and remove in 3.9 or 3.10. If you even need to test if a float is an exact integer, you could use (not x % 1.0). It is even faster than x.is_integer().



More information about the Python-Dev mailing list