summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fabs.html
blob: 8564aeffc48f9f395a90cd6e6adbcfbf9a66bb57 (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
    <h1 id="firstHeading" class="firstHeading">abs, labs, llabs, imaxabs</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int        abs( int n );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">long       labs( long n );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long long llabs( long long n );</pre>
</td> <td class="t-dcl-nopad"> </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;inttypes.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">intmax_t imaxabs( intmax_t n );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>Computes the absolute value of an integer number. The behavior is undefined if the result cannot be represented by the return type.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> n </td> <td> - </td> <td> integer value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>The absolute value of <code>n</code> (i.e. <code>|n|</code>), if it is representable.</p>
<h3 id="Notes"> Notes</h3> <p>In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement type <code>int</code>, <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code> is <code>-2147483648</code>, but the would-be result <code>2147483648</code> is greater than <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code>, which is <code>2147483647</code>.</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;stdlib.h&gt;
#include &lt;limits.h&gt;
 
int main(void)
{
    printf("abs(+3) = %d\n", abs(+3));
    printf("abs(-3) = %d\n", abs(-3));
 
//  printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">abs(+3) = 3
abs(-3) = 3</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.8.2.1 The imaxabs function (p: 159) </li>
<li> 7.22.6.1 The abs, labs and llabs functions (p: 259) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.8.2.1 The imaxabs function (p: 218) </li>
<li> 7.22.6.1 The abs, labs and llabs functions (p: 356) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.8.2.1 The imaxabs function (p: 199-200) </li>
<li> 7.20.6.1 The abs, labs and llabs functions (p: 320) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 4.10.6.1 The abs function </li>
<li> 4.10.6.3 The labs function </li>
</ul>
</ul>                   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fabs" title="c/numeric/math/fabs"> <span class="t-lines"><span>fabs</span><span>fabsf</span><span>fabsl</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 absolute value of a floating-point value (\(\small{|x|}\)|x|) <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/abs" title="cpp/numeric/math/abs">C++ documentation</a></span> for <code>abs</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/abs" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/abs</a>
  </p>
</div>