Golden ratio/Convergence: Difference between revisions

Content added Content deleted
(→‎{{header|ooRexx}}: added ooRexx code)
(added comments to ooRexx code (cf. REXX))
Line 1,177: Line 1,177:
=={{header|ooRexx}}==
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">/* REXX
<syntaxhighlight lang="oorexx">/* REXX
* Compute the Golden Ratio using recursion
* Compute the Golden Ratio using iteration
* 20230604 Walter Pachl
* 20230604 Walter Pachl
*/
*/
Line 1,183: Line 1,183:
Parse Value '1 1 1e-5' With oldPhi phi limit
Parse Value '1 1 1e-5' With oldPhi phi limit
Do iterations=1 By 1 Until delta<=limit
Do iterations=1 By 1 Until delta<=limit
phi=1+1/oldPhi
phi=1+1/oldPhi /* next approximation */
delta=abs(phi-oldPhi)
delta=abs(phi-oldPhi) /* difference to previous */
If delta>limit Then
If delta>limit Then /* not small enough */
oldPhi=phi
oldPhi=phi /* proceed with new value */
End
End
actualPhi=(1+rxCalcsqrt(5,16))/2
actualPhi=(1+rxCalcsqrt(5,16))/2 /* compute the real value */
Say 'Final value of phi : ' phi
Say 'Final value of phi : ' phi /* our approximation */
Say 'Actual value : ' actualPhi
Say 'Actual value : ' actualPhi /* the real value */
Say 'Error (approx) :' (phi-actualPhi)
Say 'Error (approx) :' (phi-actualPhi) /* the difference */
Say 'Number of iterations:' iterations
Say 'Number of iterations:' iterations
Exit
::REQUIRES RXMATH library
::REQUIRES RXMATH library
</syntaxhighlight>
</syntaxhighlight>