{"id":6293,"date":"2024-03-16T00:01:02","date_gmt":"2024-03-16T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6293"},"modified":"2024-03-09T12:50:26","modified_gmt":"2024-03-09T20:50:26","slug":"squaring-a-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6293","title":{"rendered":"Squaring a Value"},"content":{"rendered":"<p>The C language lacks an operator that squares a value or, more generally, raises a value to a specific power. To do so, use the <em>pow()<\/em> function. Yet I&#8217;m finding the need for a <em>square()<\/em> function as I explore some interesting and obscure mathematical thingies.<br \/>\n<!--more--><br \/>\nIn my digital travels, I&#8217;ve seen a few &#8220;square&#8221; or &#8220;power&#8221; operators. Perhaps the most common is the caret, <code>^<\/code>, as in <code>2^5<\/code> which raises two to the fifth power. Remember that in C, the <code>^<\/code> is the bitwise exclusive OR operator.<\/p>\n<p>Another exponent operator I&#8217;ve seen is <code>**<\/code> as in <code>2**5<\/code>, which raises two to the fifth. In C, <code>**<\/code> is used to identify a pointer to a pointer (an array of pointers).<\/p>\n<p>The lack of an exponent operator in C means that programmers use the <em>pow()<\/em> function to raise a value to a specific power:<\/p>\n<p><code>pow(2,5);<\/code><\/p>\n<p>The above statement raises two to the fifth power. It requires that you include the <code>math.h<\/code> header file. On some systems, you must also remember to link in the math library (<code>-lm<\/code>) on the command line.<\/p>\n<p>Squaring a value happens frequently in math. The way I often code it in C is to write <code>alpha*alpha<\/code> as opposed to <code>pow(alpha,2);<\/code> I would guess that <code>alpha*alpha<\/code> probably calculates faster internally.<\/p>\n<p>The following code demonstrates both methods to square a value:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03_16-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_03_16-Lesson-a.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    int alpha = 5;\r\n\r\n    printf(\"Alpha = %d\\n\",alpha);\r\n    printf(\"Alpha squared is %d\\n\",alpha*alpha);\r\n    printf(\"Alpha squared is %.f\\n\",pow(alpha,2) );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<p><code>Alpha = 5<br \/>\nAlpha squared is 25<br \/>\nAlpha squared is 25<\/code><\/p>\n<p>If I&#8217;m desperate for a <em>square()<\/em> function, I could write one. But instead, I think it would be better and more nerdy to code a macro, as shown in this update to the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03_16-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_03_16-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#define squared(a) a*a\r\n\r\nint main()\r\n{\r\n    int alpha = 5;\r\n\r\n    printf(\"Alpha = %d\\n\",alpha);\r\n    printf(\"Alpha squared is %d\\n\",alpha*alpha);\r\n    printf(\"Alpha squared is %.f\\n\",pow(alpha,2) );\r\n    printf(\"Alpha squared is %d\\n\",squared(alpha) );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>squared()<\/em> &#8220;function&#8221; is really a macro. It accepts an argument, represented as <code>a<\/code> in the definition, then replaces it with <code>a*a<\/code>:<\/p>\n<p><code>#define squared(a) a*a<\/code><\/p>\n<p>In the code, the final <em>printf()<\/em> statement is translated by the precompiler to this:<\/p>\n<p><code>printf(\"Alpha squared is %d\\n\",squared(alpha*alpha) );<\/code><\/p>\n<p>The output is the same as the first <em>printf()<\/em> statement. I don&#8217;t think the macro saves any time, but it does add to readability, which is what I wanted.<\/p>\n<p>Well, what I&#8217;d really want is an exponent operator, but that&#8217;s never going to happen in the C language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Could the C language use a <em>square()<\/em> function or operator? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6293\">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-6293","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\/6293","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=6293"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6293\/revisions"}],"predecessor-version":[{"id":6309,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6293\/revisions\/6309"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}