Golden ratio/Convergence: Difference between revisions
Content added Content deleted
(Restored FreeTom|'s Lua sample an tweaked it to itterate to the task's specified limit) |
Space Ghost (talk | contribs) (Golden ratio/Convergence in Odin) |
||
Line 1,329: | Line 1,329: | ||
Number of iterations: 14 |
Number of iterations: 14 |
||
Approximative error: -0.0000012018646491</pre> |
Approximative error: -0.0000012018646491</pre> |
||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="Go"> |
|||
package golden |
|||
import "core:fmt" |
|||
import "core:math" |
|||
/* main block */ |
|||
main :: proc() { |
|||
iterate() |
|||
} |
|||
/* definitions */ |
|||
iterate :: proc() { |
|||
count: u32 = 0 |
|||
phi0: f64 = 1.0 |
|||
difference: f64 = 1.0 |
|||
phi1: f64 |
|||
fmt.println("\nGolden ratio/Convergence") |
|||
fmt.println("-----------------------------------------") |
|||
for (1.0e-5 < difference) { |
|||
phi1 = 1.0 + (1.0 / phi0) |
|||
difference = abs((phi1 - phi0)) |
|||
phi0 = phi1 |
|||
count += 1 |
|||
fmt.printf("Iteration: %2d : iterations ==> %.8f\n", count, phi1) |
|||
} |
|||
fmt.println("-----------------------------------------") |
|||
fmt.printf("Result: %.8f after %d iterations", phi1, count) |
|||
fmt.printf("\nThe error is approximately %.10f\n", (phi1 - (0.5 * (1.0 + math.sqrt_f64(5.0))))) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Golden ratio/Convergence |
|||
----------------------------------------- |
|||
Iteration: 01 : iterations ==> 2.00000000 |
|||
Iteration: 02 : iterations ==> 1.50000000 |
|||
Iteration: 03 : iterations ==> 1.66666667 |
|||
Iteration: 04 : iterations ==> 1.60000000 |
|||
Iteration: 05 : iterations ==> 1.62500000 |
|||
Iteration: 06 : iterations ==> 1.61538462 |
|||
Iteration: 07 : iterations ==> 1.61904762 |
|||
Iteration: 08 : iterations ==> 1.61764706 |
|||
Iteration: 09 : iterations ==> 1.61818182 |
|||
Iteration: 10 : iterations ==> 1.61797753 |
|||
Iteration: 11 : iterations ==> 1.61805556 |
|||
Iteration: 12 : iterations ==> 1.61802575 |
|||
Iteration: 13 : iterations ==> 1.61803714 |
|||
Iteration: 14 : iterations ==> 1.61803279 |
|||
----------------------------------------- |
|||
Result: 1.61803279 after 14 iterations |
|||
The error is approximately -0.0000012019 |
|||
</pre> |
|||
=={{header|ObjectIcon}}== |
=={{header|ObjectIcon}}== |