{"id":2580,"date":"2025-02-20T21:52:03","date_gmt":"2025-02-21T05:52:03","guid":{"rendered":"https:\/\/www.curlybrace.com\/words\/?p=2580"},"modified":"2025-02-20T21:52:03","modified_gmt":"2025-02-21T05:52:03","slug":"most-significant-digit-classic-vs-ai-suggestion","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2025\/02\/most-significant-digit-classic-vs-ai-suggestion\/","title":{"rendered":"Most Significant Digit:  Classic vs AI Suggestion"},"content":{"rendered":"<p>I was fiddling around with some toy code and needed to find the most significant digit of an integer.  I wondered if there was some more clever way to find it, like <a href=\"https:\/\/stackoverflow.com\/questions\/1349542\/john-carmacks-unusual-fast-inverse-square-root-quake-iii\">John Carmack&#8217;s fast inverse square root<\/a>.<\/p>\n<p>Here&#8217;s the classic method, using modulus arithmetic in a loop:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint msd(int num)\r\n{\r\n    if (num &lt; 0)\r\n        num *= -1;\r\n\r\n    while (num &gt;= 10)\r\n        num \/= 10;\r\n\r\n    return num;\r\n}\r\n<\/pre>\n<p>Calling this function a ridiculous number of times yielded an average total runtime of 270,589 microseconds.<\/p>\n<p>Here&#8217;s Google&#8217;s AI-suggested method:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint getMSD(int num) {\r\n    if (num == 0) {\r\n        return 0;\r\n    }\r\n\r\n    int digits = std::log10(std::abs(num)) + 1;\r\n    return num \/ static_cast(std::pow(10, digits - 1));\r\n}\r\n<\/pre>\n<p>The same ridiculous number of calls yielded an average total runtime of 2,241,092 microseconds.  That&#8217;s over eight times slower than the classic, modulus-loop method!<\/p>\n<p>Additionally, Google AI&#8217;s static_cast from double to int throws a C4244 warning (possible loss of data).  Finally, I believe that Google&#8217;s solution is fundamentally incorrect: it returns negative values for negative inputs. By my understanding of &#8220;most significant digit&#8221;, a digit is defined as the numbers zero through nine, thus any function returning a most significant digit <em>must<\/em> return a single, unsigned number 0..9.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was fiddling around with some toy code and needed to find the most significant digit of an integer. I wondered if there was some more clever way to find it, like John Carmack&#8217;s fast inverse square root. Here&#8217;s the &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2025\/02\/most-significant-digit-classic-vs-ai-suggestion\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2580","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/2580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=2580"}],"version-history":[{"count":9,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/2580\/revisions"}],"predecessor-version":[{"id":2601,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/2580\/revisions\/2601"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=2580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}