{"id":125,"date":"2008-02-07T16:17:44","date_gmt":"2008-02-07T21:17:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/07\/why-hypot\/"},"modified":"2008-02-07T16:22:10","modified_gmt":"2008-02-07T21:22:10","slug":"why-hypot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/07\/why-hypot\/","title":{"rendered":"Why Does MATLAB Have the Function hypot?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Sometimes I am asked questions like \"why did you introduce function <tt>xyz<\/tt> into MATLAB?  And sometimes I have a compelling answer, even for something that looks simple on the surface.  Consider the\r\n         function <tt>hypot<\/tt>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What Does hypot Compute?<\/a><\/li>\r\n         <li><a href=\"#2\">Other Ways to Compute hypot<\/a><\/li>\r\n         <li><a href=\"#3\">Results<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What Does hypot Compute?<a name=\"1\"><\/a><\/h3>\r\n   <p><tt>hypot<\/tt> essentially computes the square root of the sum of the squares of 2 inputs.  So what's the big deal, right?  For \"reasonable\"\r\n      input values, there is no big issue.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fhypot = @(a,b) sqrt(abs(a).^2+abs(b).^2);<\/pre><h3>Other Ways to Compute hypot<a name=\"2\"><\/a><\/h3>\r\n   <p>Let's create a set of values and compute <tt>hypot<\/tt> 3 ways for these: with <tt>hypot<\/tt>, <tt>fhypot<\/tt>, and <tt>sqrt(2)*x<\/tt>.  These should all give the same answers, provide <tt>a<\/tt> is real and positive.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = logspace(0,5,6)\r\nformat <span style=\"color: #A020F0\">long<\/span>\r\naHypot = hypot(a,a)\r\naFhypot = fhypot(a,a)\r\naSqrt2 = sqrt(2)*a<\/pre><pre style=\"font-style:oblique\">a =\r\n           1          10         100        1000       10000      100000\r\naHypot =\r\n  1.0e+005 *\r\n  Columns 1 through 3\r\n   0.000014142135624   0.000141421356237   0.001414213562373\r\n  Columns 4 through 6\r\n   0.014142135623731   0.141421356237310   1.414213562373095\r\naFhypot =\r\n  1.0e+005 *\r\n  Columns 1 through 3\r\n   0.000014142135624   0.000141421356237   0.001414213562373\r\n  Columns 4 through 6\r\n   0.014142135623731   0.141421356237310   1.414213562373095\r\naSqrt2 =\r\n  1.0e+005 *\r\n  Columns 1 through 3\r\n   0.000014142135624   0.000141421356237   0.001414213562373\r\n  Columns 4 through 6\r\n   0.014142135623731   0.141421356237310   1.414213562373095\r\n<\/pre><h3>Results<a name=\"3\"><\/a><\/h3>\r\n   <p>The results are the same, to within round-off, for the 3 methods here. But what happens if the magnitude of <tt>a<\/tt> is larger, near <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/realmax.html\"><tt>realmax<\/tt><\/a> perhaps?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">realmax<\/pre><pre style=\"font-style:oblique\">ans =\r\n    1.797693134862316e+308\r\n<\/pre><p>For my computer, a 32-bit Windows machine, <tt>realmax<\/tt> for doubles is on the order of 10^308.  Let's see what happens if a value nearly that large is used with the different versions\r\n      of <tt>hypot<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = 1e308\r\naHypot = hypot(a,a)\r\naFhypot = fhypot(a,a)\r\naSqrt2 = sqrt(2)*a<\/pre><pre style=\"font-style:oblique\">a =\r\n    1.000000000000000e+308\r\naHypot =\r\n    1.414213562373095e+308\r\naFhypot =\r\n   Inf\r\naSqrt2 =\r\n    1.414213562373095e+308\r\n<\/pre><p>What you can see is that the straight-forward method returns <tt>Inf<\/tt> instead of a finite answer.  And that's why <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/hypot.html\"><tt>hypot<\/tt><\/a> was added to MATLAB, to compute the hypotenuse robustly, avoiding both underflow and overflow.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1aa822463de14d5fac7a5f8e843ebef0() {\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='1aa822463de14d5fac7a5f8e843ebef0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1aa822463de14d5fac7a5f8e843ebef0';\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 2008 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_1aa822463de14d5fac7a5f8e843ebef0()\"><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.5<br><\/p>\r\n<\/div>\r\n<!--\r\n1aa822463de14d5fac7a5f8e843ebef0 ##### SOURCE BEGIN #####\r\n%% Why Does MATLAB Have the Function hypot?\r\n% Sometimes I am asked questions like \"why did you introduce function |xyz|\r\n% into MATLAB?  And sometimes I have a compelling answer, even for\r\n% something that looks simple on the surface.  Consider the function\r\n% |hypot|.\r\n%% What Does hypot Compute?\r\n% |hypot| essentially computes the square root of the sum of the squares of\r\n% 2 inputs.  So what's the big deal, right?  For \"reasonable\" input values,\r\n% there is no big issue.\r\nfhypot = @(a,b) sqrt(abs(a).^2+abs(b).^2);\r\n%% Other Ways to Compute hypot\r\n% Let's create a set of values and compute |hypot| 3 ways for these: with\r\n% |hypot|, |fhypot|, and |sqrt(2)*x|.  These should all give the same\r\n% answers, provide |a| is real and positive.\r\na = logspace(0,5,6)\r\nformat long\r\naHypot = hypot(a,a)\r\naFhypot = fhypot(a,a)\r\naSqrt2 = sqrt(2)*a\r\n%% Results\r\n% The results are the same, to within round-off, for the 3 methods here.\r\n% But what happens if the magnitude of |a| is larger, near \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/realmax.html |realmax|>\r\n% perhaps?\r\nrealmax\r\n%%\r\n% For my computer, a 32-bit Windows machine, |realmax| for doubles is on\r\n% the order of 10^308.  Let's see what happens if a value nearly that large\r\n% is used with the different versions of |hypot|.\r\na = 1e308\r\naHypot = hypot(a,a)\r\naFhypot = fhypot(a,a)\r\naSqrt2 = sqrt(2)*a\r\n%% \r\n% What you can see is that the straight-forward method returns |Inf|\r\n% instead of a finite answer.  And that's why \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/hypot.html |hypot|>\r\n% was added to MATLAB, to compute the hypotenuse robustly, avoiding both\r\n% underflow and overflow.\r\n##### SOURCE END ##### 1aa822463de14d5fac7a5f8e843ebef0\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Sometimes I am asked questions like \"why did you introduce function xyz into MATLAB?  And sometimes I have a compelling answer, even for something that looks simple on the surface. ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/07\/why-hypot\/\">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,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/125"}],"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=125"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}