{"id":866,"date":"2014-08-26T16:49:22","date_gmt":"2014-08-26T20:49:22","guid":{"rendered":"http:\/\/fll-freak.com\/blog\/?p=866"},"modified":"2025-04-21T00:54:35","modified_gmt":"2025-04-21T04:54:35","slug":"interesting-bugs","status":"publish","type":"post","link":"https:\/\/fll-freak.com\/blog\/?p=866","title":{"rendered":"Interesting bugs"},"content":{"rendered":"<p>Over the last few months, I have been sending to my software group at work a puzzle\/quiz in the form of a bug I have found in my robot code. They seem to get a kick out of especialy since I offer a Peppermint Patty as a prize to the first right guess.<\/p>\n<p>#1<\/p>\n<p>Over the weekend I spend a few frustrating hours tracking down a bug in this seemingly simple idiot proof code. All it does is take the absolute value of a long integer (32 bits on an ARM processor).\u00a0<\/p>\n<pre>long labs(long val)\r\n{\r\n\u00a0 \u00a0\u00a0long retval;\r\n\u00a0 \u00a0\u00a0if (val &lt; 0)\r\n\u00a0\u00a0\u00a0\u00a0{\r\n        retval = -val;\r\n    } else {\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0retval = val;\r\n\u00a0 \u00a0\u00a0}\r\n\u00a0 \u00a0\u00a0return retval;\r\n}<\/pre>\n<p>So most of the time this code does exactly what you might expect. It returns the absolute value of the parameter you pass in. All except in one case. Can you figure it out?<\/p>\n<p>#2<\/p>\n<p>What does this code do and why do it this way?<\/p>\n<pre>uint32_t val;\r\nval = (val &lt;&lt; 3) + (val &lt;&lt; 1);<\/pre>\n<p>#3<\/p>\n<p>What is the next larger number greater than 8,388,608.0 when using an IEEE float.\u00a0<\/p>\n<pre>a)\u00a0\u00a0\u00a0\u00a0\u00a0 8,388,608.002\r\nb)\u00a0\u00a0\u00a0\u00a0\u00a0 8,388,608.2\r\nc)\u00a0\u00a0\u00a0\u00a0\u00a0 8,388,609.0\r\nd)\u00a0\u00a0\u00a0\u00a0\u00a0 8,388,680.0<\/pre>\n<p>#4<\/p>\n<p>Over the weekend I was working on a section of code that would increase then decrease the speed of my robot every 6 seconds to check the stability of my PID control loop. My task was running at 5Hz and each call to GetTimeInMilliseconds returned a number in the sequence 0, 200, 400, 600, &#8230; Needless to say I had a bug in this code as it did not change the speed at the desired rate. What did I do wrong?<\/p>\n<pre>\u00a0\u00a0\u00a0 uint32_t systemTime;\r\n\u00a0\u00a0\u00a0 double \u00a0\u00a0speedSP;\r\n\r\n\u00a0\u00a0\u00a0 \/* Get the current time in milliseconds *\/\r\n\u00a0\u00a0\u00a0 systemTime = GetTimeInMilliseconds();\r\n\r\n\u00a0\u00a0\u00a0 \/* Alternate between fast and slow every n seconds *\/\r\n\u00a0\u00a0\u00a0 if\u00a0 ( (systemTime % 6*1000) == 0)\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/* The error is not in this comparison as 4.0 is exactly represented as a float *\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (speedSP == 4.0)\r\n      {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 speedSP = 2.0; \/* Set speed setpoint at 2 m\/s *\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 speedSP = 4.0; \/* Set speed setpoint at 4 m\/s *\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the last few months, I have been sending to my software group at work a puzzle\/quiz in the form of a bug I have found in my robot code. They seem to get a kick out of especialy since &hellip; <a href=\"https:\/\/fll-freak.com\/blog\/?p=866\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,14,12],"tags":[],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-avc","category-personal","category-software"],"_links":{"self":[{"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=866"}],"version-history":[{"count":1,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/866\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/866\/revisions\/867"}],"wp:attachment":[{"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fll-freak.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}