{"id":5511,"date":"2018-08-19T13:00:06","date_gmt":"2018-08-19T13:00:06","guid":{"rendered":"http:\/\/writeasync.net\/?p=5511"},"modified":"2018-08-15T15:48:09","modified_gmt":"2018-08-15T15:48:09","slug":"edge-cases-overflow","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=5511","title":{"rendered":"Edge cases: overflow"},"content":{"rendered":"<p>Consider this (rather suboptimal) <a href=\"https:\/\/en.wikipedia.org\/wiki\/Integer_factorization\">prime factorization<\/a> algorithm:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n    public static class MyMath\r\n    {\r\n        public static IEnumerable&lt;KeyValuePair&lt;int, int&gt;&gt; Factor(int n)\r\n        {\r\n            if (n &lt; 2)\r\n            {\r\n                yield break;\r\n            }\r\n\r\n            \/\/ Handle factors of 2 first\r\n            int p = 0;\r\n            while ((n &amp; 1) == 0)\r\n            {\r\n                n \/= 2;\r\n                ++p;\r\n            }\r\n\r\n            if (p != 0)\r\n            {\r\n                yield return new KeyValuePair&lt;int, int&gt;(2, p);\r\n            }\r\n\r\n            \/\/ Now check every odd number\r\n            for (int f = 3; f &lt;= n; f += 2)\r\n            {\r\n                p = 0;\r\n                int r = 0;\r\n                while (r == 0)\r\n                {\r\n                    int m = Math.DivRem(n, f, out r);\r\n                    if (r == 0)\r\n                    {\r\n                        n = m;\r\n                        ++p;\r\n                    }\r\n                }\r\n\r\n                if (p != 0)\r\n                {\r\n                    yield return new KeyValuePair&lt;int, int&gt;(f, p);\r\n                }\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>You can run it like so and see that it returns the proper factorization for a variety of <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.int32?view=netframework-4.7.1\">Int32<\/a> values:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n            Console.WriteLine(string.Join(';', MyMath.Factor(1)));\r\n            \/\/ Output: &lt;empty string&gt;\r\n\r\n            Console.WriteLine(string.Join(';', MyMath.Factor(1024)));\r\n            \/\/ Output: &#x5B;2, 10]\r\n\r\n            Console.WriteLine(string.Join(';', MyMath.Factor(555555)));\r\n            \/\/ Output: &#x5B;3, 1];&#x5B;5, 1];&#x5B;7, 1];&#x5B;11, 1];&#x5B;13, 1];&#x5B;37, 1]\r\n\r\n            Console.WriteLine(string.Join(';', MyMath.Factor(2048002048)));\r\n            \/\/ Output: &#x5B;2, 11];&#x5B;101, 1];&#x5B;9901, 1]\r\n<\/pre>\n<p>In fact, it works properly for any possible Int32 &#8212; <em>except<\/em> one very critical value. Can you <a href=\"https:\/\/blogs.msdn.microsoft.com\/ericlippert\/2003\/10\/07\/spot-the-defect\/\">spot the defect<\/a>?<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>The title of the post may have given it away, but the problem is in the <code>for<\/code> loop boundary. If <code>n == int.MaxValue<\/code>, the check <code>f <= n<\/code> will always succeed. Because of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Integer_overflow\">integer overflow<\/a> and the lucky coincidence that <a href=\"https:\/\/en.wikipedia.org\/wiki\/2,147,483,647\">2<sup>31<\/sup> - 1<\/a> (AKA <code>int.MaxValue<\/code>) is prime, <code>f<\/code> will be incremented beyond this value and wrap around to the negatives -- specifically, -2<sup>31<\/sup> + 1 since we increment by 2. From there, the loop will keep running, continuing to wrap around forever and ever.<\/p>\n<p>I had originally written this code to benchmark some simple factoring algorithms. While I was prescient enough to write a unit test for this particular case, I was still initially confused and surprised by its exceedingly long (in fact, infinite) running time.<\/p>\n<p>There are several possible fixes for this problem. Perhaps the best one here is to optimize the algorithm and change the upper bound to the square root of the number. (For example, when using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Trial_division\">trial division<\/a> to factor the number 1,000,003, it is not necessary to check any factor larger than 1,000 since the product 1001 x 1001 will exceed the original number.) While that happens to work in this particular algorithm, there may not be an easy way to avoid the MaxValue comparison in other situations.<\/p>\n<p>Another quick fix which works in most general cases is to use <code>long<\/code> (<code>Int64<\/code>) for the loop counter <code>f<\/code> instead of <code>int<\/code>. This pushes the overflow boundary far higher, at the cost of a few well-placed cast operations. If you are running this code on an x64 processor, it is highly likely that the <a href=\"https:\/\/www.telerik.com\/blogs\/understanding-net-just-in-time-compilation\">JIT compiler<\/a> is already using <a href=\"https:\/\/en.wikipedia.org\/wiki\/X86#64-bit\">64-bit registers<\/a> anyway so there should be no major performance impact.<\/p>\n<p>Moral of the story: <a href=\"http:\/\/kaner.com\/pdfs\/kanerPadmanabhanVISTACONDomainTesting.pdf\">boundary values<\/a> are still important; try passing <code>int.MaxValue<\/code> in your unit tests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider this (rather suboptimal) prime factorization algorithm: public static class MyMath { public static IEnumerable&lt;KeyValuePair&lt;int, int&gt;&gt; Factor(int n) { if (n &lt; 2) { yield break; } \/\/ Handle factors of 2 first int p = 0; while ((n &amp;&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-5511","post","type-post","status-publish","format-standard","hentry","category-testing"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5511"}],"version-history":[{"count":2,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5511\/revisions"}],"predecessor-version":[{"id":5513,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5511\/revisions\/5513"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5511"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}