Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Last active November 9, 2018 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhedonia/6ddfa7f79638c0cefaae4c9fe915ecc7 to your computer and use it in GitHub Desktop.
Save nikhedonia/6ddfa7f79638c0cefaae4c9fe915ecc7 to your computer and use it in GitHub Desktop.
double golden(double epsilon=0.00001) {
int a = 0;
int b = 1;
double ratio = 0;
while (1) {
tie(a, b) = tuple{b, a+b};
auto delta = b / (double)a - ratio;
ratio = b / (double)a;
if ( abs(delta) < epsilon )
return ratio;
}
}
int main() {
  cout << golden() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment