{"id":4246,"date":"2020-07-18T00:01:02","date_gmt":"2020-07-18T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4246"},"modified":"2022-11-20T09:48:02","modified_gmt":"2022-11-20T17:48:02","slug":"getting-to-the-square-root-of-the-problem","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4246","title":{"rendered":"Getting to the Square Root of the Problem"},"content":{"rendered":"<p>The first part of my writing career began by updating a computer book bestseller, <em>The BASIC Handbook<\/em>. This tome served as a lexicon for the various BASIC programming language dialects, allowing users from one version of BASIC to translate and use code from another version.<br \/>\n<!--more--><br \/>\n<div id=\"attachment_4247\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4247\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/07\/The-BASIC-Handbook.png\" alt=\"The BASIC Handbook cover\" width=\"350\" height=\"467\" class=\"size-full wp-image-4247\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/07\/The-BASIC-Handbook.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/07\/The-BASIC-Handbook-225x300.png 225w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><p id=\"caption-attachment-4247\" class=\"wp-caption-text\">Figure 1. <em>The BASIC Handbook<\/em>, Compusoft Publishing, 1986.<\/p><\/div><\/p>\n<p>One of the more interesting parts of the book included BASIC code in the form of a generic algorithm. You could use the algorithm in case your version of BASIC lacked a command available for other versions.<\/p>\n<p>For example, if your version of BASIC lacked the absolute value command, <code>ABS<\/code>, you could write your own. From the book:<\/p>\n<pre class=\"screen\">\r\n29999 END\r\n30000 REM * 'ABS' SUBROUTINE *\r\n30010 REM * INPUT IS X, OUTPUT IS Y *\r\n30020 Y = X\r\n30030 IF X>=0 THEN 30060\r\n30040 Y = -X\r\n30050 RETURN<\/pre>\n<p>Looking back on that code in the book, it&#8217;s amazing how sloppy BASIC can be. Further, I found a typo: Line 30030 incorrectly lists Line 30060 as the <code>RETURN<\/code> command. It should be 30050. Ugh.<\/p>\n<p>Generally speaking, C programmers are familiar with this book&#8217;s approach for missing functions. C lacks many of the higher-level functions of other languages. So programmers write their own. In fact, sites like <a href=\"https:\/\/stackoverflow.com\/\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/stackoverflow.com\/<\/a> are rife with examples of &#8220;missing&#8221; C language functions.<\/p>\n<p>Still, it&#8217;s intriguing for me to read algorithms that handle functions we take for granted. One of these mentioned in the book is the square root command.<\/p>\n<p>From <em>The BASIC Handbook<\/em>, if your version of BASIC lacked the <code>SQR<\/code> (square root) command you could substitute the following code:<\/p>\n<pre class=\"screen\">\r\n29999 END\r\n30000 REM * SQUARE ROOT SUBROUTINE *\r\n30010 REM * INPUT X, OUTPUT Y *\r\n30020 REM * ALSO USES W AND Z *\r\n30030 IF X = 0 THEN 30160\r\n30040 IF X>0 THEN 30070\r\n30050 PRINT \"ERROR - NEGATIVE ROOT\"\r\n30060 STOP\r\n30070 Y = X\/4\r\n30080 Z = 0\r\n30090 W = (X\/Y-Y)\/2\r\n30100 IF W = 0 THEN 30160\r\n30110 IF W = Z THEN 30160\r\n30120 Y = Y + W\r\n30130 Z = W\r\n30140 GOTO 30090\r\n30150 Y = 0\r\n30160 RETURN<\/pre>\n<p>Again, this code has a boner in it: Line 30150 will never be interpreted. I believe the implied <code>GOTO<\/code> at Line 30030 should branch to Line 30150, though it instead goes to the <code>RETURN<\/code> command at Line 30160. Whatever. Because most versions of BASIC implemented the square root (<code>SQR<\/code>)  command, no one probably ever used this code. I&#8217;m not even certain the testers ran it. I don&#8217;t remember.<\/p>\n<p>Anyway, I thought it would be fun to translate this code into C just to see whether it works. After all, code is code; a lot of what&#8217;s written above translates directly into C. With a few modifications, I&#8217;d roll my own <em>sqrt()<\/em> function. Wouldn&#8217;t that be grand?<\/p>\n<p>As it turns out, the thing didn&#8217;t work. I won&#8217;t even bother posting my code because the algorithm, at least as I translated it into C, died in an endless loop. The BASIC code above is most likely flawed. Pity.<\/p>\n<p>Not one to give up, I decided to craft my own square root function. I&#8217;d start from scratch, determining how a square root is calculated in the first place &mdash; well, other than using a calculator, which is probably what I was taught in school.<\/p>\n<p>I explore how to calculate a square root in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4250\">next week&#8217;s Lesson<\/a>, where I introduce my square root substitute function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if the <em>sqrt()<\/em> function didn&#8217;t exist! What would you do? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4246\">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-4246","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\/4246","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=4246"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4246\/revisions"}],"predecessor-version":[{"id":5656,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4246\/revisions\/5656"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}