{"id":1104,"date":"2014-05-15T14:23:06","date_gmt":"2014-05-15T21:23:06","guid":{"rendered":"http:\/\/10kdev.net\/?p=1104"},"modified":"2014-05-15T14:24:51","modified_gmt":"2014-05-15T21:24:51","slug":"1286608618","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1104","title":{"rendered":"1286608618"},"content":{"rendered":"<p>The title is the remainder for evaluating the numerical sequence 9876543210 as an Integer in Java. \u00a0This number lies outsides the bounds of Integer.MAX_VALUE for Java; it&#8217;s an overflow error.<\/p>\n<p style=\"padding-left: 30px;\"><strong>1286608618 is the overflow remainder for casting 9876543210 as an Integer in Java.<\/strong><\/p>\n<p>I ran across this error in some code I was working on that had credit card type account numbers; which are generally 16 digit numbers. \u00a0The program in question got the *same* account number from one table as a string, another database as an Integer, and a third table as a BigDecimal. \u00a0In the code there were also methods that did conversions \u00a0of this number to long and to int. \u00a0Normally values like this are stored as strings but <em>I have no comment about the design of this software and database<\/em>\u00a0<em>where such things could occur<\/em> . . .<\/p>\n<p>When the value 1286608618 came back as a value during a unit test, I thought it odd and instead of just taking it as incorrect I researched it. \u00a0Since a lot of people use 9876543210 as a test value, perhaps a lot of people have seen this.<\/p>\n<p>There are a few interesting things about this error:<\/p>\n<ol>\n<li>Java doesn&#8217;t complain when the bad number gets assigned to Integer.<\/li>\n<li>Java just mindlessly returns the overflow value with no error.<\/li>\n<li>Java makes a few trips through its Integer range until it gets a number that fits within the range. \u00a0In the case of 9876543210, it is over 2 times the value of the range, to Java makes those trips through to get to\u00a01286608618.<\/li>\n<\/ol>\n<p>I wrote a little Groovy code to play around with this:<\/p>\n<pre><span style=\"color: #0000ff;\"><code><span style=\"color: #008000;\">\/\/Here's the integer and it's overflow<\/span>\r\nInteger i = 9876543210\r\nprintln \"overflow error for 9876543210: \" + i\r\n\r\n<span style=\"color: #008000;\">\/\/Integer max value<\/span>\r\nBigInteger maxInt = 2**31 - 1\r\nprintln \"calculated max integer: \\t\" + maxInt\r\nprintln \"Integer.MAX_VALUE: \\t\\t\" + Integer.MAX_VALUE\r\n\r\n<span style=\"color: #008000;\">\/\/Integer min value<\/span>\r\nBigInteger minInt = -2**31\r\nprintln \"calculated min integer: \\t\" + minInt\r\nprintln \"Integer.MIN_VALUE: \\t\\t\" + Integer.MIN_VALUE\r\n\r\n<span style=\"color: #008000;\">\/\/get the Integer range<\/span>\r\nBigInteger range = maxInt  + 1 - minInt\r\nprintln \"range: \\t\\t\\t\" + range\r\n\r\n<span style=\"color: #008000;\">\/\/two trips around the raspberry bush calculation<\/span>\r\nBigInteger big = 9876543210\r\nprintln \"big: \\t\\t\\t\" + big\r\nprintln \"overflow remainder: \\t\\t\" + (big - (2*range))\r\n<\/code><\/span><\/pre>\n<p style=\"text-align: left;\">Here&#8217;s the outcome:<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2014\/05\/318485eba600b91da3c28cb036782e91.png\" width=\"312\" height=\"134\" \/><\/p>\n<p>Well see here, I know thinking about this bakes my bean a little bit too. \u00a0There are number shifts and all kinds of thing going on. If you have <em>underflow <\/em>the same kind of logic applies as well.<\/p>\n<p>I don&#8217;t understand why it just doesn&#8217;t error &#8212; what use is that remainder number at all? \u00a0Have no idea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The title is the remainder for evaluating the numerical sequence 9876543210 as an Integer in Java. \u00a0This number lies outsides the bounds of Integer.MAX_VALUE for Java; it&#8217;s an overflow error. 1286608618 is the overflow remainder for casting 9876543210 as an Integer in Java. I ran across this error in some code I was working on [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1104"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1104"}],"version-history":[{"count":7,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions"}],"predecessor-version":[{"id":1111,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions\/1111"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}