{"id":195,"date":"2009-08-13T18:05:26","date_gmt":"2009-08-13T18:05:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/13\/evens-and-odds\/"},"modified":"2018-01-08T15:16:23","modified_gmt":"2018-01-08T20:16:23","slug":"evens-and-odds","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/13\/evens-and-odds\/","title":{"rendered":"Evens and Odds"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are so many functions in MATLAB that sometimes users think that if they can't find the function they are thinking of,\r\n         it's because they don't know the name.  Sometimes that's true, and sometimes it's because there's a quick, easy way to get\r\n         the task done.  There are relatively frequent questions on the <a>MATLAB newsgroup<\/a> regarding how to determine if numbers are even or odd.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>The most frequent answers involve people pointing to functions such as <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/rem.html\"><tt>rem<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mod.html\"><tt>mod<\/tt><\/a>. However, on these same threads, you will frequently see <a href=\"\">Urs Schwarz<\/a> warn about the limitations of those functions for the purpose of determining even\/odd-ness.  He has made a contribution,\r\n      <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24278-isodd--a-pedestrian-parity-checker\"><tt>isodd<\/tt><\/a> on the File Exchange that encapsulates the algorithm he prefers, based on he functions <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/bitmax.html\"><tt>bitmax<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/bitand.html\"><tt>bitand<\/tt><\/a> The main issues Urs handles that aren't fully handled with solutions based on <tt>rem<\/tt> or <tt>mod<\/tt> include non-integer inputs and doubles larger than the largest floating point integer.\r\n   <\/p>\r\n   <p>If you do lots of error checking already, and know that your data is within acceptable bounds, you are probably fine using\r\n      an approach based on <tt>rem<\/tt> or <tt>mod<\/tt>.  Do you have applications where an enhanced version, such as the one Urs has posted, is important?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=195#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_cfe42517f6b84cc882b0e5a4009d9d52() {\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='cfe42517f6b84cc882b0e5a4009d9d52 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cfe42517f6b84cc882b0e5a4009d9d52';\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 2009 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_cfe42517f6b84cc882b0e5a4009d9d52()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\ncfe42517f6b84cc882b0e5a4009d9d52 ##### SOURCE BEGIN #####\r\n%% Evens and Odds\r\n% There are so many functions in MATLAB that sometimes users think that if\r\n% they can't find the function they are thinking of, it's because they\r\n% don't know the name.  Sometimes that's true, and sometimes it's because\r\n% there's a quick, easy way to get the task done.  There are relatively\r\n% frequent questions on the\r\n% <http:\/\/view_thread\/258257 MATLAB newsgroup>\r\n% regarding how to determine if numbers are even or odd.\r\n%%\r\n% The most frequent answers involve people pointing to functions such as\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/rem.html |rem|> \r\n% and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mod.html |mod|>. \r\n% However, on these same threads, you will frequently see\r\n% < Urs Schwarz>\r\n% warn about the limitations of those functions for the purpose of\r\n% determining even\/odd-ness.  He has made a contribution,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24278-isodd--a-pedestrian-parity-checker |isodd|>\r\n% on the File Exchange that encapsulates the algorithm he prefers, based on\r\n% he functions\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/bitmax.html |bitmax|>\r\n% and <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/bitand.html |bitand|>\r\n% The main issues Urs handles that aren't fully handled with solutions\r\n% based on |rem| or |mod| include non-integer inputs and doubles larger\r\n% than the largest floating point integer.  \r\n%%\r\n% If you do lots of error checking already, and know that your data is\r\n% within acceptable bounds, you are probably fine using an approach based\r\n% on |rem| or |mod|.  Do you have applications where an enhanced version, such as\r\n% the one Urs has posted, is important?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=195#respond here>.\r\n\r\n\r\n##### SOURCE END ##### cfe42517f6b84cc882b0e5a4009d9d52\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are so many functions in MATLAB that sometimes users think that if they can't find the function they are thinking of,\r\n         it's because they don't know the name.  Sometimes... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/13\/evens-and-odds\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,26,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/195"}],"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=195"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":2556,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/195\/revisions\/2556"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}