{"id":139,"date":"2008-05-20T08:24:31","date_gmt":"2008-05-20T13:24:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/05\/20\/another-lesson-in-floating-point\/"},"modified":"2016-08-03T14:59:33","modified_gmt":"2016-08-03T19:59:33","slug":"another-lesson-in-floating-point","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/05\/20\/another-lesson-in-floating-point\/","title":{"rendered":"Another Lesson in Floating Point"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>In an <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/23\/a-glimpse-into-floating-point-accuracy\/\">earlier post<\/a> I discussed the issue of floating point accuracy in calculations.  I have another example to share today that has been lurking\r\n         in the <a href=\"https:\/\/www.mathworks.com\/products\/mapping\/\">Mapping Toolbox product<\/a>. The issue is with the function <tt>wrapTo180<\/tt>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Work-around<\/a><\/li>\r\n         <li><a href=\"#2\">Numerical Issue<\/a><\/li>\r\n         <li><a href=\"#3\">Test Case<\/a><\/li>\r\n         <li><a href=\"#7\">The Code<\/a><\/li>\r\n         <li><a href=\"#8\">Simple Fix<\/a><\/li>\r\n         <li><a href=\"#10\">Comments?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Work-around<a name=\"1\"><\/a><\/h3>\r\n   <p>There is a <a href=\"https:\/\/www.mathworks.com\/support\/bugreports\/details.html?rp=454686\">work-around<\/a> for those of you using Mapping Toolbox with releases R2007b and R2008a.\r\n   <\/p>\r\n   <h3>Numerical Issue<a name=\"2\"><\/a><\/h3>\r\n   <p>In R2007b and R2008a, the implementation for <tt>wrapTo180<\/tt> causes results from <tt>wrapTo180(lon)<\/tt> to differ very slightly from <tt>lon<\/tt> even for for certain values of <tt>lon<\/tt> in the interval <tt>[-180 180]<\/tt>. The differences are on the order of <tt>2*eps(lon)<\/tt>.\r\n   <\/p>\r\n   <h3>Test Case<a name=\"3\"><\/a><\/h3>\r\n   <p>The following should evaluate to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/true.html\"><tt>true<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">lon = 115.8323;<\/pre><p>but doesn't.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">isequal(lon,wrapTo180(lon))<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><p>These quantities aren't equal because of the arithmetic being done: <tt>115.8323 + 180 - 180<\/tt> differs from <tt>115.8323<\/tt> by\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">(lon + 180 - 180) - lon<\/pre><pre style=\"font-style:oblique\">ans =\r\n  2.8422e-014\r\n<\/pre><p>which happens to be<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">2*eps(lon)<\/pre><pre style=\"font-style:oblique\">ans =\r\n  2.8422e-014\r\n<\/pre><h3>The Code<a name=\"7\"><\/a><\/h3>\r\n   <p>Here's the code for <tt>wrapTo180<\/tt><\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">wrapTo180<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction lon = wrapTo180(lon)\r\n%wrapTo180 Wrap angle in degrees to [-180 180]\r\n%\r\n%   lonWrapped = wrapTo180(LON) wraps angles in LON, in degrees, to the\r\n%   interval [-180 180] such that 180 maps to 180 and -180 maps to -180.\r\n%   (In general, odd, positive multiples of 180 map to 180 and odd,\r\n%   negative multiples of 180 map to -180.)\r\n%\r\n%   See also wrapTo360, wrapTo2Pi, wrapToPi.\r\n\r\n% Copyright 2007 The MathWorks, Inc.\r\n% $Revision: 1.1.6.2 $  $Date: 2007\/08\/20 16:35:59 $\r\n\r\nlon = wrapTo360(lon + 180) - 180;\r\n\r\n<\/pre><h3>Simple Fix<a name=\"8\"><\/a><\/h3>\r\n   <p>We can just skip the arithmetic operation for values,like <tt>115.8323<\/tt>, that already <i>are<\/i> in the interval <tt>[-180 180]<\/tt>.  A nice way to do this is with <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/05\/14\/acting-on-specific-elements\/\">logical indexing<\/a> and we can replace the line of code with this.\r\n   <\/p><pre>q = (lon &lt; -180) | (180 &lt; lon);\r\nlon(q) = wrapTo360(lon(q) + 180) - 180;<\/pre><h3>Comments?<a name=\"10\"><\/a><\/h3>\r\n   <p>Have you ever had a similar issue, where a seemingly innocent code expression causes erroneous results?  How have you found\r\n      this out and solved it?  I'd love to hear your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=139#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_59ebdebc59f14cdda13d153a806c26a4() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='59ebdebc59f14cdda13d153a806c26a4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 59ebdebc59f14cdda13d153a806c26a4';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_59ebdebc59f14cdda13d153a806c26a4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.6<br><\/p>\r\n<\/div>\r\n<!--\r\n59ebdebc59f14cdda13d153a806c26a4 ##### SOURCE BEGIN #####\r\n%% Another Lesson in Floating Point\r\n% In an\r\n% <https:\/\/blogs.mathworks.com\/loren\/2006\/08\/23\/a-glimpse-into-floating-point-accuracy\/ earlier post> \r\n% I discussed the issue of floating point accuracy in calculations.  I have\r\n% another example to share today that has been lurking in the \r\n% <https:\/\/www.mathworks.com\/products\/mapping\/ Mapping Toolbox product>.\r\n% The issue is with the function\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/map\/wrapto180.html |wrapTo180|>.\r\n%\r\n%% Work-around\r\n% There is a \r\n% <https:\/\/www.mathworks.com\/support\/bugreports\/details.html?rp=454686 work-around>\r\n% for those of you using Mapping Toolbox with releases R2007b and R2008a.\r\n%% Numerical Issue\r\n% In R2007b and R2008a, the implementation for |wrapTo180| causes \r\n% results from |wrapTo180(lon)| to differ very slightly\r\n% from |lon| even for for certain values of |lon| in the interval \r\n% |[-180 180]|. The differences are on the order of |2*eps(lon)|.\r\n%% Test Case\r\n% The following should evaluate to \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/true.html |true|>.\r\nlon = 115.8323; \r\n%%\r\n% but doesn't.\r\nisequal(lon,wrapTo180(lon)) \r\n%%\r\n% These quantities aren't equal because of the arithmetic being done:\r\n% |115.8323 + 180 - 180| differs from |115.8323| by\r\n(lon + 180 - 180) - lon\r\n%%\r\n% which happens to be\r\n2*eps(lon)\r\n%% The Code\r\n% Here's the code for |wrapTo180|\r\ntype wrapTo180\r\n%% Simple Fix\r\n% We can just skip the arithmetic operation for values,like |115.8323|, that already\r\n% _are_ in the interval |[-180 180]|.  A nice way to do this is with \r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/05\/14\/acting-on-specific-elements\/ logical indexing>\r\n% and we can replace the line of code with this.\r\n%%\r\n%  q = (lon < -180) | (180 < lon); \r\n%  lon(q) = wrapTo360(lon(q) + 180) - 180;\r\n%% Comments?\r\n% Have you ever had a similar issue, where a seemingly innocent code\r\n% expression causes erroneous results?  How have you found this out and\r\n% solved it?  I'd love to hear your thoughts <https:\/\/blogs.mathworks.com\/loren\/?p=139#respond here>.\r\n##### SOURCE END ##### 59ebdebc59f14cdda13d153a806c26a4\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      In an earlier post I discussed the issue of floating point accuracy in calculations.  I have another example to share today that has been lurking\r\n         in the Mapping Toolbox... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/05\/20\/another-lesson-in-floating-point\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/139"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":1926,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/139\/revisions\/1926"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}