Message307892
I'm new with Python and I've been blocked for day on a "TypeError: 'Fraction' object is not iterable" error message, while the problem turned out to be completely different.
I don't even know to what programming case this message would have been the right interpretation, but in this situation, it was completely wrong and I believe it can cause much loss of time for inexperienced python programmer (at least, I believe it should document alternative causes)
Here is a sample code to show how misleading it can be (It's a program to compute pi with fractions)
from fractions import Fraction
def order(x):
r, old_r, n, old_n = 2, 1, 1, 0
while (x>=r):
r, old_r, n, old_n = r*r, r, 2*n, n
return order(x >> old_n) + old_n if old_n > 0 else 0
def terms(m, n, i):
return Fraction(4 * m, n**(2*i+1) * (2*i+1))
def terms_generator(exp_prec):
ws = [ [terms(parm[1], parm[2], 0), 0] + list(parm)
for parm in ((1, 44, 57),
(1, 7, 239),
(-1, 12, 682),
(1, 24, 12943))]
digits = 0
while digits<exp_prec:
curws = max(ws, key=lambda col: col[0])
digits = int(0.30103 *
(order(curws[0].denominator))
- order(curws[0].numerator))
yield curws[2] * curws[0] #, digits
curws[2] = -curws[2]
curws[1] += 1
curws[0] = terms(curws[3], curws[4], curws[1])
expected_precision = 100
pi = 0
for term, dgts in terms_generator(expected_precision):
pi += term
print("{} digits".format(dgts))
print("pi = 3.{}".format(int((pi-3)*10**expected_precision)))
Obviously, the problem came from having forgotten one argument in the "yield" line, which has nothing to do with a problem of iterability of my type. |
|
Date |
User |
Action |
Args |
2017-12-09 12:10:32 | Camion | set | recipients:
+ Camion |
2017-12-09 12:10:32 | Camion | set | messageid: <1512821432.36.0.213398074469.issue32259@psf.upfronthosting.co.za> |
2017-12-09 12:10:32 | Camion | link | issue32259 messages |
2017-12-09 12:10:31 | Camion | create | |
|