{"id":71,"date":"2006-12-27T16:43:22","date_gmt":"2006-12-27T21:43:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=71"},"modified":"2007-03-08T09:53:53","modified_gmt":"2007-03-08T14:53:53","slug":"displaying-numbers-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/12\/27\/displaying-numbers-in-matlab\/","title":{"rendered":"Displaying Numbers in MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>The way MATLAB displays numbers sometimes confuses users.  Occasionally someone posts a concern to the <a href=\"\">MATLAB newsgroup<\/a> that the calculation just performed was done to only 4 digits but the user expected more decimals.  Assuming the code is\r\n         \"typical\" in some vague sense, the user's numeric information at this point is almost always sitting in double precision MATLAB\r\n         variables.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Numeric Formats in MATLAB<\/a><\/li>\r\n         <li><a href=\"#5\">Engineering Notation<\/a><\/li>\r\n         <li><a href=\"#7\">Interesting Diversion<\/a><\/li>\r\n         <li><a href=\"#9\">References<\/a><\/li>\r\n         <li><a href=\"#10\">Year End<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Numeric Formats in MATLAB<a name=\"1\"><\/a><\/h3>\r\n   <p>Helpful posts typically follow up with the explanation that the display of output is controlled by the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/format.html\"><tt>format<\/tt><\/a> setting and that the default numeric variables in MATLAB are indeed double precision, holding about 15 significant decimal\r\n      digits.  By default, MATLAB displays floating point values using what's called a scaled fixed point format, resulting in 4\r\n      digits displayed after the decimal point.\r\n   <\/p>\r\n   <p>Here's the value of <tt>pi<\/tt> displayed with the default format in MATLAB.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format\r\nmypi = pi<\/pre><pre style=\"font-style:oblique\">\r\nmypi =\r\n\r\n    3.1416\r\n\r\n<\/pre><p>and we can see that the variable <tt>mypi<\/tt> is stored as a double precision value.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">class(mypi)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\ndouble\r\n\r\n<\/pre><p>To see more digits, I can now use some of the other <tt>format<\/tt> options, for example\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">long<\/span>\r\nmypi<\/pre><pre style=\"font-style:oblique\">\r\nmypi =\r\n\r\n   3.141592653589793\r\n\r\n<\/pre><h3>Engineering Notation<a name=\"5\"><\/a><\/h3>\r\n   <p>In Release R14SP2, we enhanced <tt>format<\/tt> to include the engineering formats (short and long), resulting in values displayed with powers of 10 appearing in multiples\r\n      of 3.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">short<\/span> <span style=\"color: #A020F0\">eng<\/span>\r\nmanypis = mypi*[1 10 100 1000]<\/pre><pre style=\"font-style:oblique\">\r\nmanypis =\r\n\r\n     3.1416e+000    31.4159e+000   314.1593e+000     3.1416e+003\r\n\r\n<\/pre><p>The new formats allow you some extra flexibility on how to view data before resorting to writing your own specialized formatting\r\n      routines, for example using <tt>fprintf<\/tt>.\r\n   <\/p>\r\n   <h3>Interesting Diversion<a name=\"7\"><\/a><\/h3>\r\n   <p>While thinking about this post, I wondered about the history of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Scientific_notation\">scientific and engineering notation<\/a>. Using some of my favorite web tools, I found out that scientific notation was first mentioned historically by <a href=\"http:\/\/www.nndb.com\/people\/588\/000087327\/\">Florian Cajori<\/a> who wrote A History of Mathematical Notations (1928-29, 2 vols.).\r\n   <\/p>\r\n   <p>I next wandered down the hall to talk to <a href=\"https:\/\/blogs.mathworks.com\/images\/eddins.jpg\">Steve<\/a> of <a href=\"https:\/\/blogs.mathworks.com\/steve\/\">Image Processing blog<\/a> fame. Wouldn't you know!  He sent a letter to his son's teacher last year when the class was asking about what engineering\r\n      notation was useful for. Steve found some interesting information engineering format for calculators.  How cool is it that\r\n      I can walk into his office and talk about something tangential like this bit of history and find out that not only does the\r\n      topic interest him too, but he had his own reason at looking into it from a slightly different perspective.  It made my day!\r\n   <\/p>\r\n   <h3>References<a name=\"9\"><\/a><\/h3>\r\n   <p>Here are some other references that might be helpful for those sorting out questions of display and precision.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fprintf.html\"><tt>fprintf<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=51\">A Glimpse into Floating-Point Accuracy<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Year End<a name=\"10\"><\/a><\/h3>\r\n   <p>It's nearly the end of the year 2006 as well as being approximately the first year birthday for this blog.  It's been wonderful\r\n      meeting you in this forum (and occasionally other ways as well!).  I'd encourage you to continue <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/12\/27\/displaying-numbers-in-matlab\/#respond\">sharing your thoughts<\/a> with my during the next year as well.  Happy New Year to all!\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0827fe0a36be4f63a1c9ce1ebf859ed9() {\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='0827fe0a36be4f63a1c9ce1ebf859ed9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0827fe0a36be4f63a1c9ce1ebf859ed9';\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 2006 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_0827fe0a36be4f63a1c9ce1ebf859ed9()\"><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.3<br><\/p>\r\n<\/div>\r\n<!--\r\n0827fe0a36be4f63a1c9ce1ebf859ed9 ##### SOURCE BEGIN #####\r\n%% Displaying Numbers in MATLAB\r\n% The way MATLAB displays numbers sometimes confuses users.  Occasionally\r\n% someone posts a concern to the\r\n% < MATLAB newsgroup>\r\n% that the calculation just performed was done to only 4 digits but the\r\n% user expected more decimals.  Assuming the code is \"typical\" in some\r\n% vague sense, the user's numeric information at this point is almost\r\n% always sitting in double precision MATLAB variables.\r\n%% Numeric Formats in MATLAB\r\n% Helpful posts typically follow up with the explanation\r\n% that the display of output is controlled by the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/format.html |format|>\r\n% setting and that the default numeric variables in MATLAB are indeed\r\n% double precision, holding about 15 significant decimal digits.  By\r\n% default, MATLAB displays floating point values using what's called a\r\n% scaled fixed point format, resulting in 4 digits displayed after the\r\n% decimal point.  \r\n%%\r\n% Here's the value of |pi| displayed with the default format in MATLAB.\r\nformat\r\nmypi = pi\r\n%% \r\n% and we can see that the variable |mypi| is stored as a double precision\r\n% value.\r\nclass(mypi)\r\n%%\r\n% To see more digits, I can now use some of the other |format| options, for\r\n% example\r\nformat long\r\nmypi\r\n%% Engineering Notation\r\n% In Release R14SP2, we enhanced |format| to include the engineering\r\n% formats (short and long), resulting in values displayed with powers of 10\r\n% appearing in multiples of 3.\r\nformat short eng\r\nmanypis = mypi*[1 10 100 1000]\r\n%% \r\n% The new formats allow you some extra flexibility on how to view data\r\n% before resorting to writing your own specialized formatting routines, for\r\n% example using |fprintf|.\r\n%% Interesting Diversion\r\n% While thinking about this post, I wondered about the history of\r\n% <http:\/\/en.wikipedia.org\/wiki\/Scientific_notation scientific and engineering notation>.  \r\n% Using some of my favorite web tools, I found out\r\n% that scientific notation was first mentioned historically by \r\n% <http:\/\/www.nndb.com\/people\/588\/000087327\/ Florian Cajori> who wrote\r\n% A History of Mathematical Notations (1928-29, 2 vols.).  \r\n%%\r\n% I next wandered\r\n% down the hall to talk to <https:\/\/blogs.mathworks.com\/images\/eddins.jpg Steve> \r\n% of <https:\/\/blogs.mathworks.com\/steve\/ Image Processing blog> fame.\r\n% Wouldn't you know!  He sent a letter to his son's teacher last year when\r\n% the class was asking about what engineering notation was useful for.  \r\n% Steve found some interesting information engineering format for\r\n% calculators.  How cool is it that I can walk into his office and talk\r\n% about something tangential like this bit of history and find out that not\r\n% only does the topic interest him too, but he had his own reason at\r\n% looking into it from a slightly different perspective.  It made my day!\r\n\r\n%% References\r\n% Here are some other references that might be helpful for those sorting\r\n% out questions of display and precision.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fprintf.html |fprintf|>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=51 A Glimpse into Floating-Point Accuracy>\r\n\r\n%% Year End\r\n% It's nearly the end of the year 2006 as well as being approximately the\r\n% first year birthday for this blog.  It's been wonderful meeting you in\r\n% this forum (and occasionally other ways as well!).  I'd encourage you to\r\n% continue <?p=71#respond sharing your thoughts> with my during the next\r\n% year as well.  Happy New Year to all!\r\n\r\n##### SOURCE END ##### 0827fe0a36be4f63a1c9ce1ebf859ed9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      The way MATLAB displays numbers sometimes confuses users.  Occasionally someone posts a concern to the MATLAB newsgroup that the calculation just performed was done to only 4 digits but... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/12\/27\/displaying-numbers-in-matlab\/\">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,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/71"}],"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=71"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}