Why is math.pi slightly wrong?

Dan Upton upton at virginia.edu
Thu May 22 15:06:01 EDT 2008


On Thu, May 22, 2008 at 2:53 PM, Mensanator <mensanator at aol.com> wrote:
> On May 22, 11:32 am, "Dutton, Sam" <Sam.Dut... at itn.co.uk> wrote:
>> I've noticed that the value of math.pi -- just entering it at the interactive prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive knows) the value is 3.1415926535897932... (Note the 2 at the end.)
>>
>> Is this a precision issue, or from the underlying C, or something else? How is math.pi calculated?
>
> If you actually need that many digits, use a different library.
>
>>>> import gmpy
>
>>>> print gmpy.pi(64) # 64 bit precision
> 3.14159265358979323846
>>>> print gmpy.pi(128) # 128 bit precision
> 3.141592653589793238462643383279502884197
>>>> print gmpy.pi(16384) # 16384 bit precision
> 3.14159265358979323846264338327950288419716939937510582097494459
> 2307816406286208998628034825342117067982148086513282306647093844
> 6095505822317253594081284811174502841027019385211055596446229489
> 5493038196442881097566593344612847564823378678316527120190914564
> 8566923460348610454326648213393607260249141273724587006606315588
> 1748815209209628292540917153643678925903600113305305488204665213
> 8414695194151160943305727036575959195309218611738193261179310511
> 8548074462379962749567351885752724891227938183011949129833673362
> 4406566430860213949463952247371907021798609437027705392171762931
> 7675238467481846766940513200056812714526356082778577134275778960
> 9173637178721468440901224953430146549585371050792279689258923542
> 0199561121290219608640344181598136297747713099605187072113499999
> 9837297804995105973173281609631859502445945534690830264252230825
> 3344685035261931188171010003137838752886587533208381420617177669
> 1473035982534904287554687311595628638823537875937519577818577805
> 3217122680661300192787661119590921642019893809525720106548586327
> 8865936153381827968230301952035301852968995773622599413891249721
> 7752834791315155748572424541506959508295331168617278558890750983
> 8175463746493931925506040092770167113900984882401285836160356370
> 7660104710181942955596198946767837449448255379774726847104047534
> 6462080466842590694912933136770289891521047521620569660240580381
> 5019351125338243003558764024749647326391419927260426992279678235
> 4781636009341721641219924586315030286182974555706749838505494588
> 5869269956909272107975093029553211653449872027559602364806654991
> 1988183479775356636980742654252786255181841757467289097777279380
> 0081647060016145249192173217214772350141441973568548161361157352
> 5521334757418494684385233239073941433345477624168625189835694855
> 6209921922218427255025425688767179049460165346680498862723279178
> 6085784383827967976681454100953883786360950680064225125205117392
> 9848960841284886269456042419652850222106611863067442786220391949
> 4504712371378696095636437191728746776465757396241389086583264599
> 5813390478027590099465764078951269468398352595709825822620522489
> 4077267194782684826014769909026401363944374553050682034962524517
> 4939965143142980919065925093722169646151570985838741059788595977
> 2975498930161753928468138268683868942774155991855925245953959431
> 0499725246808459872736446958486538367362226260991246080512438843
> 9045124413654976278079771569143599770012961608944169486855584840
> 6353422072225828488648158456028506016842739452267467678895252138
> 5225499546667278239864565961163548862305774564980355936345681743
> 2411251507606947945109659609402522887971089314566913686722874894
> 0560101503308617928680920874760917824938589009714909675985261365
> 5497818931297848216829989487226588048575640142704775551323796414
> 5152374623436454285844479526586782105114135473573952311342716610
> 2135969536231442952484937187110145765403590279934403742007310578
> 5390621983874478084784896833214457138687519435064302184531910484
> 8100537061468067491927819119793995206141966342875444064374512371
> 8192179998391015919561814675142691239748940907186494231961567945
> 2080951465502252316038819301420937621378559566389377870830390697
> 9207734672218256259966150142150306803844773454920260541466592520
> 1497442850732518666002132434088190710486331734649651453905796268
> 5610055081066587969981635747363840525714591028970641401109712062
> 8043903975951567715770042033786993600723055876317635942187312514
> 7120532928191826186125867321579198414848829164470609575270695722
> 0917567116722910981690915280173506712748583222871835209353965725
> 1210835791513698820914442100675103346711031412671113699086585163
> 9831501970165151168517143765761835155650884909989859982387345528
> 3316355076479185358932261854896321329330898570642046752590709154
> 8141654985946163718027098199430992448895757128289059232332609729
> 9712084433573265489382391193259746366730583604142813883032038249
> 0375898524374417029132765618093773444030707469211201913020330380
> 1976211011004492932151608424448596376698389522868478312355265821
> 3144957685726243344189303968642624341077322697802807318915441101
> 0446823252716201052652272111660396665573092547110557853763466820
> 6531098965269186205647693125705863566201855810072936065987648611
> 7910453348850346113657686753249441668039626579787718556084552965
> 4126654085306143444318586769751456614068007002378776591344017127
> 4947042056223053899456131407112700040785473326993908145466464588
> 0797270826683063432858785698305235808933065757406795457163775254
> 2021149557615814002501262285941302164715509792592309907965473761
> 2551765675135751782966645477917450112996148903046399471329621073
> 4043751895735961458901938971311179042978285647503203198691514028
> 7080859904801094121472213179476477726224142548545403321571853061
> 4228813758504306332175182979866223717215916077166925474873898665
> 4949450114654062843366393790039769265672146385306736096571209180
> 7638327166416274888800786925602902284721040317211860820419000422
> 9661711963779213375751149595015660496318629472654736425230817703
> 6751590673502350728354056704038674351362222477158915049530984448
> 9333097

Who wants to verify that that's correct to that many digits? ;)



More information about the Python-list mailing list