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

David Mertz mertz at gnosis.cx
Wed Mar 21 16:49:51 EDT 2018


On Wed, Mar 21, 2018 at 3:02 PM, Tim Peters <tim.peters at gmail.com> wrote:

[David Mertz] > I've been using and teaching python for close to 20 years and I never > noticed that x.isinteger() exists until this thread.

Except it was impossible to notice across most of those years, because it didn't exist across most of those years ;-)

That's probably some of the reason. I wasn't sure if someone used the time machine to stick it back into Python 1.4.

> On the other hand, x == int(x) is genuinely obvious..

But a bad approach: it can raise OverflowError (for infinite x); it can raise ValueError (for x a NaN);

These are the CORRECT answers! Infinity neither is nor is not an integer. Returning a boolean as an answer is bad behavior; I might argue about which exception is best, but False is not a good answer to float('inf').is_integer(). Infinity is neither in the Reals nor in the Integers, but it's just as much the limit of either.

Likewise Not-a-Number isn't any less an integer than it is a real number (approximated by a floating point number). It's NOT a number, which is just as much not an integer.

and can waste relative mountains of time creating huge integers, e.g.,

True enough. But it's hard to see where that should matter. No floating point number on the order of 1e306 is sufficiently precise as to be an integer in any meaningful sense. If you are doing number theory with integers of that size (or larger is perfectly fine too) the actual test is isinstance(x, int). Using a float is just simply wrong for the task to start with, whether or not those bits happen to represent something Integral... the only case where you should see this is "measuring/estimating something VERY big, very approximately."

For example, this can be true (even without reaching inf):

x.isinteger() True (math.sqrt(x**2)).isinteger() False

The problem there isn't how "is it an integer?" is spelled, it's that

any way of spelling "is it an integer?" doesn't answer the question they're trying to answer. They're just plain confused about how floating point works. The use of .isinteger() (however spelled!) isn't the cause of that, it's a symptom.

Agreed!

-- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180321/94df5a59/attachment.html>



More information about the Python-Dev mailing list