On Wed, Mar 21, 2018 at 3:02 PM, Tim Peters <tim.pet...@gmail.com> wrote:
> [David Mertz] > > I've been using and teaching python for close to 20 years and I never > > noticed that x.is_integer() 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.is_integer() True >>> (math.sqrt(x**2)).is_integer() 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 `.is_integer()` (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.
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com