{"id":196,"date":"2009-08-20T13:11:52","date_gmt":"2009-08-20T13:11:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/20\/precision-and-realmax\/"},"modified":"2009-08-20T13:12:16","modified_gmt":"2009-08-20T13:12:16","slug":"precision-and-realmax","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/20\/precision-and-realmax\/","title":{"rendered":"Precision and realmax"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I was on a recent email thread at the office regarding <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/realmax.html\"><tt>realmax<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/eps.html\"><tt>eps<\/tt><\/a>, and overflow.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What is realmax?<\/a><\/li>\r\n         <li><a href=\"#3\">Overflow with Respect to realmax<\/a><\/li>\r\n         <li><a href=\"#5\">How about Less than eps?<\/a><\/li>\r\n         <li><a href=\"#7\">References<\/a><\/li>\r\n         <li><a href=\"#8\">Do Floating Point Issues Haunt You?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What is realmax?<a name=\"1\"><\/a><\/h3>\r\n   <p>From the help for <tt>realmax<\/tt> we learn that\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = help(<span style=\"color: #A020F0\">'realmax'<\/span>);\r\ndisp(h(1:48))<\/pre><pre style=\"font-style:oblique\"> REALMAX Largest positive floating point number.\r\n<\/pre><p>Here's the double precision value for <tt>realmax<\/tt> shown with about 15 digits.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">long<\/span>\r\nrmax = realmax<\/pre><pre style=\"font-style:oblique\">\r\nrmax =\r\n\r\n    1.797693134862316e+308\r\n\r\n<\/pre><h3>Overflow with Respect to realmax<a name=\"3\"><\/a><\/h3>\r\n   <p>Let's see what it takes to add to <tt>realmax<\/tt> to get to the next <b>number<\/b>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">epsrmax = eps(rmax)\r\nbig = rmax+epsrmax<\/pre><pre style=\"font-style:oblique\">\r\nepsrmax =\r\n\r\n    1.995840309534720e+292\r\n\r\n\r\nbig =\r\n\r\n   Inf\r\n\r\n<\/pre><p>Are you surprised that the next largest number is <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/inf.html\"><tt>Inf<\/tt><\/a>?\r\n   <\/p>\r\n   <h3>How about Less than eps?<a name=\"5\"><\/a><\/h3>\r\n   <p>What if we try a number a tenth of <tt>eps<\/tt>?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">biggish = rmax + epsrmax\/10\r\nisequal(biggish, rmax)<\/pre><pre style=\"font-style:oblique\">\r\nbiggish =\r\n\r\n    1.797693134862316e+308\r\n\r\n\r\nans =\r\n\r\n     1\r\n\r\n<\/pre><p>It's not far enough away on the floating point scale to produce a value different from <tt>rmax<\/tt>. In fact, the help for <tt>eps<\/tt> has a list of values for <tt>eps<\/tt> at some interesting double and single values.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">eps<\/span><\/pre><pre style=\"font-style:oblique\"> EPS  Spacing of floating point numbers.\r\n    D = EPS(X), is the positive distance from ABS(X) to the next larger in\r\n    magnitude floating point number of the same precision as X.\r\n    X may be either double precision or single precision.\r\n    For all X, EPS(X) is equal to EPS(ABS(X)).\r\n \r\n    EPS, with no arguments, is the distance from 1.0 to the next larger double\r\n    precision number, that is EPS with no arguments returns 2^(-52).\r\n \r\n    EPS('double') is the same as EPS, or EPS(1.0).\r\n    EPS('single') is the same as EPS(single(1.0)), or single(2^-23).\r\n \r\n    Except for numbers whose absolute value is smaller than REALMIN,\r\n    if 2^E &lt;= ABS(X) &lt; 2^(E+1), then\r\n       EPS(X) returns 2^(E-23) if ISA(X,'single')\r\n       EPS(X) returns 2^(E-52) if ISA(X,'double')\r\n \r\n    For all X of class double such that ABS(X) &lt;= REALMIN, EPS(X)\r\n    returns 2^(-1074).   Similarly, for all X of class single such that\r\n    ABS(X) &lt;= REALMIN('single'), EPS(X) returns 2^(-149).\r\n \r\n    Replace expressions of the form\r\n       if Y &lt; EPS * ABS(X)\r\n    with\r\n       if Y &lt; EPS(X)\r\n \r\n    Example return values from calling EPS with various inputs are\r\n    presented in the table below:\r\n \r\n          Expression                   Return Value\r\n         ===========================================\r\n          eps(1\/2)                     2^(-53)\r\n          eps(1)                       2^(-52)\r\n          eps(2)                       2^(-51)\r\n          eps(realmax)                 2^971\r\n          eps(0)                       2^(-1074)\r\n          eps(realmin\/2)               2^(-1074)\r\n          eps(realmin\/16)              2^(-1074)\r\n          eps(Inf)                     NaN\r\n          eps(NaN)                     NaN\r\n         -------------------------------------------\r\n          eps(single(1\/2))             2^(-24)\r\n          eps(single(1))               2^(-23)\r\n          eps(single(2))               2^(-22)\r\n          eps(realmax('single'))       2^104\r\n          eps(single(0))               2^(-149)\r\n          eps(realmin('single')\/2)    2^(-149)\r\n          eps(realmin('single')\/16)   2^(-149)\r\n          eps(single(Inf))             single(NaN)\r\n          eps(single(NaN))             single(NaN)\r\n \r\n    See also REALMAX, REALMIN.\r\n\r\n    Overloaded methods:\r\n       codistributed\/eps\r\n       quantizer\/eps\r\n       qfft\/eps\r\n\r\n    Reference page in Help browser\r\n       doc eps\r\n\r\n<\/pre><h3>References<a name=\"7\"><\/a><\/h3>\r\n   <p>Here are some references to help you learn more about floating point issues.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/23\/a-glimpse-into-floating-point-accuracy\/\">Floating Point 1<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/05\/20\/another-lesson-in-floating-point\/\">Floating Point 2<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/content\/dam\/mathworks\/mathworks-dot-com\/company\/newsletters\/news_notes\/pdf\/Fall96Cleve.pdf\">Cleve's Corner<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/support\/solutions\/data\/1-16FOQ.html?solution=1-16FOQ\">How do I tell if the error in my answer is the result of round-off error or a bug?<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/moler.html\">Numerical Computing with MATLAB, Cleve's book<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Do Floating Point Issues Haunt You?<a name=\"8\"><\/a><\/h3>\r\n   <p>Do you recognize when issues you have are from floating point considerations?  Do you have any war stories or best practices\r\n      to share with others?  Post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=196#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8d82fbd593ca4298a9d40b8ac931ad13() {\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='8d82fbd593ca4298a9d40b8ac931ad13 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8d82fbd593ca4298a9d40b8ac931ad13';\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_8d82fbd593ca4298a9d40b8ac931ad13()\"><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\n8d82fbd593ca4298a9d40b8ac931ad13 ##### SOURCE BEGIN #####\r\n%% Precision and realmax\r\n% I was on a recent email thread at the office regarding\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/realmax.html |realmax|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/eps.html |eps|>,\r\n% and overflow.\r\n%% What is realmax?\r\n% From the help for |realmax| we learn that\r\nh = help('realmax');\r\ndisp(h(1:48))\r\n%%\r\n% Here's the double precision value for |realmax| shown with about 15\r\n% digits.\r\nformat long\r\nrmax = realmax\r\n\r\n%% Overflow with Respect to realmax\r\n% Let's see what it takes to add to |realmax| to get to the next\r\n% *number*.\r\nepsrmax = eps(rmax)\r\nbig = rmax+epsrmax\r\n%%\r\n% Are you surprised that the next largest number is\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/inf.html |Inf|>?\r\n%% How about Less than eps?\r\n% What if we try a number a tenth of |eps|?\r\nbiggish = rmax + epsrmax\/10\r\nisequal(biggish, rmax)\r\n%%\r\n% It's not far enough away on the floating point scale to produce a value\r\n% different from |rmax|. In fact, the help for |eps| has a list of values\r\n% for |eps| at some interesting double and single values.\r\nhelp eps\r\n%% References\r\n% Here are some references to help you learn more about floating point\r\n% issues.\r\n%\r\n% * <https:\/\/blogs.mathworks.com\/loren\/2006\/08\/23\/a-glimpse-into-floating-point-accuracy\/ Floating Point 1>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/2008\/05\/20\/another-lesson-in-floating-point\/ Floating Point 2>\r\n% * <https:\/\/www.mathworks.com\/content\/dam\/mathworks\/mathworks-dot-com\/company\/newsletters\/news_notes\/pdf\/Fall96Cleve.pdf Cleve's Corner>\r\n% * <https:\/\/www.mathworks.com\/support\/solutions\/data\/1-16FOQ.html?solution=1-16FOQ How do I tell if the error in my answer is the result of round-off error or a bug?>\r\n% * <https:\/\/www.mathworks.com\/moler.html Numerical Computing with MATLAB, Cleve's book>\r\n%\r\n%% Do Floating Point Issues Haunt You?\r\n% Do you recognize when issues you have are from floating point\r\n% considerations?  Do you have any war stories or best practices to share\r\n% with others?  Post them <https:\/\/blogs.mathworks.com\/loren\/?p=196#respond here>.\r\n \r\n\r\n\r\n##### SOURCE END ##### 8d82fbd593ca4298a9d40b8ac931ad13\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I was on a recent email thread at the office regarding realmax, eps, and overflow.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n    ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/08\/20\/precision-and-realmax\/\">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,29],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/196"}],"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=196"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}