Golden ratio/Convergence: Difference between revisions
Content added Content deleted
(Add C# implementation) |
No edit summary |
||
Line 1,477: | Line 1,477: | ||
<pre>Result: 987/610 (1.618032786) after 14 iterations |
<pre>Result: 987/610 (1.618032786) after 14 iterations |
||
The error is approximately -0.000001202</pre> |
The error is approximately -0.000001202</pre> |
||
=={{header|Onyx (wasm)}}== |
|||
<syntaxhighlight lang="TS"> |
|||
package main |
|||
use core {*} |
|||
use core.math{abs} |
|||
use core.intrinsics.wasm{sqrt_f64} |
|||
main :: () { |
|||
iterate(); |
|||
} |
|||
iterate :: () { |
|||
count := 0; |
|||
phi0: f64 = 1.0; |
|||
difference: f64 = 1.0; |
|||
phi1: f64; |
|||
println("\nGolden ratio/Convergence"); |
|||
println("-----------------------------------------"); |
|||
while 0.00001 < difference { |
|||
phi1 = 1.0 + (1.0 / phi0); |
|||
difference = abs(phi1 - phi0); |
|||
phi0 = phi1; |
|||
count += 1; |
|||
printf("Iteration {} : Estimate : {.8}\n", count, phi1); |
|||
} |
|||
println("-----------------------------------------"); |
|||
printf("Result: {} after {} iterations", phi1, count); |
|||
printf("\nThe error is approximately {.8}\n", (phi1 - (0.5 * (1.0 + sqrt_f64(5.0))))); |
|||
println("\n"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Golden ratio/Convergence |
|||
----------------------------------------- |
|||
Iteration 1 : Estimate : 2.00000000 |
|||
Iteration 2 : Estimate : 1.50000000 |
|||
Iteration 3 : Estimate : 1.66666666 |
|||
Iteration 4 : Estimate : 1.60000000 |
|||
Iteration 5 : Estimate : 1.62500000 |
|||
Iteration 6 : Estimate : 1.61538461 |
|||
Iteration 7 : Estimate : 1.61904761 |
|||
Iteration 8 : Estimate : 1.61764705 |
|||
Iteration 9 : Estimate : 1.61818181 |
|||
Iteration 10 : Estimate : 1.61797752 |
|||
Iteration 11 : Estimate : 1.61805555 |
|||
Iteration 12 : Estimate : 1.61802575 |
|||
Iteration 13 : Estimate : 1.61803713 |
|||
Iteration 14 : Estimate : 1.61803278 |
|||
----------------------------------------- |
|||
Result: 1.6180 after 14 iterations |
|||
The error is approximately -0.00000120 |
|||
</pre> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |