{"id":3647,"date":"2019-06-29T00:01:42","date_gmt":"2019-06-29T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3647"},"modified":"2019-06-22T11:20:48","modified_gmt":"2019-06-22T18:20:48","slug":"exponentiation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3647","title":{"rendered":"Exponentiation"},"content":{"rendered":"<p>The C language lacks an exponentiation operator, which would raise a base value to a certain power. For example: <code>2^8<\/code>, which in some programming languages is an expression to raise 2 to the 8th power. Instead, C uses the <em>pow()<\/em> function.<br \/>\n<!--more--><br \/>\nBefore powering forward, I&#8217;ll dispense with some jargon. Happily refer to Figure 1.<\/p>\n<div id=\"attachment_3651\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3651\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/06\/0629-figure1.png\" alt=\"\" width=\"450\" height=\"126\" class=\"size-full wp-image-3651\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/06\/0629-figure1.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/06\/0629-figure1-300x84.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-3651\" class=\"wp-caption-text\">Figure 1. Mathematical stuff, beautifully illustrated.<\/p><\/div>\n<p>In the expression <code>2^8<\/code>, 2 is the base and 8 is the exponent. Using the format presented in Figure 1, this expression is written as 2<sup>8<\/sup>. This format is common in written mathematics, but in C, it&#8217;s written as <code>pow(2,8)<\/code>.<\/p>\n<p>Being a math noob as I am, I figured that the exponent argument must always be a positive integer value. Yes: I know! Math nerds, contain yourselves! The exponent argument in the <em>pow()<\/em> function is a <em>double<\/em>, so it can be any real value, not just positive integers.<\/p>\n<p>So I got thinking. What about fractional values? What about raising the power of 2 to the <sup>1<\/sup>\/<sub>10<\/sub><sup>th<\/sup> power? To wit:<\/p>\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    int e;\r\n    double exponent;\r\n\r\n    exponent = 1.0;\r\n    for( e=0; e&lt;21; e++ )\r\n    {\r\n        printf(\"2 to the %.4f power is %f\\n\",\r\n                exponent,\r\n                pow( 2, exponent)\r\n              );\r\n        exponent -= 0.05;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>(Some compilers may require the math library to be linked in to create the program.)<\/p>\n<p>The <em>for<\/em> loop decreases the <code>exponent<\/code> value by .05 each turn, raising the power of 2 to fractional values. Here&#8217;s the output:<\/p>\n<p><code>2 to the 1.0000 power is 2.000000<br \/>\n2 to the 0.9500 power is 1.931873<br \/>\n2 to the 0.9000 power is 1.866066<br \/>\n2 to the 0.8500 power is 1.802501<br \/>\n2 to the 0.8000 power is 1.741101<br \/>\n2 to the 0.7500 power is 1.681793<br \/>\n2 to the 0.7000 power is 1.624505<br \/>\n2 to the 0.6500 power is 1.569168<br \/>\n2 to the 0.6000 power is 1.515717<br \/>\n2 to the 0.5500 power is 1.464086<br \/>\n2 to the 0.5000 power is 1.414214<br \/>\n2 to the 0.4500 power is 1.366040<br \/>\n2 to the 0.4000 power is 1.319508<br \/>\n2 to the 0.3500 power is 1.274561<br \/>\n2 to the 0.3000 power is 1.231144<br \/>\n2 to the 0.2500 power is 1.189207<br \/>\n2 to the 0.2000 power is 1.148698<br \/>\n2 to the 0.1500 power is 1.109569<br \/>\n2 to the 0.1000 power is 1.071773<br \/>\n2 to the 0.0500 power is 1.035265<br \/>\n2 to the -0.0000 power is 1.000000<\/code><\/p>\n<p>In the middle, you see 2 raised to the 0.5000 power, which is 2<sup><sup>1<\/sup>\/<sub>2<\/sub><\/sup> power, or the square root of two, 1.414214. If you&#8217;re clever, I suppose you could use the <em>pow()<\/em> function with the fraction 0.5 and dispense with the companion <em>sqrt()<\/em> function.<\/p>\n<p>Just as you can stuff a fraction into the exponent argument in the <em>pow()<\/em> function, you can also dabble with negative numbers. Scary stuff.<\/p>\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    int e;\r\n    double exponent;\r\n\r\n    exponent = -1.0;\r\n    for( e=0; e&lt;20; e++ )\r\n    {\r\n        printf(\"2 to the %.4f power is %f\\n\",\r\n                exponent,\r\n                pow( 2, exponent)\r\n              );\r\n        exponent -= 0.2;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I have no idea what&#8217;s going on here, but the output is interesting:<\/p>\n<p><code>2 to the -1.0000 power is 0.500000<br \/>\n2 to the -1.2000 power is 0.435275<br \/>\n2 to the -1.4000 power is 0.378929<br \/>\n2 to the -1.6000 power is 0.329877<br \/>\n2 to the -1.8000 power is 0.287175<br \/>\n2 to the -2.0000 power is 0.250000<br \/>\n2 to the -2.2000 power is 0.217638<br \/>\n2 to the -2.4000 power is 0.189465<br \/>\n2 to the -2.6000 power is 0.164938<br \/>\n2 to the -2.8000 power is 0.143587<br \/>\n2 to the -3.0000 power is 0.125000<br \/>\n2 to the -3.2000 power is 0.108819<br \/>\n2 to the -3.4000 power is 0.094732<br \/>\n2 to the -3.6000 power is 0.082469<br \/>\n2 to the -3.8000 power is 0.071794<br \/>\n2 to the -4.0000 power is 0.062500<br \/>\n2 to the -4.2000 power is 0.054409<br \/>\n2 to the -4.4000 power is 0.047366<br \/>\n2 to the -4.6000 power is 0.041235<br \/>\n2 to the -4.8000 power is 0.035897<\/code><\/p>\n<p>I see that 2<sup>-1<\/sup> is 0.5000, or <sup>1<\/sup>\/<sub>2<\/sub>. And 2<sup>-2<\/sup> is 0.2500 or <sup>1<\/sup>\/<sub>4<\/sub>; 2<sup>-3<\/sup> is 0.1250 or <sup>1<\/sup>\/<sub>8<\/sub>; and 2<sup>-4<\/sup> is 0.0625 or <sup>1<\/sup>\/<sub>16<\/sub>. Fascinating.<\/p>\n<p>Somewhere on YouTube I&#8217;m sure I could watch a video that would explain how the math works, drop the term &#8220;logarithm&#8221; a few times and have me nodding me head in vague agreement. But then after watching this video, I&#8217;d probably watch the Vienna Philharmonic play the <em>Imperial March<\/em> and forget all about the math. I prefer things that way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m no math genius, which probably explains my newfound fascinations with the <em>pow()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3647\">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-3647","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\/3647","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=3647"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3647\/revisions"}],"predecessor-version":[{"id":3661,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3647\/revisions\/3661"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}