[Python-ideas] Fix some special cases in Fractions?
Neil Girdhar
mistersheik at gmail.com
Thu Aug 30 03:37:10 EDT 2018
There are a lot of misunderstandings in this thread. It's probably best to
start by reading up on the roots of unity (
https://en.wikipedia.org/wiki/Root_of_unity). The key ideas are that a
real number has two complex square roots, three complex cube roots, and so
on.
Normally, in Python, we return the principal square root (
http://mathworld.wolfram.com/PrincipalSquareRoot.html). We would do the
same for the cube root I imagine if we had a cube root function Let's call
that the principal cube root, which is always real for a real-valued input.
---
> For positive numbers, I believe you're correct. For negative numbers, no.
Actually, Jonathan is right.
>>> (-2)**(2/3)
(-0.7937005259840993+1.3747296369986026j)
This is just giving you one of the other two cube roots.
> Rounding error aside, raising -2 to 2/3 power and then raising the result
to 3/2 power gives back -2, as it should.
There is no way to guarantee that "it should do this". A fractional power
is not a function, and so it has no inverse function.
> Doing it in two steps loses the negation, and then (again, within
rounding error) the end result is positive two.
I think you would see your mistake if you applied this logic to -1 ** 2 **
(1/2).
On Thu, Aug 30, 2018 at 3:08 AM Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:
> Jonathan Goble wrote:
> > How? Raising something to the 2/3 power means squaring it and then
> > taking the cube root of it.
>
> On reflection, "wrong" is not quite accurate. A better
> word might be "surprising".
>
> (-1) ** (2/3) == 1 would imply that 1 ** (3/2) == -1.
I suppose that could be considered true if you take the
> negative solution of the square root, but it seems a
> bit strange, and it's not what Python gives you for
> the result of 1 ** (3/2).
>
Python gives you the principle root when you do 1 ** (3/2), which is
exactly what I'm proposing for Fraction.
> If you want a solution that round-trips, you need
> complex numbers.
There is no solution that round trips since in general fractional powers
are not functions. The case in which you can always round trip is when you
are taking a power c ** (a/b) where a is odd, and either c is positive or b
is odd. Then, the result to the power of (b/a) gives you c.
That's what Python does when you use
> floats. Making Fractions do something different would
> make it inconsistent with floats.
>
Actually, my cases are all examples of the principal roots and are
consistent with floats.
>
> My calculator (which only does real floats) reports an
> error when trying to evaluate (-1) ** (2/3).
>
Yes, which is what Python does with reals. The difference is the the
Fraction type has exact values, and it knows that the unique answer in the
field of Fraction objects is 1.
>
> --
> Greg
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/aZIHpPhe0mw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180830/aece5d4a/attachment.html>
More information about the Python-ideas
mailing list