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

Jonathan Fine jfine2358 at gmail.com
Thu Aug 30 05:05:55 EDT 2018


Hi Neil

You 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).

I think this may be a hard problem, that looks easy.

I'm used to using a number theory computer algebra system
https://pari.math.u-bordeaux.fr/. Here's what it does with your
examples. (First I show that it's default number type is whole numbers
and fractions.)

$ gp
                                          GP/PARI CALCULATOR Version
2.5.5 (released)
? 1/2 + 1/2
%1 = 1
? 2^3
%2 = 8
? 2^(6/2)
%3 = 8

? 4^(1/2)
%4 = 2.0000000000000000000000000000000000000

? 1^(2/3)
%5 = 1.0000000000000000000000000000000000000

? (-1)^(2/3)
%6 = -0.50000000000000000000000000000000000000 +
0.86602540378443864676372317075293618347*I

? (0/1)^(2/3)
%7 = 0

This gives the same results as Python's Fraction, except for your
example [6]. There, it gives the Fraction(0) you ask for.

If the smart mathematicians and computer scientists that wrote gp/pari
get the same answers, it suggests to me that improvement would be
hard.

That said, a case can be made for the value given by [6] being a bug.
Or a poorly documented feature.

-- 
best regards

Jonathan


More information about the Python-ideas mailing list