{"id":2995,"date":"2018-03-10T00:01:03","date_gmt":"2018-03-10T08:01:03","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2995"},"modified":"2026-03-07T10:13:12","modified_gmt":"2026-03-07T18:13:12","slug":"day","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2995","title":{"rendered":"&pi; Day"},"content":{"rendered":"<p>Mathematic geeks of the world celebrate March 14 as &#8220;pi day.&#8221; The logic is that the date is 3\/14, which are the first three digits of the value &pi;, or 3.14159&#8230;.&#8734;<br \/>\n<!--more--><br \/>\nIf you really want to geek out, I can recommend a book, the <em>History of Pi<\/em> by Petr Beckmann. I don&#8217;t consider myself a genius at math, but I enjoyed reading this book.<\/p>\n<div id=\"attachment_7473\" style=\"width: 208px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7473\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/A-History-of-PI-198x300.png\" alt=\"\" width=\"198\" height=\"300\" class=\"size-medium wp-image-7473\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/A-History-of-PI-198x300.png 198w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/A-History-of-PI.png 378w\" sizes=\"auto, (max-width: 198px) 100vw, 198px\" \/><p id=\"caption-attachment-7473\" class=\"wp-caption-text\"><a href=\"https:\/\/amzn.to\/4s86ihK\" target=\"_blank\">A History of &pi; by Petr Beckmann<\/a><\/p><\/div>\n<p>Due to edicts from the Technology Writing Guild, I must state that &pi; is defined as the ratio of a circle&#8217;s diameter to its circumference. The value represents the number of times you can wrap the diameter around the circle, and it&#8217;s constant for all circles. It&#8217;s also irrational, which means it makes absurd political arguments but it also cannot be expressed as a ratio of two integer values. Further, &pi; is transcendental, which means it cannot be otherwise expressed by using algebra. The digits after the decimal in &pi; never repeat.<\/p>\n<p>As you may guess, it&#8217;s possible to calculate the value of &pi; by using a computer. The C language is one of the many tools you can use to do so. Before getting into the techniques, be aware that the value &pi; is available as a constant defined in the <code>math.h<\/code> header file, <code>M_PI<\/code>. If you need to use &pi; in your code, just include <code>math.h<\/code> and use the <code>M_PI<\/code> definition and you&#8217;re good.<\/p>\n<p>As far as calculating the value of &pi;, different programmers have their own techniques. Each is based on one of the many odd methods by which the Mentats and Vulcans calculate &pi;. The method I chose to emulate in my code is called a <em>generalized continuing fraction<\/em>, illustrated in Figure 1.<\/p>\n<div id=\"attachment_2997\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2997\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/0310-pi_calculation.png\" alt=\"\" width=\"400\" height=\"270\" class=\"size-full wp-image-2997\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/0310-pi_calculation.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/03\/0310-pi_calculation-300x203.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-2997\" class=\"wp-caption-text\">Figure 1. One of the weird cascading fraction things used to calculate the value of &pi;.<\/p><\/div>\n<p>If you look at the calculation, you see that the numbers cascade in a neat pattern: The numerator is 1, 2, 3, 4 squared and so on. The denominator is 1, 3, 5, 7, 9, and so on.<\/p>\n<p>While the fraction shown in Figure 1 is terrifying, it screams out &#8220;recursion!&#8221; to me. Having a fraction where the denominator keeps spiraling down and down is the same concept as <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1060\">recursion<\/a> in a programming language. That, combined with the nifty manner in which the numbers increment, implies that you can craft C language code to calculate the value of &pi; &mdash; which is what I did.<\/p>\n<p>The code shown below is my attempt to emulate the continuing fraction (Figure 1) by using recursion in C. The recursive function is <em>p()<\/em>, which uses two float variables, <code>a<\/code> and <code>b<\/code>: <code>a<\/code> is the left-side, odd-number values shown in Figure 1; <code>b<\/code> is the right &#8220;squared&#8221; value.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nfloat p(float a,float b)\r\n{\r\n    return((b&lt;100.0)?a+(b*b\/p(a+2.0,b+1.0)):b*b);\r\n}\r\n\r\nint main()\r\n{\r\n    printf(\"%.7f\\n\",4.0\/p(1.0,1.0));\r\n    return(0);\r\n}<\/pre>\n<p>The equation starts with 4.0 at Line 10, inside the <em>printf()<\/em> statement. Values 1.0 and 1.0 are then sent to <em>p()<\/em>.<\/p>\n<p>Within <em>p()<\/em>, the <em>return()<\/em> statement handles the recursive return condition and the division calculation. The return condition is <code>b&lt;100<\/code>. So the recursion continues as long as value <code>b<\/code> (on the top in the cascading fraction) is less than 100. Otherwise, the fraction is expressed by using this equation:<\/p>\n<p><code>a+(b*b\/p(a+2.0,b+1.0))<\/code><\/p>\n<p>Here you see the value <code>a<\/code>, <code>b<sup>2<\/sup><\/code> and then the bottom part of the fraction is the recursion, calling the <em>p()<\/em> function again, but with new values: <code>a<\/code> increased by 2 and <code>b<\/code> increased by 1.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>3.1415925<\/code><\/p>\n<p>It&#8217;s accurate out to six digits. (The constant <code>M_PI<\/code> is equal to 3.14159265358979323846264338327950288.) That&#8217;s about as good as you can get with this code. You can mess with the recursion exit condition, <code>b&lt;100<\/code>, though eventually recursion is going to overflow the stack.<\/p>\n<p>The internet is brimming with various other examples of calculating &pi;. And I know research teams at universities are constantly searching for that elusive &#8220;final digit.&#8221; It&#8217;s fun and distracting.<\/p>\n<p>Enjoy &pi; day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not quite pi day, but here on the C blog, I&#8217;m always willing to write a &pi;-relevant Lesson. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2995\">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-2995","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\/2995","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=2995"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2995\/revisions"}],"predecessor-version":[{"id":7474,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2995\/revisions\/7474"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}