{"id":6784,"date":"2021-03-02T07:23:23","date_gmt":"2021-03-02T12:23:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=6784"},"modified":"2021-03-02T07:23:23","modified_gmt":"2021-03-02T12:23:23","slug":"round-with-tie-breakers-round-two","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2021\/03\/02\/round-with-tie-breakers-round-two\/","title":{"rendered":"Round, With Tie Breakers, Round Two"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>I published <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2021\/02\/25\/round-with-ties-to-even\/\">Round, With Ties to Even<\/a> a couple of days ago. Steve Eddins and Daniel Dolan immediately had substantive comments. Here is my reaction to their comments.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#7365c624-e928-4045-a1b4-209bcdc91f74\">Two More<\/a><\/li><li><a href=\"#fdeaaa3a-2ff3-4471-8142-0985473e5785\">Sample<\/a><\/li><li><a href=\"#67773591-b7e1-4df9-968f-58fc6ca90c15\">Scaling<\/a><\/li><li><a href=\"#bbea3ac5-e51d-4dde-9895-f66f824f21d7\"><tt>round<\/tt><\/a><\/li><\/ul><\/div><h4>Two More<a name=\"7365c624-e928-4045-a1b4-209bcdc91f74\"><\/a><\/h4><p>The four choices that I described for how to break ties -- 'even', 'odd', 'up' and 'down' -- all have the same behavior for positive and negative numbers.  The current built-in <tt>round<\/tt> uses the 'up\" rule.<\/p><p>Steve sees the need for two more choices that are sensitive to the sign of the number. I will call them 'plus' and 'minus\".  They round to the right and to the left on the number line.  That's sometimes known as round toward plus infinity and toward minus infinity.<\/p><h4>Sample<a name=\"fdeaaa3a-2ff3-4471-8142-0985473e5785\"><\/a><\/h4><p>Here's a chart of a few values.<\/p><pre class=\"codeinput\">    x = (-4.5:1:4.5)';\r\n    xRound(x);\r\n<\/pre><pre class=\"codeoutput\">\r\n     x     round  up  down   even   odd  plus minus\r\n\r\n  -4.500    -5    -5    -4    -4    -5    -4    -5\r\n  -3.500    -4    -4    -3    -4    -3    -3    -4\r\n  -2.500    -3    -3    -2    -2    -3    -2    -3\r\n  -1.500    -2    -2    -1    -2    -1    -1    -2\r\n  -0.500    -1    -1    -0    -0    -1    -0    -1\r\n   0.500     1     1     0     0     1     1     0\r\n   1.500     2     2     1     2     1     2     1\r\n   2.500     3     3     2     2     3     3     2\r\n   3.500     4     4     3     4     3     4     3\r\n   4.500     5     5     4     4     5     5     4\r\n<\/pre><p>And the plot.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/rounds2.png\" alt=\"\"> <\/p><h4>Scaling<a name=\"67773591-b7e1-4df9-968f-58fc6ca90c15\"><\/a><\/h4><p>The current built-in <tt>round<\/tt> also includes the possibility of scaling by a power of 10 to facilitate rounding to, say, the nearest multiple of 1000, or of 1\/1000, or three significant figures. Daniel was concerned that having both scaling and tie-breaking options in the same function would be confusing.<\/p><p>But, I think it is OK, because:<\/p><div><ul><li><tt>round(x,'even')<\/tt>, the parameter <tt>'even'<\/tt> is a <tt>char<\/tt>.<\/li><li><tt>round(x,3)<\/tt>, the parameter <tt>3<\/tt> is numeric.<\/li><li><tt>round(x,3,'significant')<\/tt>, the parameter <tt>'significant'<\/tt> is a specific   <tt>char<\/tt>.<\/li><\/ul><\/div><p>So, the statements<\/p><div><ul><li><tt>round(x,'even',3)<\/tt><\/li><li><tt>round(x,3,'even')<\/tt><\/li><li><tt>round(x,'even',3,'significant')<\/tt><\/li><\/ul><\/div><p>are unambiguous and distinguishable.  After <tt>x<\/tt>, the other parameters can be given in any order.<\/p><h4><tt>round<\/tt><a name=\"bbea3ac5-e51d-4dde-9895-f66f824f21d7\"><\/a><\/h4><p>Here is my proposal for the enhancement request about <tt>round<\/tt>. It is available <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/Round.m\">here<\/a>.<\/p><pre class=\"codeinput\">   type <span class=\"string\">round.m<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction r = round(varargin)\r\n% r = round(x) scales and rounds the elements of x to the nearest integers.\r\n% Default: ties, elements halfway between integers, are rounded away from zero.\r\n%\r\n% r = round(x,'even') ties round to even integers.\r\n% r = round(x,'odd') ties round to odd integers.\r\n% r = round(x,'up') ties round away from zero (same as default).\r\n% r = round(x,'down') ties round towards zero.\r\n% r = round(x,'plus') ties round to the right on the number line.\r\n% r = round(x,'minus') ties round to the left on the number line.\r\n%\r\n% r = round(x,n), n &gt;= 0, round(10^n*x)\/10^n, round(12.3456,2) = 12.3500        \r\n% r = round(x,-n), n &gt; 0, 10^n*round(x\/10^n), round(1234.56,-2) = 1200.\r\n% r = round(x,n,'significant'), round(.001234,2,'significant') = .0012\r\n% r = round(x,n,'decimals) same as round(x,n).\r\n%\r\n% r = round(x,...), ties, n, 'significant' and 'decimals' can be in any order.\r\n%\r\n% Use Round(...) with capital R to distinguish from built-in round(...).\r\n\r\n    [x,n,ties] = parse_input(varargin{:});\r\n    x = prescale(x,n);\r\n    \r\n    a = abs(x) + 0.5;\r\n    r = floor(a);\r\n    switch ties\r\n       case 'even'\r\n           m = (r == a) &amp; (mod(r,2) == 1); \r\n       case 'odd'\r\n           m = (r == a) &amp; (mod(r,2) == 0);\r\n       case 'down'\r\n           m = (r == a);\r\n       case 'up'\r\n           m = [];\r\n       case 'plus'\r\n           m = (x &lt; 0) &amp; (r == a);\r\n       case 'minus'\r\n           m = (x &gt; 0) &amp; (r == a);\r\n       otherwise\r\n           error(['''' ties ''' not recognized.'])\r\n    end\r\n    r(m) = r(m) - 1;\r\n    r = sign(x).*r;\r\n\r\n    r = postscale(r,n);\r\n   \r\n    % ----------------------------------------------\r\n   \r\n    function [x,n,ties] = parse_input(varargin)\r\n        x = varargin{1};\r\n        n = zeros(size(x));\r\n        ties = 'up';\r\n        for k = 2:nargin\r\n            if isnumeric(varargin{k})\r\n                n(:) = varargin{k};\r\n            elseif strcmp(varargin{k},'significant')\r\n                n(:) = n(:) - ceil(log10(abs(x(:))));\r\n            elseif strcmp(varargin{k},'decimals')\r\n                % ignore\r\n            else\r\n                ties = varargin{k};\r\n            end\r\n        end\r\n    end\r\n   \r\n    function x = prescale(x,n)\r\n        if any(n ~= 0)\r\n            k = n &gt; 0;\r\n            x(k) = 10.^n(k).*x(k);\r\n            k = n &lt; 0;\r\n            x(k) = x(k).\/10.^(-n(k));\r\n        end\r\n    end\r\n \r\n    function r = postscale(r,n)\r\n        if any(n ~= 0)\r\n            k = n &gt; 0;\r\n            r(k) = r(k).\/10.^n(k);\r\n            k = n &lt; 0;\r\n            r(k) = 10.^(-n(k)).*r(k);\r\n        end\r\n    end\r\nend\r\n<\/pre><script language=\"JavaScript\"> <!-- \r\n    function grabCode_340ec9d7fad04baeb282a1f5fcab07ea() {\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='340ec9d7fad04baeb282a1f5fcab07ea ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 340ec9d7fad04baeb282a1f5fcab07ea';\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        copyright = 'Copyright 2021 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_340ec9d7fad04baeb282a1f5fcab07ea()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2021a<br><\/p><\/div><!--\r\n340ec9d7fad04baeb282a1f5fcab07ea ##### SOURCE BEGIN #####\r\n%% Round, With Tie Breakers, Round Two\r\n% I published\r\n% <https:\/\/blogs.mathworks.com\/cleve\/2021\/02\/25\/round-with-ties-to-even\/\r\n% Round, With Ties to Even> a couple of days ago.\r\n% Steve Eddins and Daniel Dolan immediately had substantive comments.\r\n% Here is my reaction to their comments.\r\n\r\n%% Two More\r\n% The four choices that I described for how to break ties REPLACE_WITH_DASH_DASH 'even',\r\n% 'odd', 'up' and 'down' REPLACE_WITH_DASH_DASH all have the same behavior for positive and\r\n% negative numbers.  The current built-in |round| uses the 'up\" rule.\r\n%\r\n% Steve sees the need for two more choices that\r\n% are sensitive to the sign of the number.\r\n% I will call them 'plus' and 'minus\".  They round to the right and\r\n% to the left on the number line.  That's sometimes known as round\r\n% toward plus infinity and toward minus infinity.\r\n\r\n%% Sample\r\n% Here's a chart of a few values.\r\n    \r\n    x = (-4.5:1:4.5)';\r\n    xRound(x);\r\n    \r\n%%\r\n% And the plot.\r\n%\r\n% <<rounds2.png>>\r\n\r\n%% Scaling\r\n% The current built-in |round| also includes the possibility of\r\n% scaling by a power of 10 to facilitate rounding to, say, the nearest\r\n% multiple of 1000, or of 1\/1000, or three significant figures.\r\n% Daniel was concerned that having both scaling and tie-breaking options\r\n% in the same function would be confusing.\r\n%\r\n% But, I think it is OK, because:\r\n%\r\n% * |round(x,'even')|, the parameter |'even'| is a |char|.\r\n% * |round(x,3)|, the parameter |3| is numeric.\r\n% * |round(x,3,'significant')|, the parameter |'significant'| is a specific\r\n%   |char|.\r\n%\r\n% So, the statements\r\n%\r\n% * |round(x,'even',3)|\r\n% * |round(x,3,'even')|\r\n% * |round(x,'even',3,'significant')|\r\n%\r\n% are unambiguous and distinguishable.  After |x|, the other parameters\r\n% can be given in any order.\r\n\r\n%% |round|\r\n% Here is my proposal for the enhancement request about |round|.\r\n% It is available <https:\/\/blogs.mathworks.com\/cleve\/files\/Round.m\r\n% here>.\r\n\r\n   type round.m\r\n\r\n   \r\n\r\n    \r\n\r\n##### SOURCE END ##### 340ec9d7fad04baeb282a1f5fcab07ea\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/rounds2.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>I published <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2021\/02\/25\/round-with-ties-to-even\/\">Round, With Ties to Even<\/a> a couple of days ago. Steve Eddins and Daniel Dolan immediately had substantive comments. Here is my reaction to their comments.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2021\/03\/02\/round-with-tie-breakers-round-two\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,7,37],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/6784"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=6784"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/6784\/revisions"}],"predecessor-version":[{"id":6786,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/6784\/revisions\/6786"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=6784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=6784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=6784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}