猿问

使用 Python 的 Pi

我是 Python 的初学者。我尝试用 Python 语言评估 pi。我在这里使用 阿基米德方法。


我的代码是:


import math

from decimal import *

getcontext().prec = 50


pi = 0

PolySides = 6

SideLen = 1

Perim = 6

Dia = 2


while 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 位。我不明白我在哪里失去了精度。谢谢你。


慕无忌1623718
浏览 195回答 3
3回答

撒科打诨

您必须使用.sqrt()小数法:from decimal import *getcontext().prec = 50pi = 0PolySides = 6SideLen = 1Perim = 6Dia = 2while PolySides < 1000000000000:&nbsp; &nbsp; SideLen2 = Decimal(SideLen) / Decimal(2)&nbsp; &nbsp; radius_a = (1 - SideLen2**2).sqrt()&nbsp; &nbsp; radius_b = Decimal(1) - Decimal(radius_a)&nbsp; &nbsp; SideLenNew = (SideLen2**2 + radius_b**2).sqrt()&nbsp; &nbsp; PolyCircum = Decimal(PolySides * SideLen)&nbsp; &nbsp; pi = PolyCircum / Dia&nbsp; &nbsp; print('Polygon Sides:', PolySides, 'Pi = ', pi)&nbsp; &nbsp; SideLen = SideLenNew&nbsp; &nbsp; PolySides = 2 * PolySides输出:Polygon Sides: 824633720832 Pi =&nbsp; 3.1415926535897932384626357839340399511415560167917有 22 位精确的小数。

有只小跳蛙

您正在编织进出之间float和decimal。math.sqrt接受 afloat并返回 a float,因此将进行转换,您的精度将被截断为float's。这些变化就足够了:while PolySides < 10000000000000000000:&nbsp; &nbsp; SideLen2 = Decimal(SideLen) / 2&nbsp; &nbsp; radius_a = (1 - SideLen2**2).sqrt()&nbsp; &nbsp; radius_b = 1 - radius_a&nbsp; &nbsp; SideLenNew = (SideLen2**2 + radius_b**2).sqrt()&nbsp; &nbsp; PolyCircum = Decimal(PolySides * SideLen)&nbsp; &nbsp; pi = PolyCircum / Dia&nbsp; &nbsp; print('Polygon Sides:', PolySides, 'Pi = ', pi)&nbsp; &nbsp; SideLen = SideLenNew&nbsp; &nbsp; PolySides = 2 * PolySides

慕神8447489

确保变量pi本身也是 a Decimal,并避免使用math.sqrt()@Amadan 写的那样。使用Decimal.sqrt()import mathfrom decimal import *getcontext().prec = 50pi = Decimal(0)PolySides = 6SideLen = 1Perim = 6Dia = 2while PolySides < 10000000000:&nbsp; &nbsp; SideLen2 = Decimal(SideLen) / Decimal(2)&nbsp; &nbsp; radius_a = Decimal((Decimal(1) - SideLen2**2).sqrt())&nbsp; &nbsp; radius_b = Decimal(1) - Decimal(radius_a)&nbsp; &nbsp; SideLenNew = Decimal((SideLen2**2 + radius_b**2).sqrt())&nbsp; &nbsp; PolyCircum = Decimal(PolySides * SideLen)&nbsp; &nbsp; pi = PolyCircum / Dia&nbsp; &nbsp; print('Polygon Sides:', PolySides, 'Pi = ', pi)&nbsp; &nbsp; SideLen = SideLenNew&nbsp; &nbsp; PolySides = 2 * PolySides给出:Polygon Sides: 6 Pi =&nbsp; 3Polygon Sides: 12 Pi =&nbsp; 3.1058285412302491481867860514885799401888268158392Polygon Sides: 24 Pi =&nbsp; 3.1326286132812381971617494694917362446497769154815Polygon Sides: 48 Pi =&nbsp; 3.1393502030468672071351468212084211891503505893626Polygon Sides: 96 Pi =&nbsp; 3.1410319508905096381113529264596601070364122161628Polygon Sides: 192 Pi =&nbsp; 3.1414524722854620754506093089612256452476623045496Polygon Sides: 384 Pi =&nbsp; 3.1415576079118576455164633451298595415043764795884Polygon Sides: 768 Pi =&nbsp; 3.1415838921483184086689696037211533505200449157810Polygon Sides: 1536 Pi =&nbsp; 3.1415904632280500957384585059309517235542823086758Polygon Sides: 3072 Pi =&nbsp; 3.1415921059992715505447766406101173531274972549662Polygon Sides: 6144 Pi =&nbsp; 3.1415925166921574475928740847688319059677188923680Polygon Sides: 12288 Pi =&nbsp; 3.1415926193653839551895493120653190422221927255946Polygon Sides: 24576 Pi =&nbsp; 3.1415926450336908966721415089192384127226230112419Polygon Sides: 49152 Pi =&nbsp; 3.1415926514507676517042536404922190204484274723852Polygon Sides: 98304 Pi =&nbsp; 3.1415926530550368416911231804154742022572768058866Polygon Sides: 196608 Pi =&nbsp; 3.1415926534561041392646431596150783313543414874292Polygon Sides: 393216 Pi =&nbsp; 3.1415926535563709636628233165541133642749135041839Polygon Sides: 786432 Pi =&nbsp; 3.1415926535814376697626683659225751788703495073714Polygon Sides: 1572864 Pi =&nbsp; 3.1415926535877043462876483788980471857502408672475Polygon Sides: 3145728 Pi =&nbsp; 3.1415926535892710154188945540564999738004063788314Polygon Sides: 6291456 Pi =&nbsp; 3.1415926535896626827017061710907747199859793791592Polygon Sides: 12582912 Pi =&nbsp; 3.1415926535897605995224090799271347533561151459628Polygon Sides: 25165824 Pi =&nbsp; 3.1415926535897850787275848074223367208751396830886Polygon Sides: 50331648 Pi =&nbsp; 3.1415926535897911985288787393140192102034265840860Polygon Sides: 100663296 Pi =&nbsp; 3.1415926535897927284792022222880574573760314838880Polygon Sides: 201326592 Pi =&nbsp; 3.1415926535897931109667830930316368707217160322736Polygon Sides: 402653184 Pi =&nbsp; 3.1415926535897932065886783107175360897801705020852Polygon Sides: 805306368 Pi =&nbsp; 3.1415926535897932304941521151390111674024112028327Polygon Sides: 1610612736 Pi =&nbsp; 3.1415926535897932364705205662443799538615730707255Polygon Sides: 3221225472 Pi =&nbsp; 3.1415926535897932379646126790207221515422136434928Polygon Sides: 6442450944 Pi =&nbsp; 3.1415926535897932383381357072148077010289894182968
随时随地看视频慕课网APP

相关分类

Python
我要回答