<div dir="ltr"><div>I think you could take the implementation further and decide that any power of Fraction(1) is Fraction(1) and any positive power of Fraction(0) is Fraction(0).</div><div>I woudn't be shocked that Fraction(1) ** 3.7 == Fraction(1) and Fraction(0) ** 3.7 == 0.</div><div><br></div><div>However the implementation for Fraction(-1) seems a bit to "ad hoc", and break some expected behavior of the powers.</div><div>For exemple in your code Fraction(-2) ** Fraction(2, 3) != Fraction(-1) ** Fraction(2, 3) * Fraction(2) ** Fraction(2, 3), whereas floats respect this.</div><div>You could change the code so that the property (a *b) ** c == a**c *b**c, but idk how hard it is.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-08-30 13:31 GMT+02:00 Neil Girdhar <span dir="ltr"><<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for the feedback.<br><br><div class="gmail_quote"><span class=""><div dir="ltr">On Thu, Aug 30, 2018 at 7:13 AM Paul Moore <<a href="mailto:p.f.moore@gmail.com" target="_blank">p.f.moore@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">(You're still not fixing your mail headers. Please do, it's hard to be<br>
bothered responding if I keep having to fix your mails in order to do<br>
so).<br>
<br>
On Thu, 30 Aug 2018 at 11:28, Neil Girdhar <<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@gmail.com</a>> wrote:<br>
><br>
> But I'm only asking for fractional powers of -1, 0, and 1.  Is that really a complex issue?<br>
<br>
Yes. (Even ignoring the oh-so-tempting complex number joke ;-)). As<br>
has been seen here there's no agreement on the "right" choice of which<br>
root of -1 to choose. Or possibly more accurately, no-one else is<br>
agreeing with your suggestion that we choose a different option for<br>
the case you're arguing over.<br>
<br>
And to be 100% precise, you asked for the results of three *very<br>
specific* calculations to change. I guess you actually want something<br>
more general - or are you really OK with (for example)<br>
Fraction(-1,1)**Fraction(2,3) changing as you request, but<br>
Fraction(-2,1)**Fraction(2,3) remaining as it currently is? </blockquote><div><br></div></span><div>Powers of other numbers have to keep the same behavior since in general those kinds of expressions don't create rational numbers.</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You still<br>
haven't clarified (no-one has particularly asked yet - you may<br>
consider this a request to do so if you like) how you propose in<br>
general that the result of<br>
<br>
    Fraction(-1,1) ** Fraction(a, b)<br>
and/or<br>
    Fraction(1,1) ** Fraction(a, b)<br>
or maybe even more generally<br>
    Fraction(c,d) ** Fraction(a,b)<br>
<br>
would change. What exactly are the special cases you want to define<br>
different results for? What is the process for choosing the result?<br></blockquote><div><br></div></span><div>Here's my proposed method:</div><div><br></div><div><div>class Fraction:</div><div>    def __pow__(a, b):</div><div>        """a ** b</div><div>        If b is not an integer, the result will be a float or complex</div><div>        since roots are generally irrational. If b is an integer, the</div><div>        result will be rational.</div><div>        """</div><div>        if isinstance(b, numbers.Rational):</div><div>            if b.denominator == 1:</div><div>                power = b.numerator</div><div>                if power >= 0:</div><div>                    return Fraction(a._numerator ** power,</div><div>                                    a._denominator ** power,</div><div>                                    _normalize=False)</div><div>                elif a._numerator >= 0:</div><div>                    return Fraction(a._denominator ** -power,</div><div>                                    a._numerator ** -power,</div><div>                                    _normalize=False)</div><div>                else:</div><div>                    return Fraction((-a._denominator) ** -power,</div><div>                                    (-a._numerator) ** -power,</div><div>                                    _normalize=False)</div><div>            elif a == -1 and b.denominator % 2 == 1:</div><div>                return Fraction(-1 if b.numerator % 2 == 1 else 1)</div><div>            elif a == 0:</div><div>                if b > 0:</div><div>                    return Fraction(0)</div><div>                else:</div><div>                    raise ZeroDivisionError(</div><div>                        "0 cannot be raised to a negative power")</div><div>            elif a == 1:</div><div>                return Fraction(1)</div><div>            else:</div><div>                # A fractional power will generally produce an</div><div>                # irrational number.</div><div>                return float(a) ** float(b)</div><div>        else:</div><div>            return float(a) ** b</div></div><div><br></div><div>Compare it with <a href="https://github.com/python/cpython/blob/3.7/Lib/fractions.py#L448" target="_blank">https://github.com/<wbr>python/cpython/blob/3.7/Lib/<wbr>fractions.py#L448</a></div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> You are right that the fractional power of -1 and 1 has multiple values, but the fractional power of zero has a unique value.<br>
<br>
And that part of your proposal has not generated much controversy.<br>
Maybe if you proposed only that, you might get that change made? I<br>
haven't considered the ramifications of that because the discussions<br>
about -1 are obscuring it, but it might be relatively uncontroversial.<br></blockquote><div><br></div></span><div>Fair enough. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Paul<br>
</blockquote></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><p>--<b><br>Nicolas Rolin</b> | Data Scientist<br>+ 33 631992617 - nicolas<a href="mailto:prenom.nom@tiime.fr" rel="nofollow" target="_blank">.rolin@tiime.fr</a></p><p><span></span><span><font color="#888888"><img src="https://docs.google.com/uc?export=download&id=0B5gEmxojZz7NUklic0RTMDVXd0E&revid=0B5gEmxojZz7NYytTZzQ3Q2t6d0xYZGZVSkljV3RCNGxZRENVPQ" width="96" height="28"> </font></span><br><i>15 rue Auber, </i><i>75009 Paris</i><br><i><a href="http://www.tiime.fr" rel="nofollow" target="_blank">www.tiime.fr</a></i></p></div></div></div></div></div></div>
</div>