31 lines
619 B
Text
31 lines
619 B
Text
{def eps 1e-15}
|
|
-> eps
|
|
|
|
{def agm
|
|
{lambda {:a :g}
|
|
{if {> {abs {- :a :g}} {eps}}
|
|
then {agm {/ {+ :a :g} 2}
|
|
{sqrt {* :a :g}}}
|
|
else :a }}}
|
|
-> agm
|
|
|
|
{agm 1 {/ 1 {sqrt 2}}}
|
|
-> 0.8472130847939792
|
|
|
|
Multi-precision version using the lib_BN library
|
|
|
|
{BN.DEC 70}
|
|
-> 70 digits
|
|
{def EPS {BN./ 1 {BN.pow 10 45}}}
|
|
-> EPS
|
|
|
|
{def AGM
|
|
{lambda {:a :g}
|
|
{if {= {BN.compare {BN.abs {BN.- :a :g}} {EPS}} 1}
|
|
then {AGM {BN./ {BN.+ :a :g} 2}
|
|
{BN.sqrt {BN.* :a :g}}}
|
|
else :a }}}
|
|
-> AGM
|
|
|
|
{AGM 1 {BN./ 1 {BN.sqrt 2}}}
|
|
-> 0.8472130847939790866064991234821916364814459103269421850605793726597339
|