{"id":76,"date":"2010-02-16T23:07:03","date_gmt":"2010-02-17T04:07:03","guid":{"rendered":"http:\/\/blog.bitsofgenius.com\/?p=76"},"modified":"2011-01-26T12:36:40","modified_gmt":"2011-01-26T16:36:40","slug":"old-rounding-errors-never-die-they-just-fade-into-images","status":"publish","type":"post","link":"https:\/\/blog.bitsofgenius.com\/?p=76","title":{"rendered":"Old rounding errors never die, they just fade into images&#8230;"},"content":{"rendered":"<p>My friend Matt Owen posted a snapshot of the stock results on his Facebook wall today, which showed some interesting results on NASDAQ.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"(source unknown)\" src=\"\/images\/FloatingPointError.jpg\" alt=\"(source unknown)\" width=\"344\" height=\"204\" \/><\/p>\n<p>It&#8217;s amusing to see little math slip ups appear from time to time like this on commercial web sites.\u00a0 Seeing this error reminded me of other experiences I have had in coding where numbers would throw wildly inaccurate answers every 3rd blue moon or so.<\/p>\n<p>Since I&#8217;ve experienced this type of error before, I was thinking to myself, &#8220;Wow, that floating point rounding error is still around !&#8221;\u00a0 It&#8217;s probably not the problem with these invalid values, but you might be asking yourself &#8220;what&#8217;s a floating point rounding error?&#8221;\u00a0 Not everyone who has learned coding is aware of it, and it&#8217;s more likely to affect calculations requiring precise levels of resolution, rather than applications written for everyday use.<\/p>\n<p>Nonetheless, it&#8217;s a good thing to be aware of.\u00a0 Back in my junior high school years, our math teacher used the following code in BASIC to demonstrate the problem:<\/p>\n<pre lang=\"vb\" line=\"1\">\r\nX = 0.0\r\nFor I = 1 To 1000\r\nX = X + 0.1\r\nNext I\r\nPrint X\r\n<\/pre>\n<p>The code&#8217;s purpose is simple: start at 0, and add 1\/10th to it for 1,000 times.\u00a0 The expected result is the same as 0.1 * 1,000&#8230; or 100.<\/p>\n<p>Since X is not type cast, the parser would find a suitable default floating-point type for X due to the decimal point in the number.\u00a0 Depending on the computer it ran on, it would produce slightly different results, but never the exact 100.00 which is the correct result.\u00a0 On an HP2000C mainframe, the answer would be 99.996, while on a Radio Shack TRS-80 the answer was 100.02.<\/p>\n<p>So out of curiosity, I had to see what more modern languages like VBScript and C# would produce.\u00a0 Sure enough, neither produce the expected result of 100.<\/p>\n<pre lang=\"vb\" line=\"1\">\r\nx = cdbl (0.0)\r\nfor i = 1 to 1000\r\nx = x + cdbl(0.1)\r\nnext\r\nWScript.Echo x\r\n\r\ny = csng (0.0)\r\nfor i = 1 to 1000\r\ny = y + csng(0.1)\r\nnext\r\nWScript.Echo y\r\n\r\nz = 0.0\r\nfor i = 1 to 1000\r\nz = z + 0.1\r\nnext\r\nWScript.Echo z\r\n<\/pre>\n<p>The result is:<br \/>\n99.9999999999986<br \/>\n99.99905<br \/>\n99.9999999999986 (uses a default cast of double)<\/p>\n<p>In CSharp, create a windows form with a label and a button, and insert this code at the button click event:<\/p>\n<pre lang=\"csharp\" line=\"1\" escaped=\"true\">\r\ndouble total = 0.0;\r\nfor (int i = 1; i &lt;= 1000; i++) total += 0.1;\r\nthis.label1.Text = total.ToString();\r\n<\/pre>\n<p>The result is: 99.9999999999986<\/p>\n<p>The problem is widely known and well-documented.  Still, I like the way my high school math teacher put it.\u00a0 This is a card to put in your pocket, and play when you encounter the person who swears up and down that math is precise and computers are perfect.\u00a0 You can even use it on a person who laughs at the occasional &#8220;numbers gone wild&#8221; event in their portal&#8230; as in the stock result above. \u00a0 \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My friend Matt Owen posted a snapshot of the stock results on his Facebook wall today, which showed some interesting results on NASDAQ. It&#8217;s amusing to see little math slip ups appear from time to time like this on commercial web sites.\u00a0 Seeing this error reminded me of other experiences I have had in coding [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,14,15,16,23,13,17,27],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-code-bloopers","category-dotnetclasslibraries","category-dotnetconsole","category-dotnettechniques","category-dotnetwinforms","category-technologythoughts","category-tips-and-tricks","category-visual-studio"],"_links":{"self":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76"}],"version-history":[{"count":3,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}