Created
September 19, 2017 06:36
-
-
Save thinkphp/a219babe12a865c831bd29bd42afe42f to your computer and use it in GitHub Desktop.
Golden Ratio in C lang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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