python使用三角迭代计算圆周率PI的方法

作者:不吃皮蛋 时间:2021-12-29 06:47:02 

本文实例讲述了python使用三角迭代计算圆周率PI的方法。分享给大家供大家参考。具体如下:

方法1:

# Calculating PI using trigonometric iterations
# FB36 - 20130825
import math
x = 1.0
y = 1.0
z = 1.0
w = 1.0
v = 1.0
u = 1.0
for i in range(30):
 
    x = math.sin(x) + x
    y = math.cos(y) + y
    z = math.cos(z) + math.sin(z) + z
    w = math.cos(w) - math.sin(w) + w
    v =  math.cos(v) * math.sin(v) + v
    u =  math.cos(u) / math.sin(u) + u
    print i
    print x, y * 2.0, z * 4.0 / 3.0, w * 4.0, v * 2.0, u * 2.0
    print

方法2:

# Calculating PI using trigonometric iterations
# FB36 - 20130901
import math
def sin2(x):
    return ((math.e ** complex(0.0, x) - math.e ** complex(0.0, -x)) / 2.0).imag
def cos2(x):
    return ((math.e ** complex(0.0, x) + math.e ** complex(0.0, -x)) / 2.0).real
x = 1.0
y = 1.0
x2 = 1.0
y2 = 1.0
for i in range(5):
    x = math.sin(x) + x
    y = math.cos(y) + y
    x2 = sin2(x2) + x2
    y2 = cos2(y2) + y2
    print i, x, x2, y * 2.0, y2 * 2.0

希望本文所述对大家的Python程序设计有所帮助。

标签:python,迭代,计算,方法
0
投稿

猜你喜欢

  • MATLAB数学建模之画图汇总

    2023-06-14 06:49:50
  • Python3实现转换Image图片格式

    2021-06-06 21:04:25
  • python实现根据图标提取分类应用程序实例

    2022-07-23 16:22:39
  • python实现nao机器人身体躯干和腿部动作操作

    2021-07-02 07:39:47
  • Python实现单例模式的5种方法

    2021-07-13 19:40:08
  • 关于Python包导入报错的问题总结

    2021-02-05 18:13:46
  • 利用python实现命令行有道词典的方法示例

    2021-03-24 06:32:16
  • pyqt5 使用cv2 显示图片,摄像头的实例

    2023-09-28 10:33:24
  • 利用pandas进行大文件计数处理的方法

    2021-05-20 02:36:55
  • python偏函数的实例用法总结

    2021-06-05 03:42:13
  • Django卸载之后重新安装的方法

    2023-07-23 18:27:58
  • 对SQL Server数据库进行优化的经验总结

    2010-07-26 14:52:00
  • PHP header()函数使用详细(301、404等错误设置)

    2023-11-02 17:28:23
  • Python3.7 新特性之dataclass装饰器

    2021-05-11 13:13:40
  • k-means 聚类算法与Python实现代码

    2022-02-01 02:55:22
  • 从事设计行业的十年

    2008-04-01 09:44:00
  • Python Threading 线程/互斥锁/死锁/GIL锁

    2021-03-24 12:21:25
  • python函数缺省值与引用学习笔记分享

    2023-10-22 19:43:20
  • js断点调试经验分享

    2023-08-15 06:19:09
  • 在SQL查询中使用LIKE来代替IN查询的方法

    2011-09-30 11:10:18
  • asp之家 网络编程 m.aspxhome.com