{"id":4147,"date":"2020-07-23T08:03:07","date_gmt":"2020-07-23T12:03:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=4147"},"modified":"2020-08-18T09:12:55","modified_gmt":"2020-08-18T13:12:55","slug":"what-is-0-01","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2020\/07\/23\/what-is-0-01\/","title":{"rendered":"What is 0.01?"},"content":{"rendered":"<div class=\"content\"><p>I was looking at these two numbers the other day:<\/p><p>$0.01$<\/p><p>$\\frac{5764607523034235}{576460752303423488}$<\/p><p>It turns out they are <b>very<\/b> close, differing only by about 5 parts in a quadrillion.<\/p><p>A tech support case caught my attention on that day because MATLAB creator <a href=\"https:\/\/www.mathworks.com\/moler.html\">Cleve Moler<\/a> (<a href=\"https:\/\/www.mathworks.com\/moler\/index_ncm.html\">Numerical Computing with MATLAB<\/a>, <a href=\"https:\/\/www.mathworks.com\/moler\/exm.html\">Experiments with MATLAB<\/a>, <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/introducing-cleves-laboratory.html\">Cleve's Laboratory<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/cleve\/\">Cleve's Corner<\/a>) responded to it, and I always enjoy Cleve's explanations.<\/p><p>The tech support case turned out to be related to understanding how floating-point numbers work. In part of his response, Cleve said, \"1.e-2 is not one-hundredth; it is the floating point number nearest one-hundredth.\"<\/p><p>The floating-point number system used in most numerical computing applications, including MATLAB, is genius. It enables us to successfully pretend (most of the time) that we are working with the real numbers (&#8477;). We type 0.01, meaning 1\/100, without much thought.<\/p><p>Every now and then, however, it helps to be aware that the floating-point number system is really a finite-precision approximation of &#8477;. Every number in this system is a base-2 fraction multiplied by a power of 2. Just as the rational number 1\/3 does not have a finite-length representation in base 10, the rational number 1\/100 does not have a finite-length representation in base 2. That means that when you enter 0.01 in a numerical application such as MATLAB or a spreadsheet program, the number stored by the computer is very, very close to 1\/100, but not exactly equal to it.<\/p><p>You can get a sense of this by using <tt>format hex<\/tt>, which displays the underlying bytes of the floating-point number using hexadecimal notation:<\/p><pre class=\"codeinput\">format <span class=\"string\">hex<\/span>\r\na = 0.01\r\n\r\n<span class=\"comment\">% Restore the normal format<\/span>\r\nformat\r\n<\/pre><pre class=\"codeoutput\">\r\na =\r\n\r\n   3f847ae147ae147b\r\n\r\n<\/pre><p>You can see the repeating digit pattern <tt>ae147<\/tt>, with the final hexadecimal digit, <tt>b<\/tt>, representing <tt>ae147<\/tt> rounded to <tt>b<\/tt>. To paraphrase Cleve's point, this number is not 1\/100; it is a binary fraction, scaled by a power of 2, that is <b>very close<\/b> to 1\/100.<\/p><p>I always find it interesting to use the Symbolic Math Toolbox to find out the <b>exact<\/b> value of a floating-point number. You can do this using the <tt>sym<\/tt> function with the <tt>'f'<\/tt> option. According to the documentation, using this option returns a symbolic number that is a precise rational number equal to the floating-point value.<\/p><p>So, here is one representation of true value of the floating-point number you get when you type 0.01:<\/p><pre class=\"codeinput\">sym(0.01,<span class=\"string\">'f'<\/span>)\r\n<\/pre><pre class=\"codeoutput\"> \r\nans =\r\n \r\n5764607523034235\/576460752303423488\r\n \r\n<\/pre><p>This displays better using the Live Editor; here is a screenshot:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/sym-f-1-100.png\" alt=\"\"> <\/p><p>I encourage you to take a look at Cleve's <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2014\/07\/07\/floating-point-numbers\/\">07-Jul-2014 blog post<\/a> on floating-point numbers. If your work involves any kind of numerical computing, it will be helpful to understand more about them.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_fd09e4f4292b4d51b0b1868b8913d78f() {\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='fd09e4f4292b4d51b0b1868b8913d78f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fd09e4f4292b4d51b0b1868b8913d78f';\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 2020 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_fd09e4f4292b4d51b0b1868b8913d78f()\"><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; R2020a<br><\/p><\/div><!--\r\nfd09e4f4292b4d51b0b1868b8913d78f ##### SOURCE BEGIN #####\r\n%%\r\n% I was looking at these two numbers the other day:\r\n%\r\n% $0.01$\r\n%\r\n% $\\frac{5764607523034235}{576460752303423488}$\r\n%\r\n% It turns out they are *very* close, differing only by about 5 parts in\r\n% a quadrillion.\r\n%\r\n% A tech support case caught my attention the other day because MATLAB\r\n% creator <https:\/\/www.mathworks.com\/moler.html Cleve Moler>\r\n% (<https:\/\/www.mathworks.com\/moler\/index_ncm.html Numerical Computing\r\n% with MATLAB>, <https:\/\/www.mathworks.com\/moler\/exm.html Experiments\r\n% with MATLAB>,\r\n% <https:\/\/www.mathworks.com\/company\/newsletters\/articles\/introducing-cleves-laboratory.html\r\n% Cleve's Laboratory>, <https:\/\/blogs.mathworks.com\/cleve\/ Cleve's\r\n% Corner>) responded to it, and I always enjoy Cleve's explanations. \r\n%\r\n% The tech support turned out to be related to understanding how\r\n% floating-point numbers work. In part of his response, Cleve said,\r\n% \"1.e-2 is not one-hundredth; it is the floating point number nearest\r\n% one-hundredth.\"\r\n%\r\n% The floating-point number system used in most numerical computing\r\n% applications, including MATLAB, is genius. It enables us to successfully\r\n% pretend (most of the time) that we are working with the real numbers\r\n% (\u211d). We type 0.01, meaning 1\/100, without much thought.\r\n%\r\n% Every now and then, however, it helps to be aware that the\r\n% floating-point number system is really a finite-precision\r\n% approximation of \u211d. Every number in this system is a base-2 fraction\r\n% multiplied by a power of 2. Just as the rational number 1\/3 does not\r\n% have a finite-length representation in base 10, the rational number\r\n% 1\/100 does not have a finite-length representation in base 2. That\r\n% means that when you enter 0.01 in a numerical application such as\r\n% MATLAB or a spreadsheet program, the number stored by the computer is\r\n% very, very close to 1\/100, but not exactly equal to it.\r\n%\r\n% You can get a sense of this by using |format hex|, which displays the\r\n% underlying bytes of the floating-point number using hexadecimal\r\n% notation:\r\n\r\nformat hex\r\na = 0.01\r\n\r\n% Restore the normal format\r\nformat\r\n\r\n%%\r\n% You can see the repeating digit pattern |ae147|, with the final\r\n% hexadecimal digit, |b|, representing |ae147| rounded to |b|. To\r\n% paraphrase Cleve's point, this number is not 1\/100; it is a binary\r\n% fraction, scaled by a power of 2, that is *very close* to 1\/100.\r\n%\r\n% I always find it interesting to use the Symbolic Math Toolbox\r\n% to find out the *exact* value of a floating-point number. You can do\r\n% this using the |sym| function with the |'f'| option. According to the\r\n% documentation, using this option returns a symbolic number that is a\r\n% precise rational number equal to the floating-point value.\r\n%\r\n% So, here is one representation of true value of the floating-point\r\n% number you get when you type 0.01:\r\n\r\nsym(0.01,'f')\r\n\r\n%%\r\n% This displays better using the Live Editor; here is a screenshot:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/sym-f-1-100.png>>\r\n%\r\n% I encourage you to take a look at Cleve's\r\n% <https:\/\/blogs.mathworks.com\/cleve\/2014\/07\/07\/floating-point-numbers\/\r\n% 07-Jul-2014 blog post> on floating-point numbers. If your work\r\n% involves any kind of numerical computing, it will be helpful to\r\n% understand more about them.\r\n##### SOURCE END ##### fd09e4f4292b4d51b0b1868b8913d78f\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/sym-f-1-100.png\" onError=\"this.style.display ='none';\" \/><\/div><p>I was looking at these two numbers the other day:$0.01$$\\frac{5764607523034235}{576460752303423488}$It turns out they are very close, differing only by about 5 parts in a quadrillion.A tech support... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/07\/23\/what-is-0-01\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[430,1173],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4147"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=4147"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4147\/revisions"}],"predecessor-version":[{"id":4155,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4147\/revisions\/4155"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=4147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=4147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=4147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}