为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python 的 Pi

使用 Python 的 Pi

慕无忌1623718 2021-06-03 18:13:54
我是 Python 的初学者。我尝试用 Python 语言评估 pi。我在这里使用 阿基米德方法。我的代码是:import mathfrom decimal import *getcontext().prec = 50pi = 0PolySides = 6SideLen = 1Perim = 6Dia = 2while PolySides < 10000000000:    SideLen2 = Decimal(SideLen) / Decimal(2)    radius_a = Decimal(math.sqrt(1 - SideLen2**2))    radius_b = Decimal(1) - Decimal(radius_a)    SideLenNew = Decimal(math.sqrt(SideLen2**2 + radius_b**2))    PolyCircum = Decimal(PolySides * SideLen)    pi = PolyCircum / Dia    print('Polygon Sides:', PolySides, 'Pi = ', pi)    SideLen = SideLenNew    PolySides = 2 * PolySides但出于某种原因,此代码将 pi 打印到小数点后 14 位。我不明白我在哪里失去了精度。谢谢你。
查看完整描述

3 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

您必须使用.sqrt()小数法:


from decimal import *

getcontext().prec = 50


pi = 0

PolySides = 6

SideLen = 1

Perim = 6

Dia = 2


while PolySides < 1000000000000:

    SideLen2 = Decimal(SideLen) / Decimal(2)

    radius_a = (1 - SideLen2**2).sqrt()

    radius_b = Decimal(1) - Decimal(radius_a)

    SideLenNew = (SideLen2**2 + radius_b**2).sqrt()

    PolyCircum = Decimal(PolySides * SideLen)

    pi = PolyCircum / Dia

    print('Polygon Sides:', PolySides, 'Pi = ', pi)

    SideLen = SideLenNew

    PolySides = 2 * PolySides

输出:


Polygon Sides: 824633720832 Pi =  3.1415926535897932384626357839340399511415560167917

有 22 位精确的小数。


查看完整回答
反对 回复 2021-06-06
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

您正在编织进出之间float和decimal。math.sqrt接受 afloat并返回 a float,因此将进行转换,您的精度将被截断为float's。


这些变化就足够了:


while PolySides < 10000000000000000000:

    SideLen2 = Decimal(SideLen) / 2

    radius_a = (1 - SideLen2**2).sqrt()

    radius_b = 1 - radius_a

    SideLenNew = (SideLen2**2 + radius_b**2).sqrt()

    PolyCircum = Decimal(PolySides * SideLen)

    pi = PolyCircum / Dia

    print('Polygon Sides:', PolySides, 'Pi = ', pi)

    SideLen = SideLenNew

    PolySides = 2 * PolySides


查看完整回答
反对 回复 2021-06-06
?
慕神8447489

TA贡献1780条经验 获得超1个赞

确保变量pi本身也是 a Decimal,并避免使用math.sqrt()@Amadan 写的那样。使用Decimal.sqrt()


import math

from decimal import *

getcontext().prec = 50


pi = Decimal(0)

PolySides = 6

SideLen = 1

Perim = 6

Dia = 2


while PolySides < 10000000000:

    SideLen2 = Decimal(SideLen) / Decimal(2)

    radius_a = Decimal((Decimal(1) - SideLen2**2).sqrt())

    radius_b = Decimal(1) - Decimal(radius_a)

    SideLenNew = Decimal((SideLen2**2 + radius_b**2).sqrt())

    PolyCircum = Decimal(PolySides * SideLen)

    pi = PolyCircum / Dia

    print('Polygon Sides:', PolySides, 'Pi = ', pi)

    SideLen = SideLenNew

    PolySides = 2 * PolySides

给出:


Polygon Sides: 6 Pi =  3

Polygon Sides: 12 Pi =  3.1058285412302491481867860514885799401888268158392

Polygon Sides: 24 Pi =  3.1326286132812381971617494694917362446497769154815

Polygon Sides: 48 Pi =  3.1393502030468672071351468212084211891503505893626

Polygon Sides: 96 Pi =  3.1410319508905096381113529264596601070364122161628

Polygon Sides: 192 Pi =  3.1414524722854620754506093089612256452476623045496

Polygon Sides: 384 Pi =  3.1415576079118576455164633451298595415043764795884

Polygon Sides: 768 Pi =  3.1415838921483184086689696037211533505200449157810

Polygon Sides: 1536 Pi =  3.1415904632280500957384585059309517235542823086758

Polygon Sides: 3072 Pi =  3.1415921059992715505447766406101173531274972549662

Polygon Sides: 6144 Pi =  3.1415925166921574475928740847688319059677188923680

Polygon Sides: 12288 Pi =  3.1415926193653839551895493120653190422221927255946

Polygon Sides: 24576 Pi =  3.1415926450336908966721415089192384127226230112419

Polygon Sides: 49152 Pi =  3.1415926514507676517042536404922190204484274723852

Polygon Sides: 98304 Pi =  3.1415926530550368416911231804154742022572768058866

Polygon Sides: 196608 Pi =  3.1415926534561041392646431596150783313543414874292

Polygon Sides: 393216 Pi =  3.1415926535563709636628233165541133642749135041839

Polygon Sides: 786432 Pi =  3.1415926535814376697626683659225751788703495073714

Polygon Sides: 1572864 Pi =  3.1415926535877043462876483788980471857502408672475

Polygon Sides: 3145728 Pi =  3.1415926535892710154188945540564999738004063788314

Polygon Sides: 6291456 Pi =  3.1415926535896626827017061710907747199859793791592

Polygon Sides: 12582912 Pi =  3.1415926535897605995224090799271347533561151459628

Polygon Sides: 25165824 Pi =  3.1415926535897850787275848074223367208751396830886

Polygon Sides: 50331648 Pi =  3.1415926535897911985288787393140192102034265840860

Polygon Sides: 100663296 Pi =  3.1415926535897927284792022222880574573760314838880

Polygon Sides: 201326592 Pi =  3.1415926535897931109667830930316368707217160322736

Polygon Sides: 402653184 Pi =  3.1415926535897932065886783107175360897801705020852

Polygon Sides: 805306368 Pi =  3.1415926535897932304941521151390111674024112028327

Polygon Sides: 1610612736 Pi =  3.1415926535897932364705205662443799538615730707255

Polygon Sides: 3221225472 Pi =  3.1415926535897932379646126790207221515422136434928

Polygon Sides: 6442450944 Pi =  3.1415926535897932383381357072148077010289894182968


查看完整回答
反对 回复 2021-06-06
  • 3 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信