{"id":185,"date":"2008-02-13T16:30:29","date_gmt":"2008-02-14T00:30:29","guid":{"rendered":"http:\/\/www.nomadicminds.org\/blogs\/2008\/02\/13\/floating-point-arithmetic\/"},"modified":"2008-02-10T16:30:49","modified_gmt":"2008-02-11T00:30:49","slug":"floating-point-arithmetic","status":"publish","type":"post","link":"http:\/\/www.nomadicminds.org\/blogs\/2008\/02\/floating-point-arithmetic\/","title":{"rendered":"Floating Point Arithmetic"},"content":{"rendered":"<p>I started my software engineer career constructing an Mechanical CAD software.  One day, my tangential algorithm yielded a line that does not even touch the curve it is supposed to be tangent to.  The graphics on the screen is off by several pixels.  I was miffed.  Checked and re-checked.  It was not a graphics problem, my algorithm was correct, and the implementation was not buggy.  That stumped me for a few days until I learned about floating point round-off errors.  With just a few lines of code changed, the line snapped neatly onto the curve.  That was the 1st time I learned the difference between real numbers and computerized floating point numbers.<\/p>\n<p>Then I worked for Sun and met this guy (forgot his name) who was on the IEEE standard committee.  All he talked about are Fortran, <em>Inf<\/em>, <em>NaN<\/em>, floating point exceptions, and those things I had no clue about.  Good thing I was young and he was patient.<\/p>\n<p>In pretty much all computers these days, floating point arithmetic are not precise.  If a programmer is not careful, a surprising large error can happen from simple operations.  Every programmer should really read the famous paper: <em><a href=\"http:\/\/docs.sun.com\/app\/docs\/doc\/800-7895\/6hos0aou4?a=view\">What Every Computer Scientist Should Know About Floating-Point Arithmetic<\/a><\/em>.  In addition to floating point rounding off, novice programmers frequently trip on integer overflow (or underflow).  Simply put, if you put two integers together and the sum is greater than what a computer can hold, it simply throws away the excessive bits and leave you with a result that can be very surprising.  (Try adding 2,000,000,000 and another 2,000,000,000 to an &#8220;int32&#8221; typed integer variable.  Guess what&#8217;s the answer before running the program.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started my software engineer career constructing an Mechanical CAD software. One day, my tangential algorithm yielded a line that does not even touch the curve it is supposed to be tangent to. The graphics on the screen is off &hellip; <a href=\"http:\/\/www.nomadicminds.org\/blogs\/2008\/02\/floating-point-arithmetic\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/posts\/185"}],"collection":[{"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nomadicminds.org\/blogs\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}