Golden ratio/Convergence: Difference between revisions

Content added Content deleted
(Added Lua version)
Line 1,167: Line 1,167:
Error (approx) : 2.722811719173566624681571006109388407808876983723400587864054809516456794284321e-26
Error (approx) : 2.722811719173566624681571006109388407808876983723400587864054809516456794284321e-26
</pre>
</pre>

=={{header|Lua}}==
<p>When representated as a 64-bit floating point value, it takes 38 iterations before the iterated value is indistinguishable from the calculated one.</p>
<p>The stipulation to stop when the error is less than 1e-5 is presumably to prevent calculations with arbitrary-precision values from continuing infinitely.</p>
<p>Since such data types are unavailable in the core Lua language, we may as well continue to the natural limitation.</p>
<syntaxhighlight lang="lua">local phi, iters = 1, 0
repeat
phi = 1 + 1 / phi
iters = iters + 1
until phi == (1 + math.sqrt(5))/2
print("Value of phi: " .. phi)
print("Iterations required: " .. iters)</syntaxhighlight>
{{out}}
<pre>Value of phi: 1.6180339887499
Iterations required: 38</pre>


=={{header|M4}}==
=={{header|M4}}==