{"id":5380,"date":"2014-02-28T17:47:28","date_gmt":"2014-02-28T16:47:28","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5380"},"modified":"2014-02-28T17:47:28","modified_gmt":"2014-02-28T16:47:28","slug":"floating-point-addition-is-not-associative","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5380","title":{"rendered":"Floating point addition is not associative"},"content":{"rendered":"<p>A lot of people don&#8217;t seem to know this&#8230;.and they should. When working with floating point arithmetic, it is not necessarily true that a+(b+c) = (a+b)+c. Here is a demo using MATLAB<\/p>\n<pre>&gt;&gt; x=0.1+(0.2+0.3);\r\n&gt;&gt; y=(0.1+0.2)+0.3;\r\n&gt;&gt; % are they equal?\r\n&gt;&gt; x==y\r\n\r\nans =\r\n     0\r\n\r\n&gt;&gt; % lets look\r\n&gt;&gt; sprintf('%.17f',x)\r\nans =\r\n0.59999999999999998\r\n\r\n&gt;&gt; sprintf('%.17f',y)\r\nans =\r\n0.60000000000000009<\/pre>\n<p>These results have nothing to do with the fact that I am using MATLAB. Here&#8217;s the same thing in Python<\/p>\n<pre>&gt;&gt;&gt; x=(0.1+0.2)+0.3\r\n&gt;&gt;&gt; y=0.1+(0.2+0.3)\r\n&gt;&gt;&gt; x==y\r\nFalse\r\n&gt;&gt;&gt; print('%.17f' %x)\r\n0.60000000000000009\r\n&gt;&gt;&gt; print('%.17f' %y)\r\n0.59999999999999998<\/pre>\n<p>If this upsets you, or if you don&#8217;t understand why, I suggest you read the following<\/p>\n<ul>\n<li><a href=\"http:\/\/docs.oracle.com\/cd\/E19957-01\/806-3568\/ncg_goldberg.html\">What Every Computer Scientist Should Know About Floating-Point Arithmeti<\/a><a href=\"http:\/\/docs.oracle.com\/cd\/E19957-01\/806-3568\/ncg_goldberg.html\">c<\/a><\/li>\n<\/ul>\n<p>Does anyone else out there have suggestions for similar resources on this topic?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of people don&#8217;t seem to know this&#8230;.and they should. When working with floating point arithmetic, it is not necessarily true that a+(b+c) = (a+b)+c. Here is a demo using MATLAB &gt;&gt; x=0.1+(0.2+0.3); &gt;&gt; y=(0.1+0.2)+0.3; &gt;&gt; % are they equal? &gt;&gt; x==y ans = 0 &gt;&gt; % lets look &gt;&gt; sprintf(&#8216;%.17f&#8217;,x) ans = 0.59999999999999998 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,11,76,7,31],"tags":[77],"class_list":["post-5380","post","type-post","status-publish","format-standard","hentry","category-general-math","category-matlab","category-numerics","category-programming","category-python","tag-nume"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1oM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5380"}],"version-history":[{"count":3,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5380\/revisions"}],"predecessor-version":[{"id":5390,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5380\/revisions\/5390"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}