Python求解平方根的方法
作者:苏巴什 时间:2023-02-13 13:25:47
本文实例讲述了Python求解平方根的方法。分享给大家供大家参考。具体如下:
主要通过SICP的内容改写而来。基于newton method求解平方根。代码如下:
#!/usr/bin/python
def sqrt_iter(guess,x):
if(good_enough(guess, x)):
print guess
else:
sqrt_iter(improve(guess, x),x)
def improve(guess, x):
return average(guess, x/guess)
def average(x,y):
return (x+y)/2
def good_enough(guess,x):
if(abs(guess * guess -x) < 0.0001):
return True
else:
return False
def sqrt_oliver(x):
sqrt_iter(1.0,x)
sqrt_oliver(5)
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,平方根,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python进行数据提取的方法总结
2022-06-23 18:02:45
![](https://img.aspxhome.com/file/2023/3/111963_0s.jpg)
Python实现人机中国象棋游戏
2023-01-28 21:33:52
![](https://img.aspxhome.com/file/2023/9/133489_0s.jpg)
MySql闪退和服务无法启动的解决方法
2024-01-21 07:53:12
![](https://img.aspxhome.com/file/2023/1/121591_0s.jpg)
如何获取当前 select 元素的值
2010-03-29 13:07:00
sqlserver服务器验证改为混合验证模式步骤
2024-01-20 18:02:28
![](https://img.aspxhome.com/file/2023/7/110957_0s.jpg)
SQL 查询语句积累
2024-01-27 06:32:18
python实现简单俄罗斯方块游戏
2023-04-13 03:28:05
![](https://img.aspxhome.com/file/2023/4/85044_0s.jpg)
卷积神经网络如何实现提取特征
2022-05-10 00:39:24
![](https://img.aspxhome.com/file/2023/8/64648_0s.png)
解决Keras TensorFlow 混编中 trainable=False设置无效问题
2022-10-23 16:56:19
SQL-ORDER BY 多字段排序(升序、降序)
2024-01-28 05:25:55
Python教程之pytest命令行方式运行用例
2022-08-01 22:08:06
![](https://img.aspxhome.com/file/2023/1/81531_0s.png)
MySQL 定时器EVENT学习
2010-10-14 13:55:00
如何通俗的解释TypeScript 泛型
2024-04-10 16:18:31
pygame学习笔记(2):画点的三种方法和动画实例
2021-09-02 19:59:51
![](https://img.aspxhome.com/file/2023/2/134452_0s.jpg)
两行 JavaScript 代码
2010-08-31 14:57:00
MySQL高并发生成唯一订单号的方法实现
2024-01-28 00:27:38
![](https://img.aspxhome.com/file/2023/3/99973_0s.png)
如何在Cloud Studio上执行Python代码?
2023-12-23 02:12:15
![](https://img.aspxhome.com/file/2023/9/97289_0s.png)
详解Pytest测试用例的执行方法
2022-02-15 18:28:14
![](https://img.aspxhome.com/file/2023/4/68434_0s.png)
使用批处理脚本自动生成并上传NuGet包(操作方法)
2021-12-24 18:05:34
![](https://img.aspxhome.com/file/2023/7/105707_0s.png)
SQL2005学习笔记 APPLY 运算符
2024-01-27 16:39:22