本文介绍了无法让循环中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个无限系列的演示


x ** 0/0! + x ** 1/1! + x ** 2/2! + x ** 3/3! + ... = e ** x(x是非负的)


对许多x都可以正常工作,但对于很多人而言,循环并没有中断。有没有

a的方式让它在我想要的地方打破,也就是说,当总和

等于限制时,精确度允许?


这就是我所拥有的:


======= series_xToN_OverFactorialN.py ============= =============

#!/ usr / bin / env python

#coding = utf-8

#series_xToN_OverFactorialN.py限制是来自p.63的e ** x

快乐的Pi,e

来自mpmath import mpf,e,exp,factorial

导入数学

导入时间

精度= 100

mpf.dps =精度

n = mpf(0)

x = mpf(raw_input("输入非负int或浮点数:))

term = 1

sum = 0

limit = e ** x

k = 0

而True:

k + = 1

term = x ** n / factorial(n)

sum + = term

print" sum =%s k =%d %(sum,k)

print" exp(%s)=%s" %(x,exp(x))

print" e **%s =%s %(x,e ** x)

打印

如果总和> =限制:

print" math.e **% s =%f %(x,math.e ** x)

print" last term =%s" %term

break

time.sleep(0.2)

n + = 1


" "

输出x == mpf(123.45):

sum =

41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2

K = 427

EXP(123.45)=

41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2
$ b $是** 123.45 =

41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2

"""

============================ ====================== ==


这也是在网上的< http:// python .pastebin.com / f1a5b9e03> ;.


问题x的例子:10,20,30,40,100,101

OK的例子x'的:0.2,5,10.1,11,33.3,123.45


谢谢,


Dick Moores

解决策案



嗯,因为它是ISN''用浮标测试绝对等价的情况......


或许放一个打印总和,限制在此之前会揭示您遇到的是什么类型的价值观。



如果你运行程序,你会看到,如果我理解你

正确。 < http://python.pastebin.com/f2f06fd76显示完整的

输出,精度为50,x == 5.


Dick





是的!不管你信不信我在看到你的帖子之前就这么做了。工作

好​​。请参阅< http://python.pastebin.com/f7c37186​​a>


以及pi的惊人Chudnovsky算法。参见

< http://python.pastebin.com/f4410f3dc>


谢谢,


迪克


I''m writing a demo of the infinite series

x**0/0! + x**1/1! + x**2/2! + x**3/3! + ... = e**x (x is non-negative)

It works OK for many x, but for many the loop doesn''t break. Is there
a way to get it to break where I want it to, i.e., when the sum
equals the limit as closely as the precision allows?

Here''s what I have:

======= series_xToN_OverFactorialN.py ==========================
#!/usr/bin/env python
#coding=utf-8
# series_xToN_OverFactorialN.py limit is e**x from p.63 in The
Pleasures of Pi,e
from mpmath import mpf, e, exp, factorial
import math
import time
precision = 100
mpf.dps = precision
n = mpf(0)
x = mpf(raw_input("Enter a non-negative int or float: "))
term = 1
sum = 0
limit = e**x
k = 0
while True:
k += 1
term = x**n/factorial(n)
sum += term
print " sum = %s k = %d" % (sum, k)
print "exp(%s) = %s" % (x, exp(x))
print " e**%s = %s" % (x, e**x)
print
if sum >= limit:
print "math.e**%s = %f" % (x, math.e**x)
print "last term = %s" % term
break
time.sleep(0.2)
n += 1

"""
Output for x == mpf(123.45):
sum =
41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2
k = 427
exp(123.45) =
41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2
e**123.45 =
41082209310966896423914890844331787613887964701399 5774.295143146627078225759757325948668733624698494 2
"""
================================================== ==

This is also on the web at <http://python.pastebin.com/f1a5b9e03>.

Examples of problem x''s: 10, 20, 30, 40, 100, 101
Examples of OK x''s: 0.2, 5, 10.1, 11, 33.3, 123.45

Thanks,

Dick Moores

解决方案


Well, since it ISN''T a case of testing for an absolute equivalence
with floats...

Perhaps putting a "print sum, limit" before that point would reveal
what type of values you are encountering.

If you run the program you''ll see exactly that, if I understand you
correctly. <http://python.pastebin.com/f2f06fd76shows the full
output for a precision of 50 and x == 5.

Dick



Yes! And believe it or not I did that before seeing your post. Works
well. See <http://python.pastebin.com/f7c37186a>

And also with the amazing Chudnovsky algorithm for pi. See
<http://python.pastebin.com/f4410f3dc>

Thanks,

Dick


这篇关于无法让循环中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:51