{"id":660,"date":"2018-03-20T07:00:00","date_gmt":"2018-03-20T14:00:00","guid":{"rendered":"https:\/\/coreassistance.com\/tips\/?p=660"},"modified":"2018-03-12T20:46:20","modified_gmt":"2018-03-13T03:46:20","slug":"computers-almost-always-great-at-math","status":"publish","type":"post","link":"https:\/\/coreassistance.com\/tips\/2018\/03\/20\/computers-almost-always-great-at-math\/","title":{"rendered":"Computers: Almost Always Great at Math"},"content":{"rendered":"<p>One of the reasons I love computers so much is because I hate math.  Math and I do not get along, so having a magic device that can do most any math problem I throw at it instantly and accurately has a great deal of appeal for me.<\/p>\n<p>Except, well, sometimes that accuracy piece isn&#8217;t all it&#8217;s cracked up to be.<\/p>\n<p>Let&#8217;s take a look at an extremely simple bit of code:<\/p>\n<pre><code class=\"javascript\">var result = 0.1 + 0.2;<\/code><\/pre>\n<p>After running this code you&#8217;d expect the <code>result<\/code> variable to contain the value <code>0.3<\/code>, but that&#8217;s not what happens.  When this code is run the value stored in the <code>result<\/code> variable will, in fact, be <code>0.30000000000000004<\/code>.<\/p>\n<p>I know what you&#8217;re thinking, but no, this isn&#8217;t another strange JavaScript thing.  This is, in fact, a strange <em>computer<\/em> thing.  If you were to run similar code in languages like C or Java you would get the same result.<\/p>\n<p>The reason for this behavior has to do with how computers translate back and forth between the base 10 numbering system we humans are used to and the base 2 numbering system they use internally.  If you&#8217;re curious about it check out the aptly named <a href=\"http:\/\/0.30000000000000004.com\/\">0.30000000000000004.com website<\/a> for the technical details (and links to even more technical details).<\/p>\n<p>Suffice it to say, you should be careful when doing floating point math.  Run some tests and make sure you&#8217;re getting the results you expect.  If you&#8217;re not, make changes to how you do math based on what you now know about how computers handle floating point numbers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find out why 0.1 + 0.2 doesn&#8217;t always equal 0.3.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-660","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":5,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":720,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/660\/revisions\/720"}],"wp:attachment":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}