{"id":792,"date":"2011-10-12T14:02:45","date_gmt":"2011-10-12T18:02:45","guid":{"rendered":"http:\/\/pappp.net\/?p=792"},"modified":"2011-10-12T14:02:45","modified_gmt":"2011-10-12T18:02:45","slug":"arduino-promotion-behavior","status":"publish","type":"post","link":"https:\/\/pappp.net\/?p=792","title":{"rendered":"Arduino Promotion Behavior"},"content":{"rendered":"<p>I was pulling up an old project (A little Simon game which has apparently fallen off the &#8216;net when I moved my site- will have to repost) to use as a classroom demonstration, and discovered that sometime in the last couple years, the int->unsigned long promotion in Ardino\/Processing broke\/changed without comment.  <\/p>\n<p>The code uses using primitive nested counters and delays to generate sounds and control difficulty, which means rather large numbers (100-microsecond scale delays running for seconds) are being thrown about.  To isolate the change, I wrote the following test:<br \/>\n<!--more--><br \/>\n<code>int constint = 500;<\/p>\n<p>void setup()<br \/>\n{<br \/>\n  Serial.begin(9600);<br \/>\n}<\/p>\n<p>void loop()<br \/>\n{<br \/>\n  testpromote(constint);<br \/>\n  testpromote2(constint);<br \/>\n  delay(1000);<br \/>\n}<\/p>\n<p>void testpromote(int input)<br \/>\n{<br \/>\n  Serial.print(\"Multiplying:\");<br \/>\n  Serial.print(input);<br \/>\n  Serial.println(\"*1000\");<br \/>\n  unsigned long result;<br \/>\n  result=input*1000;<br \/>\n  Serial.print(\"Result:\");<br \/>\n  Serial.println(result);<br \/>\n}<\/p>\n<p>void testpromote2(int input)<br \/>\n{<br \/>\n  Serial.print(\"Multiplying:\");<br \/>\n  Serial.print(input);<br \/>\n  Serial.println(\"*1000L\");<br \/>\n  unsigned long result;<br \/>\n  result=input*1000L;<br \/>\n  Serial.print(\"Result:\");<br \/>\n  Serial.println(result);<br \/>\n}<\/code><\/p>\n<p>Which generates this output:<br \/>\n<code><br \/>\nMultiplying:500*1000<br \/>\nResult:4294943008<br \/>\nMultiplying:500*1000L<br \/>\nResult:500000<br \/>\n<\/code><br \/>\nNote that ul=int*int produces something *distinctly wrong*, while ul=long*int produces a valid result. Easy to work around, but somewhat alarming. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was pulling up an old project (A little Simon game which has apparently fallen off the &#8216;net when I moved my site- will have to repost) to use as a classroom demonstration, and discovered that sometime in the last &hellip; <a href=\"https:\/\/pappp.net\/?p=792\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,37,1,10],"tags":[168,95],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-computers","category-diy","category-electronics","category-general","category-objects","tag-arduino","tag-programming"],"_links":{"self":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=792"}],"version-history":[{"count":0,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions"}],"wp:attachment":[{"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}