Trouble getting loop to break
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Tue Nov 20 17:23:34 EST 2007
On Tue, 20 Nov 2007 10:42:48 -0800, davisn90210 at gmail.com wrote:
> Instead of comparing sum to the "known" value of e**x, why not test for
> convergence? I.e., if sum == last_sum: break. Seems like that would be
> more robust (you don't need to know the answer to computer the answer),
> since it seems like it should converge.
That will only work if you know that the terms in your sequence are
monotonically decreasing: that is, if each term is smaller than the last.
It also assumes that the terms decrease reasonably rapidly, and you want
the full precision available in floats. Are you sure your algorithm is
that precise? It's one thing to produce 16 decimal places in your result,
but if only the first 12 of them are meaningful, and the last four are
inaccurate, you might as well not bother with the extra work.
--
Steven.
More information about the Python-list
mailing list