<div dir="ltr">Thanks for the feedback.<br><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 30, 2018 at 7:13 AM Paul Moore <<a href="mailto:p.f.moore@gmail.com">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><div>Powers of other numbers have to keep the same behavior since in general those kinds of expressions don't create rational numbers.</div><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><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">https://github.com/python/cpython/blob/3.7/Lib/fractions.py#L448</a></div><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><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>