Skip to content

Instantly share code, notes, and snippets.

@fsmunoz
Created August 1, 2017 12:36
Show Gist options
  • Save fsmunoz/6e8c24403fed5a54df019d6c768f1096 to your computer and use it in GitHub Desktop.
Save fsmunoz/6e8c24403fed5a54df019d6c768f1096 to your computer and use it in GitHub Desktop.
Rational approximation in Ada
-- Approximates pi using a naïve algorithm as per Brian
-- Hayes' http://bit-player.org/2017/approximately-yours
--
-- Author: Frederico Munoz <[email protected]>
-- Date: Aug 2017
pragma License (GPL);
with Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Numerics;
-- Approximates pi
procedure Approximate is
package T_IO renames Ada.Text_IO;
package I_IO is new Ada.Text_IO.Integer_IO (Integer);
package F_IO is new Ada.Text_IO.Float_IO (Long_Float);
T, least_Error,err,merit: Long_Float;
dmax, d, n: Integer;
begin
-- These are hardcoded but can easily be changed
T := Ada.Numerics.Pi; --3.141592920353982520964564173482358455657958984375;
dmax := 1000000000;
-- Additional initialisations
d := 1;
least_Error := T;
while d <= dmax and least_Error > 0.0 loop
n := Integer(Long_Float(d) * T);
err := abs(T - Long_Float(n)/Long_Float(d)) / T;
merit := Long_Float(1) / ((Long_Float(n)**2 + Long_Float(d)**1)**(1/2) * err);
if err < least_Error then
T_IO.Put_Line (Integer'Image(n) & " / " & Integer'Image(d) &
" = " & Long_Float'Image(Long_Float(n)/Long_Float(d)) &
" error = " & Long_Float'Image(err) &
" merit = " & Long_Float'Image(merit));
least_Error := err;
end if;
d := d + 1;
end loop;
end Approximate;
@fsmunoz
Copy link
Author

fsmunoz commented Aug 1, 2017

 3 /  1 =  3.00000000000000E+00 error =  4.50703414486279E-02 merit =  2.21875399177932E+01
 13 /  4 =  3.25000000000000E+00 error =  3.45071300973197E-02 merit =  2.89795180642297E+01
 16 /  5 =  3.20000000000000E+00 error =  1.85916357881302E-02 merit =  5.37876285549035E+01
 19 /  6 =  3.16666666666667E+00 error =  7.98130624867045E-03 merit =  1.25292773995057E+02
 22 /  7 =  3.14285714285714E+00 error =  4.02499434770701E-04 merit =  2.48447553862948E+03
 179 /  57 =  3.14035087719298E+00 error =  3.95269703534521E-04 merit =  2.52991815729349E+03
 201 /  64 =  3.14062500000000E+00 error =  3.08013704032383E-04 merit =  3.24660879340247E+03
 223 /  71 =  3.14084507042254E+00 error =  2.37963112882841E-04 merit =  4.20233198282433E+03
 245 /  78 =  3.14102564102564E+00 error =  1.80485704760049E-04 merit =  5.54060500985090E+03
 267 /  85 =  3.14117647058824E+00 error =  1.32475163857517E-04 merit =  7.54858473755538E+03
 289 /  92 =  3.14130434782609E+00 error =  9.17705748314508E-05 merit =  1.08967389801866E+04
 311 /  99 =  3.14141414141414E+00 error =  5.68221903141069E-05 merit =  1.75987584158954E+04
 333 /  106 =  3.14150943396226E+00 error =  2.64896301670477E-05 merit =  3.77506214203010E+04
 355 /  113 =  3.14159292035398E+00 error =  8.49136787674061E-08 merit =  1.17766656034204E+07
 52163 /  16604 =  3.14159238737654E+00 error =  8.47383116051073E-08 merit =  1.18010375833324E+07
 52518 /  16717 =  3.14159239097924E+00 error =  8.35915344844665E-08 merit =  1.19629338804138E+07
 52873 /  16830 =  3.14159239453357E+00 error =  8.24601566200874E-08 merit =  1.21270688898546E+07
 53228 /  16943 =  3.14159239804049E+00 error =  8.13438699929189E-08 merit =  1.22934893568139E+07
 53583 /  17056 =  3.14159240150094E+00 error =  8.02423747826730E-08 merit =  1.24622433310129E+07
 53938 /  17169 =  3.14159240491584E+00 error =  7.91553788023930E-08 merit =  1.26333802595582E+07
 54293 /  17282 =  3.14159240828608E+00 error =  7.80825976398113E-08 merit =  1.28069509753366E+07
 54648 /  17395 =  3.14159241161253E+00 error =  7.70237543746338E-08 merit =  1.29830077502601E+07
 55003 /  17508 =  3.14159241489605E+00 error =  7.59785790131074E-08 merit =  1.31616044020445E+07
 55358 /  17621 =  3.14159241813745E+00 error =  7.49468086293785E-08 merit =  1.33427962883000E+07
 55713 /  17734 =  3.14159242133754E+00 error =  7.39281870827767E-08 merit =  1.35266403716935E+07
 56068 /  17847 =  3.14159242449711E+00 error =  7.29224644523830E-08 merit =  1.37131953439805E+07
 56423 /  17960 =  3.14159242761693E+00 error =  7.19293974611039E-08 merit =  1.39025215738913E+07
 56778 /  18073 =  3.14159243069773E+00 error =  7.09487484861651E-08 merit =  1.40946813204887E+07
 57133 /  18186 =  3.14159243374024E+00 error =  6.99802862659018E-08 merit =  1.42897386301098E+07
 57488 /  18299 =  3.14159243674518E+00 error =  6.90237849102526E-08 merit =  1.44877595643333E+07
 57843 /  18412 =  3.14159243971323E+00 error =  6.80790243248336E-08 merit =  1.46888121549536E+07
 58198 /  18525 =  3.14159244264507E+00 error =  6.71457895041482E-08 merit =  1.48929665938059E+07
 58553 /  18638 =  3.14159244554137E+00 error =  6.62238708143035E-08 merit =  1.51002952214025E+07
 58908 /  18751 =  3.14159244840275E+00 error =  6.53130637102939E-08 merit =  1.53108726369912E+07
 59263 /  18864 =  3.14159245122986E+00 error =  6.44131685946435E-08 merit =  1.55247757844839E+07
 59618 /  18977 =  3.14159245402329E+00 error =  6.35239903933318E-08 merit =  1.57420841135473E+07
 59973 /  19090 =  3.14159245678366E+00 error =  6.26453389798679E-08 merit =  1.59628795419459E+07
 60328 /  19203 =  3.14159245951153E+00 error =  6.17770283271422E-08 merit =  1.61872467335992E+07
 60683 /  19316 =  3.14159246220750E+00 error =  6.09188770728591E-08 merit =  1.64152730327579E+07
 61038 /  19429 =  3.14159246487210E+00 error =  6.00707079541043E-08 merit =  1.66470486874240E+07
 61393 /  19542 =  3.14159246750588E+00 error =  5.92323476659872E-08 merit =  1.68826669785069E+07
 61748 /  19655 =  3.14159247010939E+00 error =  5.84036271443565E-08 merit =  1.71222242332363E+07
 62103 /  19768 =  3.14159247268312E+00 error =  5.75843811417270E-08 merit =  1.73658200396179E+07
 62458 /  19881 =  3.14159247522760E+00 error =  5.67744480859208E-08 merit =  1.76135573962186E+07
 62813 /  19994 =  3.14159247774332E+00 error =  5.59736699387098E-08 merit =  1.78655428721215E+07
 63168 /  20107 =  3.14159248023077E+00 error =  5.51818923371733E-08 merit =  1.81218866850340E+07
 63523 /  20220 =  3.14159248269041E+00 error =  5.43989645936984E-08 merit =  1.83827028229107E+07
 63878 /  20333 =  3.14159248512271E+00 error =  5.36247389891901E-08 merit =  1.86481094146040E+07
 64233 /  20446 =  3.14159248752812E+00 error =  5.28590713385027E-08 merit =  1.89182286914602E+07
 64588 /  20559 =  3.14159248990710E+00 error =  5.21018204250083E-08 merit =  1.91931873366945E+07
 64943 /  20672 =  3.14159249226006E+00 error =  5.13528484246707E-08 merit =  1.94731165003806E+07
 65298 /  20785 =  3.14159249458744E+00 error =  5.06120200578976E-08 merit =  1.97581522898325E+07
 65653 /  20898 =  3.14159249688965E+00 error =  4.98792032963301E-08 merit =  2.00484356989233E+07
 66008 /  21011 =  3.14159249916710E+00 error =  4.91542689387692E-08 merit =  2.03441129649530E+07
 66363 /  21124 =  3.14159250142019E+00 error =  4.84370904698175E-08 merit =  2.06453358428522E+07
 66718 /  21237 =  3.14159250364929E+00 error =  4.77275440598795E-08 merit =  2.09522618374285E+07
 67073 /  21350 =  3.14159250585480E+00 error =  4.70255085651612E-08 merit =  2.12650544462341E+07
 67428 /  21463 =  3.14159250803709E+00 error =  4.63308652449546E-08 merit =  2.15838835452981E+07
 67783 /  21576 =  3.14159251019651E+00 error =  4.56434981857112E-08 merit =  2.19089254712964E+07
 68138 /  21689 =  3.14159251233344E+00 error =  4.49632934528946E-08 merit =  2.22403637101815E+07
 68493 /  21802 =  3.14159251444822E+00 error =  4.42901396564118E-08 merit =  2.25783889542383E+07
 68848 /  21915 =  3.14159251654118E+00 error =  4.36239278092557E-08 merit =  2.29231994966723E+07
 69203 /  22028 =  3.14159251861267E+00 error =  4.29645511861471E-08 merit =  2.32750016558401E+07
 69558 /  22141 =  3.14159252066302E+00 error =  4.23119048994604E-08 merit =  2.36340103896564E+07
 69913 /  22254 =  3.14159252269255E+00 error =  4.16658866060140E-08 merit =  2.40004493233480E+07
 70268 /  22367 =  3.14159252470157E+00 error =  4.10263958002797E-08 merit =  2.43745515659745E+07
 70623 /  22480 =  3.14159252669039E+00 error =  4.03933340970997E-08 merit =  2.47565600204268E+07
 70978 /  22593 =  3.14159252865932E+00 error =  3.97666049489694E-08 merit =  2.51467280468940E+07
 71333 /  22706 =  3.14159253060865E+00 error =  3.91461137873963E-08 merit =  2.55453199117294E+07
 71688 /  22819 =  3.14159253253867E+00 error =  3.85317680228998E-08 merit =  2.59526113467124E+07
 72043 /  22932 =  3.14159253444968E+00 error =  3.79234767622949E-08 merit =  2.63688903385103E+07
 72398 /  23045 =  3.14159253634194E+00 error =  3.73211509500506E-08 merit =  2.67944576880377E+07
 72753 /  23158 =  3.14159253821574E+00 error =  3.67247032269316E-08 merit =  2.72296278017752E+07
 73108 /  23271 =  3.14159254007133E+00 error =  3.61340479299984E-08 merit =  2.76747294390397E+07
 73463 /  23384 =  3.14159254190900E+00 error =  3.55491013753236E-08 merit =  2.81301062843223E+07
 73818 /  23497 =  3.14159254372899E+00 error =  3.49697807271272E-08 merit =  2.85961186832455E+07
 74173 /  23610 =  3.14159254553155E+00 error =  3.43960056940735E-08 merit =  2.90731432275667E+07
 74528 /  23723 =  3.14159254731695E+00 error =  3.38276966916164E-08 merit =  2.95615752120609E+07
 74883 /  23836 =  3.14159254908542E+00 error =  3.32647761142215E-08 merit =  3.00618286612329E+07
 75238 /  23949 =  3.14159255083720E+00 error =  3.27071676285767E-08 merit =  3.05743380581290E+07
 75593 /  24062 =  3.14159255257252E+00 error =  3.21547964563074E-08 merit =  3.10995593257392E+07
 75948 /  24175 =  3.14159255429162E+00 error =  3.16075890912609E-08 merit =  3.16379714097362E+07
 76303 /  24288 =  3.14159255599473E+00 error =  3.10654734408644E-08 merit =  3.21900775761096E+07
 76658 /  24401 =  3.14159255768206E+00 error =  3.05283789674831E-08 merit =  3.27564067867847E+07
 77013 /  24514 =  3.14159255935384E+00 error =  2.99962359816299E-08 merit =  3.33375161007672E+07
 77368 /  24627 =  3.14159256101027E+00 error =  2.94689764901136E-08 merit =  3.39339915770568E+07
 77723 /  24740 =  3.14159256265158E+00 error =  2.89465333478909E-08 merit =  3.45464511408535E+07
 78078 /  24853 =  3.14159256427795E+00 error =  2.84288412475724E-08 merit =  3.51755455416387E+07
 78433 /  24966 =  3.14159256588961E+00 error =  2.79158353058426E-08 merit =  3.58219623036215E+07
 78788 /  25079 =  3.14159256748674E+00 error =  2.74074524770398E-08 merit =  3.64864264870198E+07
 79143 /  25192 =  3.14159256906955E+00 error =  2.69036302809342E-08 merit =  3.71697049638937E+07
 79498 /  25305 =  3.14159257063821E+00 error =  2.64043077922339E-08 merit =  3.78726080558007E+07
 79853 /  25418 =  3.14159257219293E+00 error =  2.59094249337951E-08 merit =  3.85959936415125E+07
 80208 /  25531 =  3.14159257373389E+00 error =  2.54189227593375E-08 merit =  3.93407702390793E+07
 80563 /  25644 =  3.14159257526127E+00 error =  2.49327433120869E-08 merit =  4.01079009831709E+07
 80918 /  25757 =  3.14159257677525E+00 error =  2.44508299074912E-08 merit =  4.08984072844752E+07
 81273 /  25870 =  3.14159257827600E+00 error =  2.39731264264298E-08 merit =  4.17133744765775E+07
 81628 /  25983 =  3.14159257976369E+00 error =  2.34995780220042E-08 merit =  4.25539556099107E+07
 81983 /  26096 =  3.14159258123850E+00 error =  2.30301305541060E-08 merit =  4.34213778185341E+07
 82338 /  26209 =  3.14159258270060E+00 error =  2.25647312962063E-08 merit =  4.43169469590860E+07
 82693 /  26322 =  3.14159258415014E+00 error =  2.21033279458504E-08 merit =  4.52420559677638E+07
 83048 /  26435 =  3.14159258558729E+00 error =  2.16458691900894E-08 merit =  4.61981910367383E+07
 83403 /  26548 =  3.14159258701220E+00 error =  2.11923047054805E-08 merit =  4.71869394998550E+07
 83758 /  26661 =  3.14159258842504E+00 error =  2.07425850167285E-08 merit =  4.82099988595212E+07
 84113 /  26774 =  3.14159258982595E+00 error =  2.02966614966863E-08 merit =  4.92691864700638E+07
 84468 /  26887 =  3.14159259121509E+00 error =  1.98544860836387E-08 merit =  5.03664509767423E+07
 84823 /  27000 =  3.14159259259259E+00 error =  1.94160119880924E-08 merit =  5.15038824972547E+07
 85178 /  27113 =  3.14159259395862E+00 error =  1.89811927032701E-08 merit =  5.26837283427253E+07
 85533 /  27226 =  3.14159259531330E+00 error =  1.85499828532583E-08 merit =  5.39084056255260E+07
 85888 /  27339 =  3.14159259665679E+00 error =  1.81223374862175E-08 merit =  5.51805196631243E+07
 86243 /  27452 =  3.14159259798922E+00 error =  1.76982129225301E-08 merit =  5.65028799448437E+07
 86598 /  27565 =  3.14159259931072E+00 error =  1.72775654825785E-08 merit =  5.78785246687870E+07
 86953 /  27678 =  3.14159260062143E+00 error =  1.68603529003248E-08 merit =  5.93107395741838E+07
 87308 /  27791 =  3.14159260192149E+00 error =  1.64465331924474E-08 merit =  6.08030876962704E+07
 87663 /  27904 =  3.14159260321101E+00 error =  1.60360649410563E-08 merit =  6.23594381586566E+07
 88018 /  28017 =  3.14159260449013E+00 error =  1.56289078591256E-08 merit =  6.39839974113169E+07
 88373 /  28130 =  3.14159260575898E+00 error =  1.52250219423452E-08 merit =  6.56813503315032E+07
 88728 /  28243 =  3.14159260701767E+00 error =  1.48243677518372E-08 merit =  6.74565024789044E+07
 89083 /  28356 =  3.14159260826633E+00 error =  1.44269069795874E-08 merit =  6.93149267140140E+07
 89438 /  28469 =  3.14159260950508E+00 error =  1.40326013175816E-08 merit =  7.12626246102415E+07
 89793 /  28582 =  3.14159261073403E+00 error =  1.36414135886696E-08 merit =  7.33061858655609E+07
 90148 /  28695 =  3.14159261195330E+00 error =  1.32533067570589E-08 merit =  7.54528675998073E+07
 90503 /  28808 =  3.14159261316301E+00 error =  1.28682446351054E-08 merit =  7.77106768138318E+07
 90858 /  28921 =  3.14159261436327E+00 error =  1.24861916005965E-08 merit =  8.00884714881541E+07
 91213 /  29034 =  3.14159261555418E+00 error =  1.21071123140357E-08 merit =  8.25960785744676E+07
 91568 /  29147 =  3.14159261673586E+00 error =  1.17309724254326E-08 merit =  8.52444250769878E+07
 91923 /  29260 =  3.14159261790841E+00 error =  1.13577378675125E-08 merit =  8.80457016762452E+07
 92278 /  29373 =  3.14159261907194E+00 error =  1.09873748557168E-08 merit =  9.10135508373678E+07
 92633 /  29486 =  3.14159262022655E+00 error =  1.06198507363508E-08 merit =  9.41632820296699E+07
 92988 /  29599 =  3.14159262137234E+00 error =  1.02551327143618E-08 merit =  9.75121461470266E+07
 93343 /  29712 =  3.14159262250942E+00 error =  9.89318884284491E-09 merit =  1.01079643367288E+08
 93698 /  29825 =  3.14159262363789E+00 error =  9.53398759896936E-09 merit =  1.04887906515434E+08
 94053 /  29938 =  3.14159262475783E+00 error =  9.17749802533623E-09 merit =  1.08962159102547E+08
 94408 /  30051 =  3.14159262586936E+00 error =  8.82368930590461E-09 merit =  1.13331279619152E+08
 94763 /  30164 =  3.14159262697255E+00 error =  8.47253161413949E-09 merit =  1.18028476675276E+08
 95118 /  30277 =  3.14159262806751E+00 error =  8.12399498214786E-09 merit =  1.23092148899336E+08
 95473 /  30390 =  3.14159262915433E+00 error =  7.77805043154263E-09 merit =  1.28566921595759E+08
 95828 /  30503 =  3.14159263023309E+00 error =  7.43466898393671E-09 merit =  1.34504979597692E+08
 96183 /  30616 =  3.14159263130389E+00 error =  7.09382222637493E-09 merit =  1.40967727705663E+08
 96538 /  30729 =  3.14159263236682E+00 error =  6.75548231133410E-09 merit =  1.48027920718886E+08
 96893 /  30842 =  3.14159263342196E+00 error =  6.41962153264897E-09 merit =  1.55772422862343E+08
 97248 /  30955 =  3.14159263446939E+00 error =  6.08621303230224E-09 merit =  1.64305783365215E+08
 97603 /  31068 =  3.14159263550921E+00 error =  5.75522981091862E-09 merit =  1.73755007680638E+08
 97958 /  31181 =  3.14159263654148E+00 error =  5.42664543455475E-09 merit =  1.84275905264124E+08
 98313 /  31294 =  3.14159263756631E+00 error =  5.10043417605721E-09 merit =  1.96061740134647E+08
 98668 /  31407 =  3.14159263858376E+00 error =  4.77657016691459E-09 merit =  2.09355241324958E+08
 99023 /  31520 =  3.14159263959391E+00 error =  4.45502838676340E-09 merit =  2.24465460864662E+08
 99378 /  31633 =  3.14159264059685E+00 error =  4.13578381524014E-09 merit =  2.41792135342049E+08
 99733 /  31746 =  3.14159264159264E+00 error =  3.81881199741326E-09 merit =  2.61861542458065E+08
 100088 /  31859 =  3.14159264258137E+00 error =  3.50408861970919E-09 merit =  2.85380910281599E+08
 100443 /  31972 =  3.14159264356312E+00 error =  3.19158993398632E-09 merit =  3.13323459681110E+08
 100798 /  32085 =  3.14159264453795E+00 error =  2.88129233346101E-09 merit =  3.47066484156017E+08
 101153 /  32198 =  3.14159264550593E+00 error =  2.57317291813955E-09 merit =  3.88625262200808E+08
 101508 /  32311 =  3.14159264646715E+00 error =  2.26720850531227E-09 merit =  4.41071034118350E+08
 101863 /  32424 =  3.14159264742166E+00 error =  1.96337676041739E-09 merit =  5.09326594956442E+08
 102218 /  32537 =  3.14159264836955E+00 error =  1.66165549025115E-09 merit =  6.01809464035687E+08
 102573 /  32650 =  3.14159264931087E+00 error =  1.36202264296775E-09 merit =  7.34202184642887E+08
 102928 /  32763 =  3.14159265024570E+00 error =  1.06445659079536E-09 merit =  9.39446482503157E+08
 103283 /  32876 =  3.14159265117411E+00 error =  7.68936271394082E-10 merit =  1.30049789196054E+09
 103638 /  32989 =  3.14159265209615E+00 error =  4.75440339708062E-10 merit =  2.10331332131816E+09
 103993 /  33102 =  3.14159265301190E+00 error =  1.83948298829350E-10 merit =  5.43631012824807E+09
 104348 /  33215 =  3.14159265392142E+00 error =  1.05560489507986E-10 merit =  9.47324140557668E+09
 208341 /  66317 =  3.14159265346744E+00 error =  3.89472349753863E-11 merit =  2.56757636487411E+10
 312689 /  99532 =  3.14159265361894E+00 error =  9.27661782093526E-12 merit =  1.07797908602338E+11
 833719 /  265381 =  3.14159265358108E+00 error =  2.77415047216540E-12 merit =  3.60470713479156E+11
 1146408 /  364913 =  3.14159265359140E+00 error =  5.12705414651919E-13 merit =  1.95043775903734E+12
 3126535 /  995207 =  3.14159265358865E+00 error =  3.63714097573583E-13 merit =  2.74941226273939E+12
 4272943 /  1360120 =  3.14159265358939E+00 error =  1.28635767116969E-13 merit =  7.77388763959169E+12
 5419351 /  1725033 =  3.14159265358982E+00 error =  7.06789929214115E-15 merit =  1.41484755040569E+14
 42208400 /  13435351 =  3.14159265358977E+00 error =  6.64382533461268E-15 merit =  1.50515696851669E+14
 47627751 /  15160384 =  3.14159265358978E+00 error =  5.08888749034163E-15 merit =  1.96506604223012E+14
 53047102 /  16885417 =  3.14159265358978E+00 error =  3.81666561775622E-15 merit =  2.62008805630683E+14
 58466453 /  18610450 =  3.14159265358978E+00 error =  2.82715971685646E-15 merit =  3.53711887601422E+14
 63885804 /  20335483 =  3.14159265358979E+00 error =  1.97901180179952E-15 merit =  5.05302696573460E+14
 69305155 /  22060516 =  3.14159265358979E+00 error =  1.27222187258541E-15 merit =  7.86026416892049E+14
 74724506 /  23785549 =  3.14159265358979E+00 error =  7.06789929214115E-16 merit =  1.41484755040569E+15
 80143857 /  25510582 =  3.14159265358979E+00 error =  1.41357985842823E-16 merit =  7.07423775202844E+15
 245850922 /  78256779 =  3.14159265358979E+00 error =  0.00000000000000E+00 merit =  +Inf****************

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