加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

python – 麻省理工学院开放课程,第3讲 – 数学问题

发布时间:2020-12-14 17:26:48 所属栏目:百科 来源:网络整理
导读:关于MIT OCW Python第3讲,我遇到了问题. 根据简单的数学计算,她使用的代码不应该是成功的. ## EXAMPLE: approximate cube root #####################cube = 27##cube = 8120601##cube = 10000#epsilon = 0.1#guess = 0.0#increment = 0.01#num_guesses = 0#
关于MIT OCW Python第3讲,我遇到了问题.

根据简单的数学计算,她使用的代码不应该是成功的.

## EXAMPLE: approximate cube root 
####################
#cube = 27
##cube = 8120601
##cube = 10000
#epsilon = 0.1
#guess = 0.0
#increment = 0.01
#num_guesses = 0
## look for close enough answer and make sure
## didn't accidentally skip the close enough bound
#while abs(guess**3 - cube) >= epsilon and guess <= cube:
#    guess += increment
#    num_guesses += 1
#print('num_guesses =',num_guesses)
#if abs(guess**3 - cube) >= epsilon:
#    print('Failed on cube root of',cube,"with these parameters.")
#else:
#    print(guess,'is close to the cube root of',cube)

这是她使用的代码,我遇到的问题是理解这部分:

while abs(guess**3 - cube) >= epsilon and guess <= cube:
#    guess += increment

如果guess是0.0,cube是27并且increment是0.01那么这个术语的数学应该是:

abs(0**3 - 27) = 27 #----- This is fine according to the code but the next step would be:#
abs(27.01**3 - 27) = 19677.878101

这应该阻止循环进一步工作.我的理解在某处显然是错误的,但我看不到哪里!

请停下来……

解决方法

您可以尝试使用Python可视化工具来查看猜测值如何随每次迭代而变化. Try here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读