{"id":2461,"date":"2017-04-15T00:01:26","date_gmt":"2017-04-15T07:01:26","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2461"},"modified":"2017-04-08T09:56:26","modified_gmt":"2017-04-08T16:56:26","slug":"surrender-to-the-overflow","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2461","title":{"rendered":"Surrender to the Overflow"},"content":{"rendered":"<p>It&#8217;s a common question beginning programmers ask: &#8220;Why use different types of variables when every number can be expressed as a <em>float<\/em>?&#8221;<br \/>\n<!--more--><br \/>\nThe answer is, &#8220;efficiency.&#8221; It&#8217;s the same reason you write 1 instead of 1.000000 when you do math. A &#8220;1&#8221; is more compact, just as an integer occupies less space and is easier for the processor to manipulate than a real number.<\/p>\n<p>Back when storage space and processor speed were limited, real number calculations became a true burden on the computer. Early CPUs lacked math instructions, so a separate co-processor was required. And storage was tight, so if you could get by with a <em>short int<\/em> value, you declared a variable as such.<\/p>\n<p>Today&#8217;s processors have advanced math instructions built-in, plus the computer has oodles of RAM. Still, it&#8217;s wise to use a <em>float<\/em> or <em>double<\/em> only when a fractional or large value is required. And with integer sizes, a <em>long<\/em> holds more values than an <em>short<\/em>, but it&#8217;s more efficient to use a <em>short<\/em> when <em>long<\/em>-size values aren&#8217;t required. The only problem you run into is when values overflow.<\/p>\n<p>As an example, the following code generates a Fibonacci sequence. It&#8217;s related to Exercise 9-16 in <em>Beginning Programming with C For Dummies<\/em>. The <code>LIMIT<\/code> value sets the length of the Fibonacci sequence:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define LIMIT 10000\r\n\r\nint main()\r\n{\r\n    int fibo,nacci,count;\r\n\r\n    fibo=0;\r\n    nacci=1;\r\n    count=0;\r\n\r\n    do\r\n    {\r\n        printf(\"%d\\n\",fibo);\r\n        fibo+=nacci;\r\n        printf(\"%d\\n\",nacci);\r\n        nacci+=fibo;\r\n        count+=2;\r\n    } while( count &lt; LIMIT );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>When run, you see 10000 numbers in a Fibonacci sequence. On my terminal window, I see the sequence start like this:<\/p>\n<pre><code>0\r\n1\r\n1\r\n2\r\n3\r\n5\r\n8<\/code><\/pre>\n<p>And on and on, until the end, which looks like this:<\/p>\n<pre><code>1640003618\r\n-913691081\r\n726312537\r\n-187378544\r\n538933993\r\n351555449\r\n890489442<\/code><\/pre>\n<p>These are not values in a Fibonacci sequence. In fact, this output is the telltale sign of overflow: The program is running fine, but the variable width is unable to hold the huge value calculated. So the numbers are crazy, with negative values followed by positive values.<\/p>\n<p>To fix overflow, you must set the proper variable size. The range for an <em>int<\/em> variable on my computer is between -2,147,483,648 and 2,147,483,647. So the first change is to declare <code>fibo<\/code> and <code>nacci<\/code> as <em>unsigned int<\/em> values and modify <em>printf()<\/em> to use the <code>%u<\/code> (<em>unsigned int<\/em>) placeholder. The maximum value stored in an <em>unsigned int<\/em> is 4,294,967,295. Is that big enough?<\/p>\n<p>Here is the last part of the program&#8217;s output:<\/p>\n<pre><code>1640003618\r\n3381276215\r\n726312537\r\n4107588752\r\n538933993\r\n351555449\r\n890489442<\/code><\/pre>\n<p>No, overflow is still occurring.<\/p>\n<p>The next step is to change the <em>int<\/em> type to <em>long<\/em>, but I&#8217;ll leapfrog that option and go straight to the <em>unsigned long long<\/em> variable type, which stores a range of values from zero to 18,446,744,073,709,551,615.<\/p>\n<p>The <code>%llu<\/code> placeholder is used in the <em>printf()<\/em> function to display <em>unsigned long long<\/em> integer values.<\/p>\n<p>After modifying the code, I ran it again. Here is the output&#8217;s tail:<\/p>\n<pre><code>3889924131843504162\r\n223230997310352951\r\n4113155129153857113\r\n4336386126464210064\r\n8449541255618067177\r\n12785927382082277241\r\n2788724563990792802<\/code><\/pre>\n<p>Again, I don&#8217;t see a good sequence, which means that the <em>long long int<\/em> variable isn&#8217;t wide enough to hold the 10,000th value in a Fibonacci sequence.<\/p>\n<p>At this, point I surrender to floating point variables, which removes accuracy but might yield correct values. Variables <code>fibo<\/code> and <code>nacci<\/code> are declared as <em>double<\/em>, and the <em>printf()<\/em> placeholder <code>%E<\/code> is used for exponential notation. Here&#8217;s the last part of the output:<\/p>\n<pre><code>INF\r\nINF\r\nINF\r\nINF\r\nINF\r\nINF\r\nINF<\/code><\/pre>\n<p>Oh, well. Apparently the 10,000th value in the Fibonacci sequence is <code>INF<\/code> or <em>infinity<\/em>. It&#8217;s a number so huge that standard programming variables can&#8217;t hold it.<\/p>\n<p>I scrolled back to see the last legitimate value and it was 1.306989E+308, which is huge and not very accurate.<\/p>\n<p>The point of this lesson isn&#8217;t to discover the 10,000th Fibonacci sequence value, but rather to demonstrate when it&#8217;s necessary to switch from <em>int<\/em> to <em>float<\/em> variables and the consequences of doing so.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point, integer values become so large that you must use floating point variables to hold them. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2461\">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-2461","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\/2461","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=2461"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2461\/revisions"}],"predecessor-version":[{"id":2475,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2461\/revisions\/2475"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}