Python 3.8 breaks backward compatibility with math.factorial(Decimal(int))

kirby urner <[email protected]> Tue, 18 Feb 2020 13:39:02 -0800
Newsgroups gmane.comp.python.education
Message-ID <CAPJgG3SicbDaqLxkoEmosCP=Wzby9R7iyp_nNsq59jkt=7+3rg@mail.gmail.com>
Now you'll need to use math.factorial(int(Decimal)) or write your own
factorial function.

The storyline:

Apropos of recent threads experimenting with high precision (arbitrary
precision) numbers as a way to promote interest in "pure math" applications
using Python, I discovered this morning that my Ramanujan Convergence
script on repl.it <https://repl.it/@kurner/computepi> was broken all of a
sudden (after working before), and set out to discover why.

Answer:  in Python 3.8, to which repl.it has newly upgraded, math.factorial
no longer accepts Decimal type numbers, even if they're integral
(integers).  You can read the discussion here:
https://bugs.python.org/issue33083

Here's the line of code that broke, and was fixed with explicit castings to
int.

term = (fact(int(c1*i))*(c2 + c3*i))/(pow(fact(int(i)),4)*pow(c4,4*i))

By the time of the final division, the numerator and denominator have been
coerced back into Decimals and so will divide with the expected precision
(set by the context):  Where:

```
    c1 = Decimal(4)
    c2 = Decimal(1103)
    c3 = Decimal(26390)
    c4 = Decimal(396)
    c5 = Decimal(9801)
```
For more context:

https://github.com/4dsolutions/Python5/blob/master/Pi%20Day%20Fun.ipynb
(I'm updating it now...)

Kirby

_______________________________________________
Edu-sig mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/edu-sig.python.org/