{"id":236,"date":"2013-08-24T00:01:07","date_gmt":"2013-08-24T08:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=236"},"modified":"2013-08-17T08:23:05","modified_gmt":"2013-08-17T16:23:05","slug":"why-not-float-all-the-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=236","title":{"rendered":"Why Not Float All the Time?"},"content":{"rendered":"<p>What&#8217;s the point of using integers when you could simply code programs using floating-point values all the time to always get the best results? The reason is a good one, but it takes some explaining.<br \/>\n<!--more--><br \/>\nThe answer has to deal with storage and processing power. Integers take less processing power to manipulate. Floating point values take more processing power. But just as you could use a screwdriver to pound in a nail, it&#8217;s always best to use the proper tool for the job.<\/p>\n<p>As an example, consider an <em>int<\/em> and a <em>float<\/em> variable. According to the <em>sizeof<\/em> operator, on my computer both variable types occupy four bytes of storage.<\/p>\n<p>The four bytes of storage are just that as far as the computer is concerned. It&#8217;s how the bytes are read that&#8217;s important. Consider these declarations:<\/p>\n<p><code>int i = 486;<br \/>\nfloat f = 123.45;<\/code><\/p>\n<p>The value stored in variable <code>i<\/code> is 486. Its binary representation is:<\/p>\n<p><code>0000-0000 0000-0000 0000-0001 1110-0110<\/code> or <code>0x1e6<\/code><\/p>\n<p>(I&#8217;ve added the dashes and spaces above to make the four bytes readable.) That&#8217;s straightforward: Binary <code>111100110<\/code> is hex <code>0x1e6<\/code>, which is decimal value <code>486<\/code>. Maybe not obvious to you, but easy for a computer.<\/p>\n<p>The value stored in variable <code>f<\/code> is 123.45. Its binary representation is:<\/p>\n<p><code>0110-0110 1110-0110 1111-0110 0100-0010<\/code> or <code>0x66e6f642<\/code><\/p>\n<p>If the hex value <code>0x66e6f642<\/code> is read as decimal, it shows up as 1,726,412,354. (I added commas for readability.) That&#8217;s nowhere near 123.45, so the processor is doing some kind of sorcery to extrapolate the proper floating point value.<\/p>\n<p>(Just because you&#8217;re curious, I had to use a debugger to view the binary representation of floating-point value 123.45.)<\/p>\n<p>The integer value 486, on the other hand, is read and digested in one gulp. It&#8217;s easy for the processor to deal with <code>int<\/code> values because, in a way, integers are its native tongue. Floating point values require translation. Back in the old days, that duty was handled by a separate math co-processor. Today, the math processor is integrated into the main CPU, but it&#8217;s still used to efficiently deal with non-integer values.<\/p>\n<p>So while you could code all the variables in your program as floating point, you&#8217;re just adding more overhead. With today&#8217;s fast processors you probably won&#8217;t see much speed impact, but the point is why add overhead when you don&#8217;t have to? Instead, code integer values as <em>int<\/em>s and use floating-point variables only when necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The computer&#8217;s processor views integer values and floating point numbers different. It&#8217;s important to choose the right type when you code your programs. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=236\">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-236","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\/236","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=236"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}