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

yipeiwu_com5年前Python基础

本文实例讲述了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代码打包为jar软件的简单方法

py 写东西快 但是java 生态广 比如大数据 py 虽然好 但是利用不到java的整个的生态的代码 scala 虽然也好但是毕竟 有些库 需要自己写的多 虽然也很简单 ,但是查文档也...

Python3基础之输入和输出实例分析

通常来说,一个Python程序可以从键盘读取输入,也可以从文件读取输入;而程序的结果可以输出到屏幕上,也可以保存到文件中便于以后使用。本文就来介绍Python中最基本的I/O函数。 一、...

Python字符串大小写转换拼接删除空白

1.字符串大小写转换 string.title() #将字符串中所有单词的首字母以大写形式显示 string.upper() #将字符串中所有字母转化为大写字母 stri...

python 使用sys.stdin和fileinput读入标准输入的方法

1、使用sys.stdin 读取标准输入 [root@c6-ansible-20 script]# cat demo02.py #! /usr/bin/env python fro...

win10系统下python3安装及pip换源和使用教程

win10系统下python3安装及pip换源和使用教程

一、python3的安装 建议安装python3,python2在未来将不再维护。 python官方下载地址 https://www.python.org/downloads/windo...