[Python-Dev] Deprecating float.is_integer() (original) (raw)
David Mertz mertz at gnosis.cx
Wed Mar 21 14:14:06 EDT 2018
- Previous message (by thread): [Python-Dev] Deprecating float.is_integer()
- Next message (by thread): [Python-Dev] Deprecating float.is_integer()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I've been using and teaching python for close to 20 years and I never noticed that x.is_integer() exists until this thread. I would say the "one obvious way" is less than obvious.
On the other hand, x == int(x)
is genuinely obvious... and it immediately
suggests the probably better math.isclose(x, int(x))
that is what you
usually mean.
On Wed, Mar 21, 2018, 2:08 PM Mark Dickinson <dickinsm at gmail.com> wrote:
I'd prefer to see
float.isinteger
stay. There are occasions when one wants to check that a floating-point number is integral, and on those occasions, usingx.isinteger()
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
isinteger
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 likex % 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/ctxmppython.py#L764. MPFR also has an mpfrintegerp predicate: http://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr005finteger005fp. A concrete use-case: suppose you wanted to implement the beta function ( https://en.wikipedia.org/wiki/Betafunction) 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 isinteger 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 isinteger 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.) Or, you could look at the SciPy implementation of the beta function, which does indeed do the C equivalent of isinteger in many places: https://github.com/scipy/scipy/blob/11509c4a98edded6c59423ac44ca1b7f28fba1fd/scipy/special/cephes/beta.c#L67 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
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/mertz%40gnosis.cx -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180321/1125ccb9/attachment-0001.html>
- Previous message (by thread): [Python-Dev] Deprecating float.is_integer()
- Next message (by thread): [Python-Dev] Deprecating float.is_integer()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]