[Python-Dev] int() and math.trunc don't accept objects that only define __index__

Rémi Lapeyre remi.lapeyre at henki.fr
Fri Mar 15 06:59:18 EDT 2019


Le 15 mars 2019 à 03:49:19, Steven D'Aprano
(steve at pearwood.info(mailto:steve at pearwood.info)) a écrit:

> On Wed, Mar 13, 2019 at 03:21:31AM -0700, Rémi Lapeyre wrote:
>
> > When __index__ is defined it means that there is a lossless conversion
> > to int possible. In this case, this means a lossless conversion to
> > float and complex is also possible
>
> That's not correct:
>
> py> n = 2**64 + 1
> py> n == int(float(n))
> False

Thanks, I should have thought of that. Do you think __index__ should
only define
__int__ then?


> Python floats (C doubles) can lose digits when converting from ints
> over 2**53 or so.
>
>
> > (with the exception of overflows
> > but anyone doing float(var) should expect them).
>
> I don't. I expect float(var) to overflow to infinity, if it is going to
> overflow, and always forget that it can raise.
>
>
> py> float("9e9999")
> inf
>
> py> float(str(9*10**9999))
> inf
>
> But:
>
> py> float(9*10**9999)
> Traceback (most recent call last):
> File "", line 1, in
> OverflowError: int too large to convert to float
>
> This never fails to surprise me.
>
>
>
>
> --
> Steven
> _______________________________________________
> 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/remi.lapeyre%40henki.fr


More information about the Python-Dev mailing list