{"id":129,"date":"2015-01-09T22:04:20","date_gmt":"2015-01-09T14:04:20","guid":{"rendered":"http:\/\/googleglass.my\/blog\/?p=129"},"modified":"2015-01-09T22:05:58","modified_gmt":"2015-01-09T14:05:58","slug":"arduino-arithmetic-quirk","status":"publish","type":"post","link":"https:\/\/gri.my\/blog\/arduino-arithmetic-quirk\/","title":{"rendered":"Arduino arithmetic quirk"},"content":{"rendered":"<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nlong bigvalue;\r\nbigvalue\u00a0= 400*400;\r\n<\/pre>\n<p>You&#8217;d expect\u00a0bigvalue to hold 16000 right? No. Apparently arithmetic operation are done using the <a href=\"http:\/\/arduino.cc\/en\/Reference\/Arithmetic\" target=\"_blank\">data type of the operand<\/a>.\u00a0So if\u00a0you do any arithmetic operation (+-*\/) that exceeds what\u00a0the\u00a0operand data\u00a0type can hold, for example\u00a0integer {+32767,-32768} you&#8217;d get some random overflown garbage result, even if the variable to hold the result is capable of containing it.<\/p>\n<p>A workaround is to put\u00a0the constant\u00a0into a variable compatible with the result. Like<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nlong bigvalue;\r\nlong bye = 400;\r\nbigvalue\u00a0= 400*bye;\r\n<\/pre>\n<p>The compiler will use the larger data type if mixed data types are used in an arithmetic operation, and in the above case\u00a0produce the correct result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>long bigvalue; bigvalue\u00a0= 400*400; You&#8217;d expect\u00a0bigvalue to hold 16000 right? No. Apparently arithmetic operation are done using the data type of the operand.\u00a0So if\u00a0you do any arithmetic operation (+-*\/) that exceeds what\u00a0the\u00a0operand data\u00a0type can hold,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6,7],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-technical","tag-arduino","tag-code"],"_links":{"self":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":4,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}