summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fhypot.html
blob: bd94e763494f0fadb66fd3adef6ac4186aca321a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    <h1 id="firstHeading" class="firstHeading">hypot, hypotf, hypotl</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float       hypotf( float x, float y );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double      hypot( double x, double y );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double hypotl( long double x, long double y );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define hypot( x, y )</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1-3)</span> Computes the square root of the sum of the squares of <code>x</code> and <code>y</code>, without undue overflow or underflow at intermediate stages of the computation.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If any argument has type <code>long double</code>, the long double version of the function is called. Otherwise, if any argument has integer type or has type <code>double</code>, the double version of the function is called. Otherwise, the <code>float</code> version of the function is called.</div> <p>The value computed by this function is the length of the hypotenuse of a right-angled triangle with sides of length <code>x</code> and <code>y</code>, or the distance of the point <code>(x,y)</code> from the origin <code>(0,0)</code>, or the magnitude of a complex number <code>x+<i>i</i>y</code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x </td> <td> - </td> <td> floating point value </td>
</tr> <tr class="t-par"> <td> y </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the hypotenuse of a right-angled triangle, \(\scriptsize{\sqrt{x^2+y^2} }\)<span class="t-mrad"><span>√</span><span>x<sup class="t-su">2</sup>+y<sup class="t-su">2</sup></span></span>, is returned.</p>
<p>If a range error due to overflow occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">+HUGE_VAL</a></code>, <code>+HUGE_VALF</code>, or <code>+HUGE_VALL</code> is returned.</p>
<p>If a range error due to underflow occurs, the correct result (after rounding) is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> <code>hypot(x, y)</code>, <code>hypot(y, x)</code>, and <code>hypot(x, -y)</code> are equivalent </li>
<li> if one of the arguments is ±0, <code>hypot</code> is equivalent to <code><a href="http://en.cppreference.com/w/c/numeric/math/fabs"><span class="kw644">fabs</span></a></code> called with the non-zero argument </li>
<li> if one of the arguments is ±∞, <code>hypot</code> returns +∞ even if the other argument is NaN </li>
<li> otherwise, if any of the arguments is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p>Implementations usually guarantee precision of less than 1 ulp (<a href="https://en.wikipedia.org/wiki/Unit_in_the_last_place" class="extiw" title="enwiki:Unit in the last place">units in the last place</a>): <a rel="nofollow" class="external text" href="http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/ieee754/dbl-64/e_hypot.c">GNU</a>, <a rel="nofollow" class="external text" href="http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/msun/src/e_hypot.c">BSD</a>.</p>
<p><code>hypot(x, y)</code> is equivalent to <code><a href="http://en.cppreference.com/w/c/numeric/complex/cabs"><span class="kw765">cabs</span></a><span class="br0">(</span>x <span class="sy2">+</span> I<span class="sy2">*</span>y<span class="br0">)</span></code>.</p>
<p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/hypot.html">POSIX specifies</a> that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations).</p>
<p><code>hypot(INFINITY, NAN)</code> returns +∞, but <code><a href="http://en.cppreference.com/w/c/numeric/math/sqrt"><span class="kw665">sqrt</span></a><span class="br0">(</span>INFINITY<span class="sy2">*</span>INFINITY<span class="sy2">+</span>NAN<span class="sy2">*</span>NAN<span class="br0">)</span></code> returns NaN.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;errno.h&gt;
#include &lt;fenv.h&gt;
#include &lt;float.h&gt;
 
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    // typical usage
    printf("(1,1) cartesian is (%f,%f) polar\n", hypot(1,1), atan2(1,1));
    // special values
    printf("hypot(NAN,INFINITY) = %f\n", hypot(NAN,INFINITY));
    // error handling 
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("hypot(DBL_MAX,DBL_MAX) = %f\n", hypot(DBL_MAX,DBL_MAX));
    if(errno == ERANGE)         perror("    errno == ERANGE");
    if(fetestexcept(FE_OVERFLOW)) puts("    FE_OVERFLOW raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">(1,1) cartesian is (1.414214,0.785398) polar
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.7.3 The hypot functions (p: 181) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.4.3 The hypot functions (p: 382) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.7.3 The hypot functions (p: 248) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.4.3 The hypot functions (p: 524) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.7.3 The hypot functions (p: 229) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.4.3 The hypot functions (p: 461) </li>
</ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="pow" title="c/numeric/math/pow"> <span class="t-lines"><span>pow</span><span>powf</span><span>powl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes a number raised to the given power (\(\small{x^y}\)x<sup>y</sup>) <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="sqrt" title="c/numeric/math/sqrt"> <span class="t-lines"><span>sqrt</span><span>sqrtf</span><span>sqrtl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes square root (\(\small{\sqrt{x} }\)<span class="t-mrad"><span>√</span><span>x</span></span>) <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="cbrt" title="c/numeric/math/cbrt"> <span class="t-lines"><span>cbrt</span><span>cbrtf</span><span>cbrtl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes cube root (\(\small{\sqrt[3]{x} }\)<span class="t-mrad"><span>3</span><span>√</span><span>x</span></span>) <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../complex/cabs" title="c/numeric/complex/cabs"> <span class="t-lines"><span>cabs</span><span>cabsf</span><span>cabsl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes the magnitude of a complex number <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/hypot" title="cpp/numeric/math/hypot">C++ documentation</a></span> for <code>hypot</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/numeric/math/hypot" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/hypot</a>
  </p>
</div>