[Python-ideas] Fix some special cases in Fractions?
Steven D'Aprano
steve at pearwood.info
Thu Aug 30 09:01:52 EDT 2018
On Wed, Aug 29, 2018 at 09:39:05PM -0700, Neil Girdhar wrote:
> Would there be any problem with changing:
>
> In [4]: Fraction(1, 1) ** Fraction(2, 3)
> Out[4]: 1.0
>
> In [5]: Fraction(-1, 1) ** Fraction(2, 3)
> Out[5]: (-0.4999999999999998+0.8660254037844387j)
>
> In [6]: Fraction(0, 1) ** Fraction(2, 3)
> Out[6]: 0.0
>
> I'd like these to be Fraction(1), Fraction(1), and Fraction(0).
If anyone else has mentioned the backward-compatibility issue by now, I
haven't see it. I believe that would make it a fairly big problem.
I expect that there is code out in the wild which (for good or ill) now
expects 1**Fraction(2, 3) to return 1.0, rather than Fraction(1), and
similarly for the other examples. Changing that could break people's
code.
Even if we had consensus that this was a good idea, or at least
consensus from the maths-folk who care about this sort of thing (I'd
like to know what Uncle Timmy and Mark think of this idea), it would
still probably need a "__future__" import to activate it, or a
deprecation period. Or some other annoyance.
Possibly the simpler approach would be to add a subclass that does what
you want. UnitRootFraction or something.
Then the only argument will be whether such a subclass ought to go into
the fractions module, or your own personal toolkit :-)
I must admit though, I'm a bit curious as to what you are doing that
having 1**Fraction(2,3) return 1.0 is an annoyance, but having
27**Fraction(2,3) return 8.999999999999998 instead of Fraction(9) isn't.
--
Steve
More information about the Python-ideas
mailing list