{"id":225,"date":"2010-04-01T11:18:14","date_gmt":"2010-04-01T11:18:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/01\/double-or-nothing-no-joking\/"},"modified":"2010-04-01T12:08:01","modified_gmt":"2010-04-01T12:08:01","slug":"double-or-nothing-no-joking","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/01\/double-or-nothing-no-joking\/","title":{"rendered":"Double or Nothing?  No Joking!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Note the date, folks.  I am using this excuse to be a bit light-hearted today.  I saw the following quote on someone's door\r\n         here at MathWorks.\r\n      <\/p>\r\n      <p>\r\n         <blockquote>\r\n         MATLAB gives you the benefit of the double.\r\n         <\/blockquote>\r\n         \r\n      <\/p>\r\n      <p>This made me smile and I realized that there are some ways in which MATLAB uses doubles that might not always be obvious to\r\n         users.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What Type is X?<\/a><\/li>\r\n         <li><a href=\"#5\">Other Integers<\/a><\/li>\r\n         <li><a href=\"#6\">Display the Values Stored in Various Types<\/a><\/li>\r\n         <li><a href=\"#10\">Some Behaviors<\/a><\/li>\r\n         <li><a href=\"#12\">MATLAB Types<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What Type is X?<a name=\"1\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = 17<\/pre><pre style=\"font-style:oblique\">X =\r\n    17\r\n<\/pre><p>When the value of <tt>X<\/tt> is displayed, as <tt>17<\/tt>, there is no decimal point or other indication that <tt>X<\/tt> is not strictly an integer.  Despite that, <tt>X<\/tt> is a double precision variable.  I have at least a couple of ways to find this information programmatically.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos<\/pre><pre style=\"font-style:oblique\">  Name      Size            Bytes  Class     Attributes\r\n\r\n  X         1x1                 8  double              \r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">class(X)<\/pre><pre style=\"font-style:oblique\">ans =\r\ndouble\r\n<\/pre><p>By default, MATLAB variables are double precision arrays.  In the olden days, these were the <i>only<\/i> kind of arrays in MATLAB.  Back then even character arrays were stored as double values.\r\n   <\/p>\r\n   <h3>Other Integers<a name=\"5\"><\/a><\/h3>\r\n   <p>There are other ways to store integers in MATLAB these days.  Let me list a few:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>characters: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/char.html\"><tt>char<\/tt><\/a><\/li>\r\n         <li>signed integers, e.g., <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/int8.html\"><tt>int8<\/tt><\/a><\/li>\r\n         <li>unsigned integers, e.g., <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/uint8.html\"><tt>uint8<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Display the Values Stored in Various Types<a name=\"6\"><\/a><\/h3>\r\n   <p>First I'll convert the double to a string and display it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">str17 = num2str(X)<\/pre><pre style=\"font-style:oblique\">str17 =\r\n17\r\n<\/pre><p>Notice that the display looks different than that for <tt>X<\/tt>.  There is no leading white space for character arrays (unless it's part of the string itself).\r\n   <\/p>\r\n   <p>Now let's look at the display for some integer types.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">u8 = uint8(X)\r\ni8 = int8(X)<\/pre><pre style=\"font-style:oblique\">u8 =\r\n   17\r\ni8 =\r\n   17\r\n<\/pre><p>Notice here that you can't distinguish the <b>type<\/b> based on the display of the values.  The default numeric display in MATLAB tries to display the maximum amount of information\r\n      compactly.\r\n   <\/p>\r\n   <h3>Some Behaviors<a name=\"10\"><\/a><\/h3>\r\n   <p>Because the default type is double, we have found it really convenient to allow some interaction between double scalar values\r\n      and other numeric array types.  The way I like to state the behavior is that, for arithmetic operators, MATLAB does the calculations\r\n      <i>as if the arrays are doubles<\/i>, and then respects the memory savings of the non-double storage class.  What this means, for example, is that I can take\r\n      a grayscale image and halve the values like so, causing the new image to be darker.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">p = imread(<span style=\"color: #A020F0\">'pout.tif'<\/span>);\r\nclass(p)\r\nimshow(p)<\/pre><pre style=\"font-style:oblique\">ans =\r\nuint8\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/225\/doubleBenefit_01.png\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pHalf = p * 0.5;\r\nclass(pHalf)\r\nimshow(pHalf)<\/pre><pre style=\"font-style:oblique\">ans =\r\nuint8\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/225\/doubleBenefit_02.png\"> <h3>MATLAB Types<a name=\"12\"><\/a><\/h3>\r\n   <p>Have you had cases where you either took advantage of the interactions of integers and doubles in MATLAB, or where integers\r\n      and floating point integers gave you a surprise?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=225#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a79940bb844b4d4fb085f0e73f1c81f8() {\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='a79940bb844b4d4fb085f0e73f1c81f8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a79940bb844b4d4fb085f0e73f1c81f8';\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 2010 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_a79940bb844b4d4fb085f0e73f1c81f8()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\na79940bb844b4d4fb085f0e73f1c81f8 ##### SOURCE BEGIN #####\r\n%% Double or Nothing?  No Joking!\r\n% Note the date, folks.  I am using this excuse to be a bit light-hearted\r\n% today.  I saw the following quote on someone's door here at MathWorks.\r\n%\r\n% <html>\r\n% <blockquote>\r\n% MATLAB gives you the benefit of the double.\r\n% <\/blockquote>\r\n% <\/html>\r\n%\r\n% This made me smile and I realized that there are some ways in which\r\n% MATLAB uses doubles that might not always be obvious to users.\r\n%% What Type is X?\r\nX = 17\r\n%%\r\n% When the value of |X| is displayed, as |17|, there is no decimal point or\r\n% other indication that |X| is not strictly an integer.  Despite that, |X|\r\n% is a double precision variable.  I have at least a couple of ways to find\r\n% this information programmatically.\r\nwhos\r\n%%\r\nclass(X)\r\n%%\r\n% By default, MATLAB variables are double precision arrays.  In the olden\r\n% days, these were the _only_ kind of arrays in MATLAB.  Back then even\r\n% character arrays were stored as double values.\r\n%% Other Integers\r\n% There are other ways to store integers in MATLAB these days.  Let me list\r\n% a few:\r\n%\r\n% * characters: \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/char.html |char|>\r\n% * signed integers, e.g.,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/int8.html |int8|>\r\n% * unsigned integers, e.g.,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/uint8.html |uint8|>\r\n%\r\n%% Display the Values Stored in Various Types\r\n% First I'll convert the double to a string and display it.\r\nstr17 = num2str(X)\r\n%%\r\n% Notice that the display looks different than that for |X|.  There is no\r\n% leading white space for character arrays (unless it's part of the string \r\n% itself).\r\n%%\r\n% Now let's look at the display for some integer types.\r\nu8 = uint8(X)\r\ni8 = int8(X)\r\n%%\r\n% Notice here that you can't distinguish the *type* based on the display of\r\n% the values.  The default numeric display in MATLAB tries to display the\r\n% maximum amount of information compactly.\r\n%% Some Behaviors\r\n% Because the default type is double, we have found it really convenient to\r\n% allow some interaction between double scalar values and other numeric\r\n% array types.  The way I like to state the behavior is that, for\r\n% arithmetic operators, MATLAB does the calculations _as if the arrays are\r\n% doubles_, and then respects the memory savings of the non-double storage\r\n% class.  What this means, for example, is that I can take a grayscale\r\n% image and halve the values like so, causing the new image to be darker.\r\np = imread('pout.tif');\r\nclass(p)\r\nimshow(p)\r\n%%\r\npHalf = p * 0.5;\r\nclass(pHalf)\r\nimshow(pHalf)\r\n%% MATLAB Types\r\n% Have you had cases where you either took advantage of the interactions of\r\n% integers and doubles in MATLAB, or where integers and floating point\r\n% integers gave you a surprise?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=225#respond here>.\r\n\r\n##### SOURCE END ##### a79940bb844b4d4fb085f0e73f1c81f8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Note the date, folks.  I am using this excuse to be a bit light-hearted today.  I saw the following quote on someone's door\r\n         here at MathWorks.\r\n    ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/01\/double-or-nothing-no-joking\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,15,7],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/225"}],"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=225"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}