summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fsqrt.html
blob: 460db07c4e8e4503177f2e1ad124f1a201fb8cea (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
    <h1 id="firstHeading" class="firstHeading">sqrt, sqrtf, sqrtl</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       sqrtf( float arg );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">double      sqrt( double arg );</pre>
</td> <td> (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double sqrtl( long double arg );</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 sqrt( arg )</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 square root of <code>arg</code>.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If <code>arg</code> has type <code>long double</code>, <code>sqrtl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>sqrt</code> is called. Otherwise, <code>sqrtf</code> is called. If <code>arg</code> is complex or imaginary, then the macro invokes the corresponding complex function (<code><a href="http://en.cppreference.com/w/c/numeric/complex/csqrt"><span class="kw778">csqrtf</span></a></code>, <code><a href="http://en.cppreference.com/w/c/numeric/complex/csqrt"><span class="kw777">csqrt</span></a></code>, <code><a href="http://en.cppreference.com/w/c/numeric/complex/csqrt"><span class="kw779">csqrtl</span></a></code>).</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> arg </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, square root of <code>arg</code> (\({\small \sqrt{arg} }\)<span class="t-mrad"><span>√</span><span>arg</span></span>), is returned.</p>
<p>If a domain error occurs, an implementation-defined value is returned (NaN where supported).</p>
<p>If a range error occurs due to underflow, 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>Domain error occurs if <code>arg</code> is less than zero.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If the argument is less than -0, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised and NaN is returned. </li>
<li> If the argument is +∞ or ±0, it is returned, unmodified. </li>
<li> If the argument is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p><code>sqrt</code> is required by the IEEE standard to be correctly rounded from the infinitely precise result. In particular, the exact result is produced if it can be represented in the floating-point type. The only other operations which require this are the <a href="../../language/operator_arithmetic" title="c/language/operator arithmetic">arithmetic operators</a> and the function <code><a href="fma" title="c/numeric/math/fma">fma</a></code>. Other functions, including <code><a href="pow" title="c/numeric/math/pow">pow</a></code>, are not so constrained.</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;
 
#pragma STDC FENV_ACCESS ON
 
int main(void)
{
    // normal use
    printf("sqrt(100) = %f\n", sqrt(100));
    printf("sqrt(2) = %f\n", sqrt(2));
    printf("golden ratio = %f\n", (1+sqrt(5))/2);
    // special values
    printf("sqrt(-0) = %f\n", sqrt(-0.0));
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("sqrt(-1.0) = %f\n", sqrt(-1));
    if(errno == EDOM) perror("    errno == EDOM");
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID was raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">sqrt(100) = 10.000000
sqrt(2) = 1.414214
golden ratio = 1.618034
sqrt(-0) = -0.000000
sqrt(-1.0) = -nan
    errno = EDOM: Numerical argument out of domain
    FE_INVALID was raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.7.5 The sqrt functions (p: 249) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.4.5 The sqrt functions (p: 525) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.7.5 The sqrt functions (p: 229-230) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.4.5 The sqrt functions (p: 462) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.5.5.2 The sqrt function </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="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="hypot" title="c/numeric/math/hypot"> <span class="t-lines"><span>hypot</span><span>hypotf</span><span>hypotl</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 square root of the sum of the squares of two given numbers (\(\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>) <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../complex/csqrt" title="c/numeric/complex/csqrt"> <span class="t-lines"><span>csqrt</span><span>csqrtf</span><span>csqrtl</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 complex square root <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/sqrt" title="cpp/numeric/math/sqrt">C++ documentation</a></span> for <code>sqrt</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/sqrt" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/sqrt</a>
  </p>
</div>