{"id":1968,"date":"2016-06-18T00:01:27","date_gmt":"2016-06-18T07:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1968"},"modified":"2016-06-11T08:40:27","modified_gmt":"2016-06-11T15:40:27","slug":"the-powerful-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1968","title":{"rendered":"The <em>pow()<\/em>erful Function"},"content":{"rendered":"<p>When I first learned the C language, I was surprised to find something missing from its assortment of operators. The <code>+<\/code>, <code>-<\/code>, <code>*<\/code>, and <code>\/<\/code> operators are pretty common for nearly all programming languages. And you&#8217;ll find the <code>%<\/code> and <code>!<\/code> operators used for modulus and logical NOT in a few programming languages. Yet what other languages have that C lacks is an exponent or power operator.<br \/>\n<!--more--><br \/>\nIn other languages, I recall the <code>^<\/code> operator was the power operator. In BASIC, for example, you could write the following expression:<\/p>\n<p><code>2^10<\/code><\/p>\n<p>Which means 2<sup>10<\/sup> or two to the tenth power, 1024. In the C language, however, <code>^<\/code> is the exclusive OR (or XOR) operator. <\/p>\n<p>It makes sense that the C language prioritizes an bitwise operator for <code>^<\/code> instead of using it as a power operator: C is a mid-level programming language. That doesn&#8217;t imply that you can&#8217;t raise a value to a specific power. The tool to perform that operation is the <em>pow()<\/em> function.<\/p>\n<p>Here is the <em>pow()<\/em> function&#8217;s man page definition:<\/p>\n<pre><code>double pow(double x, double y)<\/code><\/pre>\n<p><code>x<\/code> is the base and <code>y<\/code> is the exponent. The values are all <em>doubles<\/em>, which makes sense because the results could be incredibly huge or infinitesimally small.<\/p>\n<p>The <em>pow()<\/em> function is declared in the <code>math.h<\/code> header file.<\/p>\n<p>The following code uses the <em>pow()<\/em> function to display powers of 2 from 1 through 10, which are coincidentally the first ten holy computer numbers:<\/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    double t;\r\n\r\n    for(t=1.0;t&lt;=10.0;t++)\r\n        printf(\"2 to the %.f power is %.f\\n\",\r\n                t,\r\n                pow(2.0,t));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 6 declares variable <code>t<\/code> to be a <em>double<\/em>, which is required for the <em>pow()<\/em> function.<\/p>\n<p>The <em>for<\/em> loop at Line 8 uses <em>double<\/em> values, 1.0 and 10.0 for variable <code>t<\/code>. Remember that floating point values (literals) in C must be specified with a decimal part. So values 1.0 and 10.0 are used where you&#8217;d otherwise set integers.<\/p>\n<p>The <em>pow()<\/em> function appears within the <em>printf()<\/em> statement, which is split between Lines 9, 10, and 11. The <em>double<\/em> constant 2.0 is specified directly in the function. Also, the <em>printf()<\/em> function uses the <code>%.f<\/code> placeholder to ensure that the output contains only the whole number portion of the answer.<\/p>\n<p>Here is the code&#8217;s output:<\/p>\n<pre><code>2 to the 1 power is 2\r\n2 to the 2 power is 4\r\n2 to the 3 power is 8\r\n2 to the 4 power is 16\r\n2 to the 5 power is 32\r\n2 to the 6 power is 64\r\n2 to the 7 power is 128\r\n2 to the 8 power is 256\r\n2 to the 9 power is 512\r\n2 to the 10 power is 1024<\/code><\/pre>\n<p>If you replace the <code>%.f<\/code> placeholder with <code>%f<\/code> (which might be your first inclination), the output looks like this:<\/p>\n<pre><code>2 to the 1.000000 power is 2.000000\r\n2 to the 2.000000 power is 4.000000\r\n2 to the 3.000000 power is 8.000000\r\n2 to the 4.000000 power is 16.000000\r\n2 to the 5.000000 power is 32.000000\r\n2 to the 6.000000 power is 64.000000\r\n2 to the 7.000000 power is 128.000000\r\n2 to the 8.000000 power is 256.000000\r\n2 to the 9.000000 power is 512.000000\r\n2 to the 10.000000 power is 1024.000000<\/code><\/pre>\n<p>That&#8217;s a bit confusing, so you can see why I instead specified <code>%.f<\/code> in the <em>printf()<\/em> statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C language lacks an exponential operator. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1968\">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-1968","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\/1968","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=1968"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1968\/revisions"}],"predecessor-version":[{"id":1984,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1968\/revisions\/1984"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}