无法让循环中断 [英] Trouble getting loop to break

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

问题描述

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


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

解决策案

2007年11月20日12:45,Dennis Lee Bieber写道:


> 2007年11月19日星期一23 :41:02 -0800,Dick Moores< rd *@rcblue.com>
在comp.lang.python中声明了以下内容:


a to way让它打破我想要的地方,即,当总和

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


如果总和> =限制:



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


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



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

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

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


Dick


而不是将sum与已知相比较。 e ** x的值,为什么不测试
的收敛?即,如果sum == last_sum:break。好像那样

会更强大(你不需要知道电脑答案的答案),因为看起来它应该会收敛。 />

--Nathan Davis


11月20日凌晨1点41分,Dick Moores< r ... @ rcblue.comwrote:


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


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


2007年11月20日上午10:42, da ***** ****@gmail.com 写道:


>而不是将总和与已知相比较。 e ** x的值,为什么不测试收敛?即,如果sum == last_sum:break。好像那样
会更健壮(你不需要知道电脑的答案答案),因为它似乎应该收敛。



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

好​​。请参阅< 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

解决方案

At 12:45 AM 11/20/2007, Dennis Lee Bieber wrote:

>On Mon, 19 Nov 2007 23:41:02 -0800, Dick Moores <rd*@rcblue.com>
declaimed the following in comp.lang.python:

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?

if sum >= limit:


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


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.

--Nathan Davis

On Nov 20, 1:41 am, Dick Moores <r...@rcblue.comwrote:

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


At 10:42 AM 11/20/2007, da*********@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.

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆