Skip to content

Instantly share code, notes, and snippets.

@purarue
Created February 9, 2023 07:14
Show Gist options
  • Save purarue/2d550a52dac052b0f3cfac038a2a6afc to your computer and use it in GitHub Desktop.
Save purarue/2d550a52dac052b0f3cfac038a2a6afc to your computer and use it in GitHub Desktop.
from typing import Iterator, TypeVar
from decimal import Decimal
it = int(input("Enter number of iterations: "))
def calculate_pi(till: int, factory: TypeVar) -> Iterator[int]:
approx = factory(0)
denom = factory(1)
for i in range(till):
if i % 2 == 0:
approx += factory(1) / denom
else:
approx -= factory(1) / denom
denom += 2
yield approx * 4
int_pi = calculate_pi(it, int)
dec_pi = calculate_pi(it, Decimal)
for i, (int_res, dec_res) in enumerate(zip(int_pi, dec_pi)):
print(f"int: {int_res} | dec: {dec_res} | diff: {abs(dec_res - Decimal(int_res))}")
if i >= it:
break
@purarue
Copy link
Author

purarue commented Feb 9, 2023

int: 2.666666666666667 | dec: 2.666666666666666666666666667 | diff: 2.960594732330417441129684448E-16
int: 3.466666666666667 | dec: 3.466666666666666666666666667 | diff: 1.184237892930166976451873779E-16
int: 2.8952380952380956 | dec: 2.895238095238095238095238095 | diff: 3.721890520650524783134460449E-16
int: 3.3396825396825403 | dec: 3.339682539682539682539682540 | diff: 5.695620342200803077220916748E-16
int: 2.9760461760461765 | dec: 2.976046176046176046176046176 | diff: 4.484467951704268669486045837E-16
int: 3.2837384837384844 | dec: 3.283738483738483738483738484 | diff: 6.875717543201528910398483276E-16
int: 3.017071817071818 | dec: 3.017071817071817071817071817 | diff: 7.467836489671612398624420166E-16
int: 3.2523659347188767 | dec: 3.252365934718875895346483582 | diff: 7.990294383609921358823776245E-16
int: 3.0418396189294032 | dec: 3.041839618929402211135957266 | diff: 1.032760601440182986497879028E-15
int: 3.232315809405594 | dec: 3.232315809405592687326433456 | diff: 1.244231653750212803721427917E-15
int: 3.058402765927333 | dec: 3.058402765927331817761216065 | diff: 1.360081012841229138374328613E-15
int: 3.2184027659273333 | dec: 3.218402765927331817761216065 | diff: 1.502189559993249175548553467E-15
int: 3.0702546177791854 | dec: 3.070254617779183669613067917 | diff: 1.732458039174614976525306702E-15
int: 3.208185652261944 | dec: 3.208185652261942290302723089 | diff: 1.564010407852591225564479828E-15
int: 3.079153394197428 | dec: 3.079153394197426161270465025 | diff: 1.678614074910155771672725677E-15
int: 3.200365515409549 | dec: 3.200365515409547373391677146 | diff: 1.570956084643686046540737152E-15
int: 3.0860798011238346 | dec: 3.086079801123833087677391432 | diff: 1.532891295227680679440498352E-15
int: 3.1941879092319425 | dec: 3.194187909231941195785499540 | diff: 1.316847895841271839141845703E-15
int: 3.09162380666784 | dec: 3.091623806667838631682935437 | diff: 1.237139576125029831111431122E-15
int: 3.189184782277596 | dec: 3.189184782277594729243911047 | diff: 1.323791129266261561095714569E-15
int: 3.0961615264636424 | dec: 3.096161526463641240871818024 | diff: 1.158548632577610355079174042E-15
int: 3.1850504153525314 | dec: 3.185050415352530129760706912 | diff: 1.286841070979628444194793701E-15
int: 3.099944032373808 | dec: 3.099944032373806725505387764 | diff: 1.164007885275483465254306793E-15
int: 3.1815766854350325 | dec: 3.181576685435031215301306131 | diff: 1.254638336265639101207256317E-15
int: 3.1031453128860127 | dec: 3.103145312886011607458168876 | diff: 1.089193336517674597144126892E-15
int: 3.1786170109992202 | dec: 3.178617010999219154627980197 | diff: 1.072435253126860913515090942E-15
int: 3.1058897382719475 | dec: 3.105889738271946427355252924 | diff: 1.048212205317130225360393524E-15
int: 3.1760651768684385 | dec: 3.176065176868437655425428362 | diff: 8.222720810080457364320755005E-16
int: 3.108268566698947 | dec: 3.108268566698946130001699549 | diff: 1.002918539251579681992530823E-15
int: 3.1738423371907505 | dec: 3.173842337190749408690224139 | diff: 1.075720049063229291200637817E-15
int: 3.110350273698687 | dec: 3.110350273698685916626732075 | diff: 1.301289171527261096239089966E-15
int: 3.1718887352371485 | dec: 3.171888735237147455088270537 | diff: 1.082660637446768731355667114E-15
int: 3.112187242699835 | dec: 3.112187242699834022252449641 | diff: 9.103275112367742832899093628E-16
int: 3.1701582571925884 | dec: 3.170158257192587645440855438 | diff: 7.236813215897479663491249084E-16
int: 3.1138202290235744 | dec: 3.113820229023573560933813185 | diff: 7.987386528314909437298774719E-16
int: 3.1686147495715193 | dec: 3.168614749571518766413265240 | diff: 5.736523409894729053974151611E-16
int: 3.115281416238186 | dec: 3.115281416238185433079931906 | diff: 6.743125618894870983958244324E-16
int: 3.167229468186238 | dec: 3.167229468186237381131879958 | diff: 6.916147388965804470777511597E-16
int: 3.116596556793833 | dec: 3.116596556793832317840740718 | diff: 8.658775933941999546885490417E-16
int: 3.1659792728432157 | dec: 3.165979272843215033890123434 | diff: 6.410916970503904823064804077E-16
int: 3.117786501758878 | dec: 3.117786501758877684492533072 | diff: 4.859279972238143875002861023E-16
int: 3.1648453252882898 | dec: 3.164845325288289449198415425 | diff: 3.187414711625555202364921570E-16
int: 3.118868313794037 | dec: 3.118868313794036575635197034 | diff: 3.748906816035634372234344482E-16
int: 3.163812134018756 | dec: 3.163812134018755676758792540 | diff: 4.846655424649946457147598267E-16
int: 3.1198560900627124 | dec: 3.119856090062711720714836496 | diff: 6.408287810936320492029190063E-16
int: 3.1628668427508844 | dec: 3.162866842750883763725589184 | diff: 6.026275587411105338335037231E-16
int: 3.1207615795929895 | dec: 3.120761579592989026883483921 | diff: 4.717381074167236574888229370E-16
int: 3.161998692995051 | dec: 3.161998692995050882553587014 | diff: 3.252344505587545469999313354E-16
int: 3.121594652591011 | dec: 3.121594652591010478513182973 | diff: 5.091501839315986607670783997E-16
int: 3.1611986129870506 | dec: 3.161198612987050082473579013 | diff: 5.443255668896234224438667297E-16
int: 3.12236366153074 | dec: 3.122363661530739402861928527 | diff: 5.615717497969850774407386780E-16
int: 3.160458899625978 | dec: 3.160458899625977498100023765 | diff: 4.262302763189659944176673889E-16
int: 3.1230757220558845 | dec: 3.123075722055884040156098532 | diff: 4.220799098711242877244949341E-16
int: 3.1597729697623063 | dec: 3.159772969762306058504722385 | diff: 2.020724114135061109066009521E-16
int: 3.12373693372627 | dec: 3.123736933726270022468686349 | diff: 1.260571412589548522830009460E-16
int: 3.159135163814766 | dec: 3.159135163814765597689925287 | diff: 1.810770433643254418969154358E-16
int: 3.124352555119114 | dec: 3.124352555119113423776881808 | diff: 3.818825991233537552952766418E-16
int: 3.158540589307148 | dec: 3.158540589307147611811069842 | diff: 2.604223024124135525822639465E-16
int: 3.1249271439289967 | dec: 3.124927143928996351306868162 | diff: 3.798412495983127434849739075E-16
int: 3.157984995168666 | dec: 3.157984995168665772794471467 | diff: 2.697364868263550700545310974E-16
int: 3.125464669965414 | dec: 3.125464669965413740274146264 | diff: 9.282289916225695466995239258E-17
int: 3.157464669965414 | dec: 3.157464669965413740274146264 | diff: 1.212446085926609621047973633E-16
int: 3.125968606973288 | dec: 3.125968606973287756022177760 | diff: 1.771928555029680633544921875E-16
int: 3.1569763589112725 | dec: 3.156976358911272252146208768 | diff: 2.322736877321851320266723633E-16
int: 3.1264420077662343 | dec: 3.126442007766234084207277470 | diff: 1.814237782076588782668113708E-16
int: 3.156517195736159 | dec: 3.156517195736158896237352658 | diff: 4.229033502510950732231140137E-17
int: 3.126887566106529 | dec: 3.126887566106529266607723028 | diff: 8.505448115844887036085128784E-17
int: 3.1560846463985 | dec: 3.156084646398500069527431058 | diff: 1.207112790304247008562088013E-16
int: 3.127307667981234 | dec: 3.127307667981233882477071345 | diff: 5.820235947108973443508148193E-17
int: 3.155676462307475 | dec: 3.155676462307475017228844395 | diff: 9.914675470513806074857711792E-17
int: 3.127704434335447 | dec: 3.127704434335447045200872367 | diff: 1.581516147551743524074554443E-16
int: 3.155290641231999 | dec: 3.155290641231998769338803401 | diff: 2.132799304611821254491806030E-16
int: 3.1280797568782575 | dec: 3.128079756878257272740163946 | diff: 1.830697801304635801315307617E-16
int: 3.15492539446215 | dec: 3.154925394462149890189828375 | diff: 1.443237416876997277140617371E-16
int: 3.1284353282369843 | dec: 3.128435328236984327275921090 | diff: 9.038287031621049940586090088E-18
int: 3.1545791190866574 | dec: 3.154579119086657529890300175 | diff: 8.384311633574498742818832397E-17
int: 3.128772667473754 | dec: 3.128772667473754304083848562 | diff: 1.497402248940446014404296875E-16
int: 3.1542503744801236 | dec: 3.154250374480123730835440919 | diff: 1.553974122809817048311233521E-16
int: 3.129093141775721 | dec: 3.129093141775721215112170479 | diff: 1.498113844843771436214447021E-16
int: 3.1539378622726155 | dec: 3.153937862272615625050058678 | diff: 1.029199772333705319762229919E-16
int: 3.129397984972002 | dec: 3.129397984972002128117543340 | diff: 3.875265413041631489992141724E-17
int: 3.1536404092144266 | dec: 3.153640409214426370541785764 | diff: 1.948567400173474163413047791E-16
int: 3.129688313406043 | dec: 3.129688313406043137008851633 | diff: 6.052345409698040604591369629E-18
int: 3.1533569524592977 | dec: 3.153356952459297574878674118 | diff: 1.269286983868452615737915039E-16
int: 3.129965139593801 | dec: 3.129965139593800498855282305 | diff: 3.502718097735609632730484009E-16
int: 3.153086526877038 | dec: 3.153086526877037493074935484 | diff: 5.607649612633767697811126709E-16
int: 3.1302293840198954 | dec: 3.130229384019894635932078342 | diff: 7.307876873194007428288459778E-16
int: 3.1528282540763923 | dec: 3.152828254076391811073321280 | diff: 5.225424646212018889188766479E-16
int: 3.1304818853613083 | dec: 3.130481885361308012190639715 | diff: 3.240668401079616135358810425E-16
int: 3.1525813328751204 | dec: 3.152581332875120166886772312 | diff: 2.676356145465890159010887146E-16
int: 3.1307234093778527 | dec: 3.130723409377852407323930782 | diff: 2.433684446093724794983863831E-16
int: 3.1523450309994745 | dec: 3.152345030999474028945552404 | diff: 4.666132906417282811403274536E-16
int: 3.1309546566679236 | dec: 3.130954656667923226806514970 | diff: 3.811201807249140876531600952E-16
int: 3.152118677831945 | dec: 3.152118677831944390827678992 | diff: 6.019899464696118967533111572E-16
int: 3.131176269454982 | dec: 3.131176269454981040042338678 | diff: 8.019463446218599857091903687E-16
int: 3.151901658056018 | dec: 3.151901658056017309472390491 | diff: 8.755777162036890205144882202E-16
int: 3.1313888375431977 | dec: 3.131388837543196796651877671 | diff: 9.484538942298531421422958374E-16
int: 3.1516934060711166 | dec: 3.151693406071115578377765996 | diff: 1.009318912838618069231510162E-15
int: 3.1315929035585537 | dec: 3.131592903558552764307414237 | diff: 9.222863541244902198314666748E-16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment