[Python-ideas] Fix some special cases in Fractions?

Chris Angelico rosuav at gmail.com
Thu Aug 30 02:54:16 EDT 2018


On Thu, Aug 30, 2018 at 4:30 PM, Jonathan Goble <jcgoble3 at gmail.com> wrote:
> On Thu, Aug 30, 2018, 2:08 AM Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>>
>> Also, Fraction(1) for the second case would be flat-out wrong.
>
>
> How? Raising something to the 2/3 power means squaring it and then taking
> the cube root of it. -1 squared is 1, and the cube root of 1 is 1. Or am I
> having a 2:30am brain fart?
>

For positive numbers, I believe you're correct. For negative numbers, no.

>>> (-2)**(2/3)
(-0.7937005259840993+1.3747296369986026j)
>>> _**(3/2)
(-1.9999999999999993+2.4492935982947054e-16j)
>>> ((-2)**2)**(1/3)
1.5874010519681994
>>> _**(3/2)
1.9999999999999998

Rounding error aside, raising -2 to 2/3 power and then raising the
result to 3/2 power gives back -2, as it should. Doing it in two steps
loses the negation, and then (again, within rounding error) the end
result is positive two.

ChrisA


More information about the Python-ideas mailing list