hypot.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>hypot</title>
  5. <link rel="stylesheet" href="../../math.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
  8. <link rel="up" href="../powers.html" title="Basic Functions">
  9. <link rel="prev" href="powm1.html" title="powm1">
  10. <link rel="next" href="ct_pow.html" title="Compile Time Power of a Runtime Base">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="powm1.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../powers.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="ct_pow.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="math_toolkit.powers.hypot"></a><a class="link" href="hypot.html" title="hypot">hypot</a>
  28. </h3></div></div></div>
  29. <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">&gt;</span>
  30. <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">hypot</span><span class="special">(</span><span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">y</span><span class="special">);</span>
  31. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
  32. <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">hypot</span><span class="special">(</span><span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">y</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
  33. </pre>
  34. <p>
  35. <span class="bold"><strong>Effects: </strong></span> computes
  36. </p>
  37. <div class="blockquote"><blockquote class="blockquote"><p>
  38. <span class="inlinemediaobject"><img src="../../../equations/hypot.svg"></span>
  39. </p></blockquote></div>
  40. <p>
  41. in such a way as to avoid undue underflow and overflow.
  42. </p>
  43. <p>
  44. The return type of this function is computed using the <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result
  45. type calculation rules</em></span></a> when T1 and T2 are of different
  46. types.
  47. </p>
  48. <p>
  49. The final <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
  50. be used to control the behaviour of the function: how it handles errors,
  51. what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">policy
  52. documentation for more details</a>.
  53. </p>
  54. <p>
  55. When calculating
  56. </p>
  57. <div class="blockquote"><blockquote class="blockquote"><p>
  58. <span class="inlinemediaobject"><img src="../../../equations/hypot.svg"></span>
  59. </p></blockquote></div>
  60. <p>
  61. it's quite easy for the intermediate terms to either overflow or underflow,
  62. even though the result is in fact perfectly representable.
  63. </p>
  64. <h5>
  65. <a name="math_toolkit.powers.hypot.h0"></a>
  66. <span class="phrase"><a name="math_toolkit.powers.hypot.implementation"></a></span><a class="link" href="hypot.html#math_toolkit.powers.hypot.implementation">Implementation</a>
  67. </h5>
  68. <p>
  69. The function is even and symmetric in <span class="emphasis"><em>x</em></span> and <span class="emphasis"><em>y</em></span>,
  70. so first take assume <span class="emphasis"><em>x,y &gt; 0</em></span> and <span class="emphasis"><em>x &gt;
  71. y</em></span> (we can permute the arguments if this is not the case).
  72. </p>
  73. <p>
  74. Then if <span class="emphasis"><em>x * &#949; &gt;= y</em></span> we can simply return <span class="emphasis"><em>x</em></span>.
  75. </p>
  76. <p>
  77. Otherwise the result is given by:
  78. </p>
  79. <div class="blockquote"><blockquote class="blockquote"><p>
  80. <span class="inlinemediaobject"><img src="../../../equations/hypot2.svg"></span>
  81. </p></blockquote></div>
  82. </div>
  83. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  84. <td align="left"></td>
  85. <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
  86. Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
  87. Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
  88. R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
  89. Daryle Walker and Xiaogang Zhang<p>
  90. Distributed under the Boost Software License, Version 1.0. (See accompanying
  91. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  92. </p>
  93. </div></td>
  94. </tr></table>
  95. <hr>
  96. <div class="spirit-nav">
  97. <a accesskey="p" href="powm1.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../powers.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="ct_pow.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  98. </div>
  99. </body>
  100. </html>