Golden ratio/Convergence: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
|||
Line 917: | Line 917: | ||
14 iterations |
14 iterations |
||
The error is approximately -1.201864649e-06</pre> |
The error is approximately -1.201864649e-06</pre> |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Also works with gojq, the Go implementation of jq''' |
|||
Only minor adaptations are required for the following to work with |
|||
jaq, the Rust implementation of jq, so the output shown below |
|||
includes a run using jaq. |
|||
<syntaxhighlight lang="jq"> |
|||
def phi: (1 + (5|sqrt)) / 2; |
|||
# epsilon > 0 |
|||
def phi($epsilon): |
|||
{ phi: 1, oldPhi: (1+$epsilon), iters:0} |
|||
| until( (.phi - .oldPhi) | length < $epsilon; |
|||
.oldPhi = .phi |
|||
| .phi = 1 + (1 / .oldPhi) |
|||
| .iters += 1 ) |
|||
| "Final value of phi : \(.phi) vs \(phi)", |
|||
"Number of iterations : \(.iters)", |
|||
"Absolute error (approx) : \((.phi - phi) | length)"; |
|||
phi(1e-5) |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
jq (the C implementation): |
|||
<pre> |
|||
Final value of phi : 1.6180327868852458 vs 1.618033988749895 |
|||
Number of iterations : 14 |
|||
Absolute error (approx) : 1.2018646491362972e-06 |
|||
</pre> |
|||
gojq (the Go implementation): |
|||
<pre> |
|||
Final value of phi : 1.6180327868852458 vs 1.618033988749895 |
|||
Number of iterations : 14 |
|||
Absolute error (approx) : 0.0000012018646491362972 |
|||
</pre> |
|||
jaq (the Rust implementation): |
|||
<pre> |
|||
Final value of phi : 1.6180327868852458 vs 1.618033988749895 |
|||
Number of iterations : 14 |
|||
Absolute error (approx) : 1.2018646491362972e-6 |
|||
</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |