Arithmetic-geometric mean: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
=={{header|11l}}==
{{trans|Python}}
<
V an = (a0 + g0) / 2.0
V gn = sqrt(a0 * g0)
Line 29:
R an
print(agm(1, 1 / sqrt(2)))</
{{out}}
<pre>0.847213</pre>
Line 35:
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
<
USING AGM,R13
SAVEAREA B STM-SAVEAREA(R15)
Line 125:
LTORG
YREGS
END AGM</
{{out}}
<pre>
Line 132:
=={{header|8th}}==
<
with: n
Line 146:
;with
bye
</syntaxhighlight>
{{out}}
<pre>
Line 155:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
PROC Agm(REAL POINTER a0,g0,result)
Line 191:
Print(",") PrintR(g)
Print(")=") PrintRE(res)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Arithmetic-geometric_mean.png Screenshot from Atari 8-bit computer]
Line 199:
=={{header|Ada}}==
<
procedure Arith_Geom_Mean is
Line 224:
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;</
Output:<pre>0.84721308479397909</pre>
Line 232:
Printing out the difference between the means at each iteration nicely demonstrates the quadratic convergence.
<
BEGIN
PROC agm = (LONG REAL x, y) LONG REAL :
Line 256:
printf (($l(-35,33)l$, agm (LONG 1.0, LONG 1.0 / long sqrt (LONG 2.0))))
END
</syntaxhighlight>
Output:<pre>+1.707106781186547524400844362e +0 +2.928932188134524755991556379e -1
+2.928932188134524755991556379e -1 +1.265697533955921916929670477e -2
Line 269:
=={{header|APL}}==
<
agd←{(⍺-⍵)<10*¯8:⍺⋄((⍺+⍵)÷2)∇(⍺×⍵)*÷2}
1 agd ÷2*÷2
</syntaxhighlight>
Output: <pre>0.8472130848</pre>
Line 278:
By functional composition:
<
property tolerance : 1.0E-5
Line 336:
end script
end if
end mReturn</
{{Out}}
<pre>0.847213084835</pre>
=={{header|AutoHotkey}}==
<
While abs(a-g) > tolerance
{
Line 351:
}
SetFormat, FloatFast, 0.15
MsgBox % agm(1, 1/sqrt(2))</
Output:
<pre>0.847213084793979</pre>
=={{header|AWK}}==
<
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
Line 372:
return (x<0 ? -x : x)
}
</syntaxhighlight>
Output
<pre>0.8472130847939792</pre>
Line 379:
==={{header|BASIC}}===
{{works with|QBasic}}
<
END
Line 390:
AGM = a
END FUNCTION</
{{out}}
<pre>
Line 397:
==={{header|BASIC256}}===
<
end
Line 410:
return a
end function</
{{out}}
<pre>
Line 417:
==={{header|Commodore BASIC}}===
<
20 G = 1/SQR(2)
30 GOSUB 100
Line 426:
120 G = SQR(TA*G)
130 IF A<TA THEN 100
140 RETURN</
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
@% = &1010
PRINT FNagm(1, 1/SQR(2))
Line 443:
UNTIL a = ta
= a
</syntaxhighlight>
Produces this output:
<pre>
Line 450:
==={{header|GW-BASIC}}===
<
20 G = 1!/SQR(2!)
30 FOR I=1 TO 20 'twenty iterations is plenty
Line 457:
60 A = B
70 NEXT I
80 PRINT A</
==={{header|IS-BASIC}}===
<
110 DEF AGM(A,G)
120 DO
Line 467:
150 LOOP UNTIL A=TA
160 LET AGM=A
170 END DEF</
==={{header|True BASIC}}===
{{works with|QBasic}}
<
DO
LET ta = (a + g) / 2
Line 485:
PRINT AGM(1, 1 / SQR(2))
END</
{{out}}
<pre>
Line 492:
=={{header|bc}}==
<
* numbers x and y.
* Result will have d digits after the decimal point.
Line 516:
scale = 20
m(1, 1 / sqrt(2), 20)</
{{Out}}
Line 522:
=={{header|BQN}}==
<
(|𝕨-𝕩) ≤ 1e¯15? 𝕨;
(0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩
}
1 AGM 1÷√2</
{{out}}
<pre>0.8472130847939792</pre>
Line 533:
=={{header|C}}==
===Basic===
<
#include<stdio.h>
#include<stdlib.h>
Line 565:
return 0;
}
</syntaxhighlight>
Original output:
Line 579:
===GMP===
<
Nigel_Galloway
Line 612:
return 0;
}</
The first couple of iterations produces:
Line 628:
=={{header|C sharp|C#}}==
<
{
using System;
Line 705:
}
}
}</
Output:
<pre>0.847213084835193</pre>
Note that the last 5 digits are spurious, as ''maximumRelativeDifference'' was only specified to be 1e-5. Using 1e-11 instead will give the result 0.847213084793979, which is as far as ''double'' can take it.
===Using Decimal Type===
<
class Program {
Line 727:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</
{{Out}}
<pre>0.8472130847939790866064991235</pre>
Line 734:
{{Libheader|System.Numerics}}
Even though the System.Numerics library directly supports only '''BigInteger''' (and not big rationals or big floating point numbers), it can be coerced into making this calculation. One just has to keep track of the decimal place and multiply by a very large constant.
<
using static System.Console;
using BI = System.Numerics.BigInteger;
Line 763:
WriteLine("0.{0}", CalcByAGM(digits));
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
}</
{{out}}
<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
=={{header|C++}}==
<
#include<bits/stdc++.h>
using namespace std;
Line 797:
return 0;
}
</syntaxhighlight>
Line 806:
=={{header|Clojure}}==
<
(:gen-class))
Line 831:
(println (agm one isqrt2))
</syntaxhighlight>
{{Output}}
<pre>
Line 838:
=={{header|COBOL}}==
<
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
Line 870:
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).</
{{out}}
<pre>0.8472130847939792</pre>
=={{header|Common Lisp}}==
<
(loop for a = a0 then (* (+ a g) 5d-1)
and g = g0 then (sqrt (* a g))
until (< (abs (- a g)) tolerance)
finally (return a)))
</syntaxhighlight>
{{out}}
Line 891:
=={{header|D}}==
<
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
Line 903:
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}</
{{out}}
<pre>0.8472130847939790866</pre>
Line 910:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<
program geometric_mean;
Line 954:
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
end.</
{{out}}
<pre>Enter two numbers:1
Line 962:
=={{header|EchoLisp}}==
We use the '''(~= a b)''' operator which tests for |a - b| < ε = (math-precision).
<
(lib 'math)
Line 977:
(agm 1 (/ 1 (sqrt 2)))
→ 0.8472130847939792
</syntaxhighlight>
=={{header|Elixir}}==
<
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
Line 988:
end
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)</
{{out}}
Line 996:
=={{header|Erlang}}==
<
%% Author: Abhay Jain
Line 1,014:
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).</
Output:
<
=={{header|ERRE}}==
Line 1,041:
PRINT(A)
END PROGRAM
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)</
Output
<pre>0.847213</pre>
=={{header|Factor}}==
<
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .</
{{out}}
<pre>
Line 1,066:
=={{header|Forth}}==
<
begin
fover fover f+ 2e f/
Line 1,074:
fdrop ;
1e 2e -0.5e f** agm f. \ 0.847213084793979</
=={{header|Fortran}}==
A '''Fortran 77''' implementation
<
implicit none
double precision agm,a,b,eps,c
Line 1,092:
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,120:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> 0.8472130847939792</pre>
Line 1,127:
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}
<
import "futlib/math"
Line 1,139:
fun main(x: f64, y: f64): f64 =
agm(x,y)
</syntaxhighlight>
=={{header|Go}}==
<
import (
Line 1,160:
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}</
{{out}}
<pre>
Line 1,169:
{{trans|Java}}
Solution:
<
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}</
Test:
<
assert (0.8472130847939792 - agm(1, 0.5**0.5)).abs() <= 10.0**-14</
Output:
Line 1,183:
=={{header|Haskell}}==
<
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
Line 1,202:
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal</
{{out}}
<pre>0.8472130847527654</pre>
Line 1,223:
}
return an
end</
Output:
Line 1,239:
First, the basic approach (with display precision set to 16 digits, which slightly exceeds the accuracy of 64 bit IEEE floating point arithmetic):
<
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791</
This is the limit -- it stops when values are within a small epsilon of previous calculations. We can ask J for unique values (which also means -- unless we specify otherwise -- values within a small epsilon of each other, for floating point values):
<
0.8472130847939792</
Another variation would be to show intermediate values, in the limit process:
<
1 0.7071067811865475
0.8535533905932737 0.8408964152537145
0.8472249029234942 0.8472012667468915
0.8472130848351929 0.8472130847527654
0.8472130847939792 0.8472130847939791</
=== Arbitrary Precision ===
Line 1,263:
Borrowing routines from that page, but going with a default of approximately 100 digits of precision:
<
round=: DP&$: : (4 : 0)
Line 1,292:
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
(2x^m) * 1++/*/\d%1+i.n
)</
We are also going to want a routine to display numbers with this precision, and we are going to need to manage epsilon manually, and we are going to need an arbitrary root routine:
<
x{.deb (x*2j1)":y
)
Line 1,302:
root=: ln@] exp@% [
epsilon=: 1r9^DP</
Some example uses:
<
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
fmt *~sqrt 2
Line 1,313:
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
fmt 2 root 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572</
Note that 2 root 2 is considerably slower than sqrt 2. The price of generality. So, while we could define geometric mean generally, a desire for good performance pushes us to use a routine specialized for two numbers:
<
geomean2=: [: sqrt */</
A quick test to make sure these can be equivalent:
<
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt geomean2 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517</
Now for our task example:
<
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
Line 1,337:
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229</
We could of course extract out only a representative final value, but it's obvious enough, and showing how rapidly this converges is fun.
Line 1,343:
=={{header|Java}}==
<
* Arithmetic-Geometric Mean of 1 & 1/sqrt(2)
* Brendan Shaklovitz
Line 1,365:
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}</
{{out}}
<pre>0.8472130847939792</pre>
Line 1,372:
===ES5===
<
var an = (a0 + g0) / 2,
gn = Math.sqrt(a0 * g0);
Line 1,381:
}
agm(1, 1 / Math.sqrt(2));</
===ES6===
<
'use strict';
Line 1,426:
return agm(1, 1 / Math.sqrt(2));
})();</
{{Out}}
<syntaxhighlight lang
=={{header|jq}}==
{{works with|jq|1.4}}
Naive version that assumes tolerance is appropriately specified:
<
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,442:
else .
end;
[a, g] | _agm | .[0] ;</
This version avoids an infinite loop if the requested tolerance is too small:
<
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,459:
# Example:
agm(1; 1/(2|sqrt); 1e-100)</
{{Out}}
$ jq -n -f Arithmetic-geometric_mean.jq
Line 1,466:
=={{header|Julia}}==
{{works with|Julia|1.2}}
<
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
Line 1,485:
println("# Using ", precision(BigFloat), "-bit float numbers:")
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)</
The ε for this calculation is given as a positive integer multiple of the machine ε for <tt>x</tt>.
Line 1,498:
=={{header|Klingphix}}==
{{trans|Oforth}}
<
:agm [ over over + 2 / rot rot * sqrt ] [ over over tostr swap tostr # ] while drop ;
Line 1,506:
pstack
" " input</
{{trans|F#}}
<
:agm %a %g %p !p !g !a
Line 1,517:
pstack
" " input</
{{out}}
<pre>(0.847213)</pre>
=={{header|Kotlin}}==
<
fun agm(a: Double, g: Double): Double {
Line 1,540:
fun main(args: Array<String>) {
println(agm(1.0, 1.0 / Math.sqrt(2.0)))
}</
{{out}}
Line 1,549:
=={{header|LFE}}==
<
(defun agm (a g)
(agm a g 1.0e-15))
Line 1,565:
(defun next-g (a g)
(math:sqrt (* a g)))
</syntaxhighlight>
Usage:
Line 1,575:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang=lb>
print agm(1, 1/sqr(2))
print using("#.#################",agm(1, 1/sqr(2)))
Line 1,590:
end function
</syntaxhighlight>
=={{header|LiveCode}}==
<
put abs(aa-g) into absdiff
put (aa+g)/2 into aan
Line 1,605:
end repeat
return aa
end agm</
Example
<
-- ouput
-- 0.847213</
=={{header|LLVM}}==
<
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 1,714:
attributes #2 = { nounwind readnone speculatable }
attributes #4 = { nounwind }
attributes #6 = { noreturn }</
{{out}}
<pre>The arithmetic-geometric mean is 0.8472130847939791654</pre>
=={{header|Logo}}==
<
output and [:a - :b < 1e-15] [:a - :b > -1e-15]
end
Line 1,728:
show agm 1 1/sqrt 2
</syntaxhighlight>
=={{header|Lua}}==
<
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
Line 1,742:
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))</
'''Output:'''
Line 1,749:
=={{header|M2000 Interpreter}}==
<
Module Checkit {
Function Agm {
Line 1,764:
}
Checkit
</syntaxhighlight>
=={{header|Maple}}==
Maple provides this function under the name GaussAGM. To compute a floating point approximation, use evalf.
<
> evalf( GaussAGM( 1, 1 / sqrt( 2 ) ) ); # default precision is 10 digits
0.8472130847
Line 1,775:
0.847213084793979086606499123482191636481445910326942185060579372659\
7340048341347597232002939946112300
</syntaxhighlight>
Alternatively, if one or both arguments is already a float, Maple will compute a floating point approximation automatically.
<
> GaussAGM( 1.0, 1 / sqrt( 2 ) );
0.8472130847
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
To any arbitrary precision, just increase PrecisionDigits
<
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛</
<pre>AGMean[1, 1/Sqrt[2]]
Line 1,791:
=={{header|MATLAB}} / {{header|Octave}}==
<
%%arithmetic_geometric_mean(a,g)
while (1)
Line 1,799:
if (abs(a0-a) < a*eps) break; end;
end;
end</
<pre>octave:26> agm(1,1/sqrt(2))
ans = 0.84721
Line 1,805:
=={{header|Maxima}}==
<
agm(1, 1/sqrt(2)), bfloat, fpprec: 85;
/* 8.472130847939790866064991234821916364814459103269421850605793726597340048341347597232b-1 */</
=={{header|МК-61/52}}==
Line 1,814:
- ИП2 - /-/ x<0 31 ИП1 П3 ИП0 ИП1
* КвКор П1 ИП0 ИП3 + 2 / П0 БП
08 ИП0 С/П</
=={{header|Modula-2}}==
{{trans|C}}
<
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongConv IMPORT ValueReal;
Line 1,887:
WriteReal(AGM(x, y));
WriteLn
END AGM.</
{{out}}
<pre>Enter two numbers: 1.0
Line 1,898:
=={{header|NetRexx}}==
{{trans|Java}}
<
options replace format comments java crossref symbols nobinary
Line 1,922:
end
return a1 + 0
</syntaxhighlight>
'''Output:'''
<pre>
Line 1,929:
=={{header|NewLISP}}==
<
(define (a-next a g) (mul 0.5 (add a g)))
Line 1,951:
(amg 1.0 root-reciprocal-2 quadrillionth)
)
</syntaxhighlight>
=={{header|Nim}}==
<
proc agm(a, g: float,delta: float = 1.0e-15): float =
Line 1,967:
result = aOld
echo agm(1.0,1.0/sqrt(2.0))</
Output:<br/>
Line 1,976:
See first 24 iterations:
<
from strutils import parseFloat, formatFloat, ffDecimal
Line 1,995:
echo("Result A: " & formatFloat(t.resA, ffDecimal, 24))
echo("Result G: " & formatFloat(t.resG, ffDecimal, 24))</
=={{header|Oberon-2}}==
{{works with|oo2c}}
<
MODULE Agm;
IMPORT
Line 2,025:
Out.LongReal(Of(1,1 / Math.sqrt(2)),0,0);Out.Ln
END Agm.
</syntaxhighlight>
{{Out}}
<pre>
Line 2,033:
=={{header|Objeck}}==
{{trans|Java}}
<
class ArithmeticMean {
function : Amg(a : Float, g : Float) ~ Nil {
Line 2,050:
}
}
</syntaxhighlight>
Output:
Line 2,056:
=={{header|OCaml}}==
<
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)</
Output
<pre>0.8472130847939792</pre>
Line 2,066:
=={{header|Oforth}}==
<
while( 2dup <> ) [ 2dup + 2 / -rot * sqrt ] drop ;</
Usage :
<syntaxhighlight lang
{{out}}
Line 2,078:
=={{header|OOC}}==
<
import math // import for sqrt() function
Line 2,098:
"%.16f" printfln(agm(1., sqrt(0.5)))
}
</syntaxhighlight>
Output
<pre>0.8472130847939792</pre>
=={{header|ooRexx}}==
<
say agm(1, 1/rxcalcsqrt(2,16))
Line 2,120:
return a1+0
::requires rxmath LIBRARY</
{{out}}
<pre>0.8472130847939791968</pre>
Line 2,126:
=={{header|PARI/GP}}==
Built-in:
<
Iteration:
<
=={{header|Pascal}}==
Line 2,135:
{{libheader|GMP}}
Port of the C example:
<
uses
Line 2,169:
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.</
Output is as long as the C example.
=={{header|Perl}}==
<
my ($a0, $g0, $a1, $g1);
Line 2,189:
}
print agm(1, 1/sqrt(2))."\n";</
Output:
<pre>0.847213084793979</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">tolerance</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1.0e-15</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">-</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)></span><span style="color: #000000;">tolerance</span> <span style="color: #008080;">do</span>
Line 2,203:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (rounds to 10 d.p.)</span>
<!--</
{{out}}
<pre>
Line 2,214:
=={{header|Phixmonti}}==
<
1.0e-15 var tolerance
Line 2,228:
enddef
1 1 2 sqrt / agm tostr ?</
=={{header|PHP}}==
<
define('PRECISION', 13);
Line 2,254:
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
</syntaxhighlight>
{{out}}
<pre>
Line 2,261:
=={{header|Picat}}==
<
println(agm(1.0, 1/sqrt(2))).
agm(A,G) = A, A-G < 1.0e-10 => true.
agm(A,G) = agm((A+G)/2, sqrt(A*G)).
</syntaxhighlight>
{{out}}
Line 2,274:
=={{header|PicoLisp}}==
<
(de agm (A G)
Line 2,283:
(round
(agm 1.0 (*/ 1.0 1.0 (sqrt 2.0 1.0)))
70 )</
Output:
<pre>-> "0.8472130847939790866064991234821916364814459103269421850605793726597340"</pre>
=={{header|PL/I}}==
<
arithmetic_geometric_mean: /* 31 August 2012 */
procedure options (main);
Line 2,302:
put skip list ('The result is:', a);
end arithmetic_geometric_mean;
</syntaxhighlight>
Results:
<pre>
Line 2,315:
=={{header|Potion}}==
Input values should be floating point
<
xi = 1
7 times :
Line 2,331:
.
x
.</
=={{header|PowerShell}}==
<
function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
Line 2,349:
}
agm 1 (1/[Math]::Sqrt(2))
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,358:
=={{header|Prolog}}==
<
agm(A,G,A) :- abs(A-G) < 1.0e-15, !.
agm(A,G,Res) :- A1 is (A+G)/2.0, G1 is sqrt(A*G),!, agm(A1,G1,Res).
Line 2,364:
?- agm(1,1/sqrt(2),Res).
Res = 0.8472130847939792.
</syntaxhighlight>
=={{header|PureBasic}}==
<
Protected.d ta=a+1, tg
While ta <> a
Line 2,381:
Input()
CloseConsole()
EndIf</
0.8472130847939792
Line 2,389:
===Basic Version===
<
def agm(a0, g0, tolerance=1e-10):
Line 2,404:
return an
print agm(1, 1 / sqrt(2))</
{{out}}
<pre> 0.847213084835</pre>
===Multi-Precision Version===
<
def agm(a, g, tolerance=Decimal("1e-65")):
Line 2,417:
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())</
{{out}}
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
Line 2,424:
=={{header|Quackery}}==
<
[ temp put
Line 2,441:
125 point$ echo$ cr cr
swap say "Num: " echo cr
say "Den: " echo</
{{out}}
Line 2,454:
=={{header|R}}==
<
geometricMean <- function(a, b) { sqrt(a * b) }
Line 2,467:
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))</
{{out}}
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16</pre>
This function also works on vectors a and b (following the spirit of R):
<
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))</
{{out}}
<pre> agm rel_error
Line 2,484:
=={{header|Racket}}==
This version uses Racket's normal numbers:
<
#lang racket
(define (agm a g [ε 1e-15])
Line 2,492:
(agm 1 (/ 1 (sqrt 2)))
</syntaxhighlight>
Output:
<pre>
Line 2,499:
This alternative version uses arbitrary precision floats:
<
#lang racket
(require math/bigfloat)
(bf-precision 200)
(bfagm 1.bf (bf/ (bfsqrt 2.bf)))
</syntaxhighlight>
Output:
<pre>
Line 2,512:
=={{header|Raku}}==
(formerly Perl 6)
<
($a, $g) = ($a + $g)/2, sqrt $a * $g until $a ≅ $g;
return $a;
}
say agm 1, 1/sqrt 2;</
{{out}}
<pre>0.84721308479397917</pre>
It's also possible to write it recursively:
<
$a ≅ $g ?? $a !! agm(|@$_)
given ($a + $g)/2, sqrt $a * $g;
}
say agm 1, 1/sqrt 2;</
=={{header|Raven}}==
<
# $errlim $g $a "%d %g %d\n" print
$a 1.0 + as $t
Line 2,541:
16 1 2 sqrt / 1 agm "agm: %.15g\n" print</
{{out}}
<pre>t: 0.853553 a: 0.853553 g: 0.840896
Line 2,550:
=={{header|Relation}}==
<
function agm(x,y)
set a = x
Line 2,569:
echo sqrt(x+y)
echo agm(x,y)
</syntaxhighlight>
<pre>
Line 2,581:
REXX supports arbitrary precision, so the default digits can be changed if desired.
<
parse arg a b digs . /*obtain optional numbers from the C.L.*/
if digs=='' | digs=="," then digs= 120 /*No DIGS specified? Then use default.*/
Line 2,612:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</
{{out|output|text= when using the default input:}}
<pre>
Line 2,621:
=={{header|Ring}}==
<
decimals(9)
see agm(1, 1/sqrt(2)) + nl
Line 2,635:
end
return gn
</syntaxhighlight>
=={{header|Ruby}}==
===Flt Version===
The thing to note about this implementation is that it uses the [http://flt.rubyforge.org/ Flt] library for high-precision math. This lets you adapt context (including precision and epsilon) to a ridiculous-in-real-life degree.
<
# It lets us control 'context' of numbers, individually or collectively -- including precision
# (which adjusts the context's value of epsilon accordingly).
Line 2,660:
end
puts agm(1, 1 / BinNum(2).sqrt)</
{{out}}
<pre>0.84721308479397908660649912348219163648144591032694218506057937265973400483413475972320029399461122994212228562523341096309796266583087105969971363598338426</pre>
Line 2,667:
===BigDecimal Version===
Ruby has a BigDecimal class in standard library
<
PRECISION = 100
Line 2,682:
a = BigDecimal(1)
g = 1 / BigDecimal(2).sqrt(PRECISION)
puts agm(a, g)</
{{out}}
<pre>
Line 2,690:
=={{header|Run BASIC}}==
<
print agm(1,1/2^.5)
print using("#.############################",agm(1, 1/sqr(2)))
Line 2,702:
g = gn
wend
end function</
<pre>0.847213085
0.847213085
Line 2,709:
=={{header|Rust}}==
<
// cargo run --name agm arg1 arg2
Line 2,739:
}
}
}</
{{out}}
Line 2,748:
=={{header|Scala}}==
<
def agm(a: Double, g: Double, eps: Double): Double = {
if (math.abs(a - g) < eps) (a + g) / 2
Line 2,755:
agm(1, math.sqrt(2)/2, 1e-15)
</syntaxhighlight>
=={{header|Scheme}}==
<
(define agm
(case-lambda
Line 2,770:
(display (agm 1 (/ 1 (sqrt 2)))) (newline)
</syntaxhighlight>
{{out}}
Line 2,778:
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 2,807:
writeln(agm(1.0, 2.0) digits 6);
writeln(agm(1.0, 1.0 / sqrt(2.0)) digits 6);
end func;</
{{out}}
Line 2,816:
=={{header|SequenceL}}==
<
agm(a, g) :=
Line 2,828:
agm(arithmeticMean, geometricMean);
main := agm(1.0, 1.0 / sqrt(2));</
{{out}}
Line 2,836:
=={{header|Sidef}}==
<
loop {
var (a1, g1) = ((a+g)/2, sqrt(a*g))
Line 2,844:
}
say agm(1, 1/sqrt(2))</
{{out}}
<pre>0.8472130847939790866064991234821916364814</pre>
Line 2,855:
Better precision than this is not easily obtainable on the ZX81, unfortunately.
<
20 LET G=1/SQR 2
30 GOSUB 100
Line 2,865:
130 IF ABS(A-G)>.00000001 THEN GOTO 100
140 LET AGM=A
150 RETURN</
{{out}}
<pre>0.84721309</pre>
Line 2,872:
{{works with|Smalltalk/X}}
That is simply a copy/paste of the already existing agm method in the Number class:
<
"return the arithmetic-geometric mean agm(x, y)
of the receiver (x) and the argument, y.
Line 2,890:
gi := gn.
] doUntil:[ delta < epsilon ].
^ ai</
<
Transcript showCR: ( (1/2) agm:(1/6) ).
Transcript showCR: (1 agm:(1 / 2 sqrt)).</
{{out}}
<pre>13.4581714817256
Line 2,903:
{{works with|oracle|11.2 and higher}}
The solution uses recursive WITH clause (aka recursive CTE, recursive query, recursive factored subquery). Some, perhaps many, but not all SQL dialects support recursive WITH clause. The solution below was written and tested in Oracle SQL - Oracle has supported recursive WITH clause since version 11.2.
<
rec (rn, a, g, diff) as (
select 1, 1, 1/sqrt(2), 1 - 1/sqrt(2)
Line 2,915:
from rec
where diff <= 1e-38
;</
Line 2,925:
=={{header|Standard ML}}==
<
fun agm(a, g) = let
fun agm'(a, g, eps) =
Line 2,934:
in agm'(a, g, 1e~15)
end;
</syntaxhighlight>
{{out}}
<pre>
Line 2,941:
=={{header|Stata}}==
<
real scalar agm(real scalar a, real scalar b) {
Line 2,954:
agm(1,1/sqrt(2))
end</
{{out}}
<pre>.8472130848</pre>
=={{header|Swift}}==
<
enum AGRError : Error {
Line 2,989:
} catch {
print("agr is undefined when a * g < 0")
}</
{{out}}
<pre>0.847213084835193</pre>
Line 2,995:
=={{header|Tcl}}==
The tricky thing about this implementation is that despite the finite precision available to IEEE doubles (which Tcl uses in its implementation of floating point arithmetic, in common with many other languages) the sequence of values does not ''quite'' converge to a single value; it gets to within a ULP and then errors prevent it from getting closer. This means that an additional termination condition is required: once a value does not change (hence the <code>old_b</code> variable) we have got as close as we can. Note also that we are using exact equality with floating point; this is reasonable because this is a rapidly converging sequence (it only takes 4 iterations in this case).
<
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
Line 3,004:
}
puts [agm 1 [expr 1/sqrt(2)]]</
Output:
<pre>0.8472130847939792</pre>
=={{header|TI-83 BASIC}}==
<
While abs(A-G)>e-15
(A+G)/2→B
sqrt(AG)→G:B→A
End
A</
{{out}}
<pre>.8472130848</pre>
Line 3,021:
{{works with|ksh93}}
ksh is one of the few unix shells that can do floating point arithmetic (bash does not).
<
float a=$1 g=$2 eps=${3:-1e-11} tmp
while (( abs(a-g) > eps )); do
Line 3,032:
}
agm $((1/sqrt(2))) 1</
{{output}}
Line 3,043:
0.8472130848</pre>
You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <
=={{header|VBA}}==
<
Do While Abs(a - g) > tolerance
tmp = a
Line 3,057:
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub</
<pre> 0,853553390593274
0,847224902923494
Line 3,066:
=={{header|VBScript}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang=vb>
Function agm(a,g)
Do Until a = tmp_a
Line 3,077:
WScript.Echo agm(1,1/Sqr(2))
</syntaxhighlight>
{{Out}}
Line 3,085:
{{trans|C#}}
===Double, Decimal Versions===
<
Imports System.Console
Line 3,114:
End Sub
End Module</
{{out}}
<pre>Double result: 0.847213084793979
Line 3,122:
{{trans|C#}}
{{Libheader|System.Numerics}}
<
Imports System.Console
Imports BI = System.Numerics.BigInteger
Line 3,157:
End Sub
End Module</
{{out}}
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
=={{header|Vlang}}==
<
const ep = 1e-14
Line 3,177:
fn main() {
println(agm(1.0, 1.0/math.sqrt2))
}</
Using standard math module
<
import math
fn main() {
println(stats.geometric_mean<f64>([1.0, 1.0/math.sqrt2]))
}</
{{out}}
<pre>0.8408964152537145
Line 3,191:
=={{header|Wren}}==
{{trans|Go}}
<
var agm = Fn.new { |a, g|
Line 3,202:
}
System.print(agm.call(1, 1/2.sqrt))</
{{out}}
Line 3,210:
=={{header|XPL0}}==
<
real A, A1, G;
[Format(0, 16);
Line 3,219:
RlOut(0, A); RlOut(0, G); RlOut(0, A-G); CrLf(0);
until A=G;
]</
Output:
Line 3,231:
=={{header|zkl}}==
{{trans|XPL0}}
<
while(not a.closeTo(g,1.0e-15)){
a1:=(a+g)/2.0; g=(a*g).sqrt(); a=a1;
println(a," ",g," ",a-g);
}</
{{out}}
<pre>
Line 3,244:
</pre>
Or, using tail recursion
<
if(a.closeTo(g,1.0e-15)) return(a) else return(self.fcn((a+g)/2.0, (a*g).sqrt()));
}()</
{{out}}
<pre>
Line 3,258:
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
<
20 LET ta=a
30 LET a=(a+g)/2
Line 3,264:
50 IF a<ta THEN GO TO 20
60 PRINT a
</syntaxhighlight>
{{out}}
<pre>0.84721309</pre>
|