Fibonacci number: 1, 1, 2, 3, 5, 8, 13 ............... Wait for the program to launch. Let's take a look at the rule.
The golden ratio calculation method, which is equal to the ratio of the last two items of the Fibonacci number. In this program, we make the absolute values of two consecutive proportional differences less than 10 to the power of-12, it is considered accurate enough, the program output and termination.
Basic Program Framework:
# Include <stdio. h> main () {Double X, xnew; // X is the current number, and xnew is the next number. However, because this program calculates the ratio, double different must be used for both of them, ratio, ratio_new, sum; // ratio is the ratio of the previous time and has been calculated. Ratio_new is the proportion just obtained. // Because the two ratios must be compared here and the difference value is less than 10 to the power of 12, both ratios must be used. X = 0; // 0th x new = 1; // 1ratio_new = 0; do {} while (different> = 1e-12 ); // loop printf ("golden ratio = % lf \ n", ratio_new) when the error is too large );}
Complete procedure
# Include <stdio. h> # include <math. h> main () {Double X, xnew; double different, ratio, ratio_new, sum; X = 0; xnew = 1; ratio_new = 0; do {// The following three rows calculate the Fibonacci number. Sum = x + xnew; X = xnew; xnew = sum;Ratio = ratio_new; // make the old proportion equal to the new proportion ratio_new = x/xnew; // then update the new proportion, ratio and ratio_new will always represent the old and new proportions. X/xnew is the calculated ratio different = FABS (ratio-ratio_new); // determine the difference between the two ratios} while (different> = 1e-12 ); // loop printf ("golden ratio = % lf \ n", ratio_new) when the error is too large );}