{"id":7426,"date":"2026-03-14T00:01:30","date_gmt":"2026-03-14T07:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7426"},"modified":"2026-03-07T10:36:09","modified_gmt":"2026-03-07T18:36:09","slug":"day-2026","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7426","title":{"rendered":"&pi; Day 2026"},"content":{"rendered":"<p>It&#8217;s been a few years since I&#8217;ve had &pi; day here on the C blog. Because I schedule posts for Saturdays, only thrice has a post fallen on &pi; day: In 2015, 2020, and now in 2026.<br \/>\n<!--more--><br \/>\nToday is known as &pi; day because it&#8217;s 3\/14, which are the first three digits of &pi;: 3.14159&#8230; So why not code yet another program that uses some clever mathematical trick to generate the value of &pi;?<\/p>\n<p>The secret, of course, is to locate some famous mathematician&#8217;s efforts to use a strange calculation that generates the value of &pi;. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4042\">2020<\/a>, I used the Wallis Product. This time, I&#8217;m thieving the  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Leibniz_formula_for_%CF%80\" target=\"_blank\">Leibniz Formula<\/a>. Here is the mumbo jumbo artfully depicted in Figure 1:<\/p>\n<div id=\"attachment_7429\" style=\"width: 564px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7429\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure1.png\" alt=\"Original Leibniz formula for pi; mathematical equation\" width=\"554\" height=\"106\" class=\"size-full wp-image-7429\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure1.png 554w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure1-300x57.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure1-500x96.png 500w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><p id=\"caption-attachment-7429\" class=\"wp-caption-text\">Figure 1. The Leibniz formula for &pi;.<\/p><\/div>\n<p>Do you see a pattern? The denominators in the fractions show odd numbers: 3, 5, 7, 9, and up. The signs alternates between plus and minus. The trailing three dots denotes an infinite series.<\/p>\n<p>My feeble math brain picked up on a few things right away.<\/p>\n<p>First, the value 1 can be re-written as <sup>1<\/sup>\/<sub>1<\/sub>, shown in Figure 2.<\/p>\n<div id=\"attachment_7430\" style=\"width: 565px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7430\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0315-figure2.png\" alt=\"Leibniz formula for pi with the first fraction re-written; mathematical equation\" width=\"555\" height=\"106\" class=\"size-full wp-image-7430\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0315-figure2.png 555w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0315-figure2-300x57.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0315-figure2-500x95.png 500w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><p id=\"caption-attachment-7430\" class=\"wp-caption-text\">Figure 1. Re-writing the value 1 as 1\/1 in the Leibniz formula for &pi;.<\/p><\/div>\n<p>Second, because the result is written as <sup>&pi;<\/sup>\/<sub>4<\/sub>, it&#8217;s possible to multiple both sides of the equation by four. The result is shown in Figure 3:<\/p>\n<div id=\"attachment_7431\" style=\"width: 562px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7431\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure3.png\" alt=\"Leibniz formula for pi multiplied by four; mathematical equation\" width=\"552\" height=\"106\" class=\"size-full wp-image-7431\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure3.png 552w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure3-300x58.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0314-figure3-500x96.png 500w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><p id=\"caption-attachment-7431\" class=\"wp-caption-text\">Figure 3. After multiplying both sides by 4, the equation calculates the full value of &pi; and is easier to code in C.<\/p><\/div>\n<p>To work through this series you must set a denominator value starting with one and then increasing by two each operation, resulting in the series of odd numbers at the bottom of the fraction. The top value (the numerator) is always going to be four.<\/p>\n<p>Further, the sign must be flipped between each operation, first subtracting the next value and then adding it. I covered this operation in an <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6224\">Exercise blog post<\/a> from 2024.<\/p>\n<p>My attempt to use the Leibniz formula to calculate the value of &pi;, is shown in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_03_14-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_03_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n\r\n#define PI_CHAR 0x03c0\r\n#define ACCURACY 500000\r\n\r\nint main()\r\n{\r\n    float pi = 0.0;\r\n    int loop,denominator,sign;\r\n\r\n    <span class=\"comments\">\/* use Leibniz formula to calculate pi *\/<\/span>\r\n    denominator = 1;\r\n       sign = 0;\r\n    for( loop=0; loop&lt;ACCURACY; loop++ )\r\n    {\r\n        pi += (sign%2?-1:1) * 4.0\/denominator;\r\n        denominator+=2;\r\n        sign++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output wide character 'pi' *\/<\/span>\r\n    setlocale(LC_ALL,\"\");\r\n    wprintf(L\"%lc = %.5f\\n\",PI_CHAR,pi);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>This program outputs <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2568\">wide characters<\/a> (Unicode), so the <code>stdio.h<\/code> header file isn&#8217;t needed. Instead, the headers required for wide characters are used.<\/p>\n<p>The two defined constants are the &pi; Unicode character, <code>PI_CHAR<\/code>, and <code>ACCURACY<\/code>, which sets the number of times the main loop repeats. The greater the value of <code>ACCURACY<\/code>, the more accurate the result, though 500,000 is good enough for this program. (And it&#8217;s good enough for plotting the course of Jupiter&#8217;s orbit to within a few meters.)<\/p>\n<p>The code uses a <em>for<\/em> loop to repeat <code>ACCURACY<\/code> times. Each time, the value of variable <code>pi<\/code> is increased by the value of <code>sign<\/code> (which determines whether to add or subject the next value) multiplied by 4.0 divided by the value of <code>denomniator<\/code>:<\/p>\n<p><code>pi += (sign%2?-1:1) * 4.0\/denominator;<\/code><\/p>\n<p>The expression <code>4.0\/denomniator<\/code> need not be typecast to a <em>float<\/em>, at least not when using the <em>clang<\/em> compiler.<\/p>\n<p>The value of <code>denominator<\/code> is increased by two: <code>denominator+=2;<\/code>, and sign is incremented: <code>sign++;<\/code><\/p>\n<p>The loop repeats, honing the value of <code>pi<\/code> with each iteration. When it&#8217;s done, the value of variable <code>pi<\/code> is output:<\/p>\n<pre>\u03c0 = 3.14159<\/pre>\n<p>Though I may never understand the math, I do enjoy coding these &pi; exercises &mdash; so much so, that I do another one next week, even though it&#8217;s not &pi; day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculating the value of &pi;, this time by using the Leibniz Formula. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7426\">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-7426","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\/7426","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=7426"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7426\/revisions"}],"predecessor-version":[{"id":7465,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7426\/revisions\/7465"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}