Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created September 19, 2017 06:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thinkphp/a219babe12a865c831bd29bd42afe42f to your computer and use it in GitHub Desktop.
Golden Ratio in C lang
#include <stdio.h>
#include <math.h>
float golden_ration_root_iteration(float phi, float EPS) {
float tmp = 0.0;
phi = 1.0;
EPS = 1E-8;
while( (phi > tmp) ? (phi - tmp) : (tmp - phi) > EPS) {
tmp = phi;
phi = sqrt(1.0 + phi);
}
return phi;
};
float golden_ration_fractional_iteration(float phi, float EPS) {
float tmp = 0.0;
phi = 1.0;
EPS = 1E-8;
while( (phi > tmp) ? (phi - tmp) : (tmp - phi) > EPS) {
tmp = phi;
phi = 1.0 + (1.0 / phi);
}
return phi;
};
int main() {
printf("%lf\n", golden_ration_root_iteration(1.0, 1E-8));
printf("%lf\n", golden_ration_fractional_iteration(1.0, 1E-8));
return(0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment