(original) (raw)
I'd prefer to see \`float.is\_integer\` stay. There \_are\_ occasions when one wants to check that a floating-point number is integral, and on those occasions, using \`x.is\_integer()\` is the one obvious way to do it. I don't think the fact that it can be misused should be grounds for deprecation.
As far as real uses: I didn't find uses of \`is\_integer\` in our code base here at Enthought, but I did find plenty of places where it \_could\_ reasonably have been used, and where something less readable like \`x % 1 == 0\` was being used instead. For evidence that it's generally useful: it's already been noted that the decimal module uses it internally. The mpmath package defines its own "isint" function and uses it in several places: see https://github.com/fredrik-johansson/mpmath/blob/2858b1000ffdd8596defb50381dcb83de2bcccc6/mpmath/ctx\_mp\_python.py#L764. MPFR also has an mpfr\_integer\_p predicate: http://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr\_005finteger\_005fp.
Or, you could look at the SciPy implementation of the beta function, which does indeed do the C equivalent of is\_integer in many places: https://github.com/scipy/scipy/blob/11509c4a98edded6c59423ac44ca1b7f28fba1fd/scipy/special/cephes/beta.c#L67
A concrete use-case: suppose you wanted to implement the beta function (https://en.wikipedia.org/wiki/Beta\_function) for real arguments in Python. You'll likely need special handling for the poles, which occur only for some negative integer arguments, so you'll need an is\_integer test for those. For small positive integer arguments, you may well want the accuracy advantage that arises from computing the beta function in terms of factorials (giving a correctly-rounded result) instead of via the log of the gamma function. So again, you'll want an is\_integer test to identify those cases. (Oddly enough, I found myself looking at this recently as a result of the thread about quartile definitions: there are links between the beta function, the beta distribution, and order statistics, and the (k-1/3)/(n+1/3) expression used in the recommended quartile definition comes from an approximation to the median of a beta distribution with integral parameters.)
In sum: it's an occasionally useful operation; there's no other obvious, readable spelling of the operation that does the right thing in all cases, and it's \_already\_ in Python! In general, I'd think that deprecation of an existing construct should not be done lightly, and should only be done when there's an obvious and significant benefit to that deprecation. I don't see that benefit here.
--
Mark