{"id":4444,"date":"2020-11-14T00:01:28","date_gmt":"2020-11-14T08:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4444"},"modified":"2021-07-09T16:50:47","modified_gmt":"2021-07-09T23:50:47","slug":"the-golden-ratio","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4444","title":{"rendered":"The Golden Ratio"},"content":{"rendered":"<p>Like many other mathematical delights, the golden ratio pops up all over. It has a rich history, and a special Greek letter to represent it: &phi; <em>phi<\/em>. The value is irrational, meaning it cannot be expressed as a ratio of two integers: 1.6180339&#8230;<br \/>\n<!--more--><br \/>\nWhat this ratio represents is illustrated in Figure 1. The length of <em>a+b<\/em> is the same ratio to <em>a<\/em> as length <em>b<\/em> is to length <em>a<\/em>. This concept is difficult to put into words, which is why I animated the figure.<\/p>\n<div id=\"attachment_4449\" style=\"width: 363px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4449\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114_figure1_goldenratio.gif\" alt=\"Golden ratio animation showing a+b\/a\" width=\"353\" height=\"369\" class=\"size-full wp-image-4449\" \/><p id=\"caption-attachment-4449\" class=\"wp-caption-text\">Figure 1. An animation showing the relationship between &#8216;a&#8217; and &#8216;b&#8217; relative to each other.<\/p><\/div>\n<p>The expression to describe the relationship between lengths <em>a<\/em> and <em>b<\/em> is written like this:<\/p>\n<p><code>(a+b)\/a == a\/b == &phi;<\/code><\/p>\n<p>Mathematicians using brains far more massive than my own have determined, by using the quadratic formula and other scary words, that this equation works out like this:<\/p>\n<p>(1+&#8730;5)\/2 = 1.6180339887&#8230;<\/p>\n<p>Unlike &pi;, <em>e<\/em>, &#8730;2, and other values, the golden ratio doesn&#8217;t exist as a defined constant in the <code>math.h<\/code> header file. If your code demands it, you must perform some math, as shown in the following example:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_11_14-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_11_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    double gr;\r\n\r\n    gr = (1.0 + sqrt(5)) \/ 2.0;\r\n    printf(\"The golden ratio is %f\\n\",gr);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>math.h<\/code> header file is required for the <em>sqrt()<\/em> function, used in the equation at Line 8. This expression matches the one used to determine the value of &phi;, as shown earlier. And remember, some systems require that you link in the math library (<code>m<\/code>) for the <em>sqrt()<\/em> function to behave properly.<\/p>\n<p>Here is the output:<\/p>\n<p><code>The golden ratio is 1.618034<\/code><\/p>\n<p>A second way exists to calculate the golden ratio. This method using a continued fraction, which is one of those terrifying constructions illustrated in Figure 2.<\/p>\n<div id=\"attachment_4453\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4453\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction.png\" alt=\"phi expressed as a continued fraction, which really looks scary\" width=\"500\" height=\"240\" class=\"size-full wp-image-4453\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction-300x144.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-4453\" class=\"wp-caption-text\">Figure 1. Calculating &phi; as a continued fraction &#8211; something that begs for a recursive function.<\/p><\/div>\n<p>When I see a mathematical goober such as the one shown in Figure 2, as a programmer I see a recursive function. I explore this option for calculating the golden ratio in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4456\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not as well known as &pi; or even the <em>e<\/em> constant, but it&#8217;s still one of those math thingies programmers can play with. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4444\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4444","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4444"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4444\/revisions"}],"predecessor-version":[{"id":4874,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4444\/revisions\/4874"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}