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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<h1 id="firstHeading" class="firstHeading">fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><math.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float fabsf( 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 fabs( 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 fabsl( 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-dcl t-since-c23"> <td> <pre data-language="c">_Decimal32 fabsd32( _Decimal32 arg );</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">_Decimal64 fabsd64( _Decimal64 arg );</pre>
</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">_Decimal128 fabsd128( _Decimal128 arg );</pre>
</td> <td> (6) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><tgmath.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define fabs( arith )</pre>
</td> <td> (7) </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-6)</span> Computes the absolute value of a floating point value <code>arg</code>. <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
<td> <p>The functions with decimal floating point parameters are declared if and only if the implementation predefines <code>__STDC_IEC_60559_DFP__</code> (i.e. the implementation supports decimal floating point numbers).</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table>
</div> <div class="t-li1">
<span class="t-li">7)</span> Type-generic macro: If the argument has type <span class="t-rev-inl t-since-c23"><span><code>_Decimal128</code>, <code>_Decimal64</code>, <code>_Decimal32</code>,</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span><code>long double</code>, <code>double</code>, or <code>float</code>, <span class="t-rev-inl t-since-c23"><span><code>fabsd128</code>, <code>fabsd64</code>, <code>fabsd32</code>,</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span><code>fabsl</code>, <code>fabs</code>, or <code>fabsf</code> is called, respectively. Otherwise, if the argument has integer type, <code>fabs</code> is called. Otherwise, if the argument is complex, then the macro invokes the corresponding complex function (<code><a href="../complex/cabs" title="c/numeric/complex/cabs">cabsf</a></code>, <code><a href="../complex/cabs" title="c/numeric/complex/cabs">cabs</a></code>, <code><a href="../complex/cabs" title="c/numeric/complex/cabs">cabsl</a></code>). Otherwise, the behavior is undefined.</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> <tr class="t-par"> <td> arith </td> <td> - </td> <td> floating point or integer value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If successful, returns the absolute value of <code>arg</code> (\(\small |arg| \)|arg|). The value returned is exact and does not depend on any rounding modes.</p>
<h3 id="Error_handling"> Error handling</h3> <p>This function is not subject to any of the error conditions 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> If the argument is ±0, +0 is returned </li>
<li> If the argument is ±∞, +∞ is returned </li>
<li> If the argument is NaN, NaN is returned </li>
</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <math.h>
#include <stdio.h>
#define PI 3.14159
// This numerical integration assumes all area is positive.
double integrate(double f(double),
double a, double b, // assume a < b
unsigned steps) // assume steps > 0
{
const double dx = (b - a) / steps;
double sum = 0.0;
for (double x = a; x < b; x += dx)
sum += fabs(f(x));
return dx * sum;
}
int main(void)
{
printf("fabs(+3) = %f\n", fabs(+3.0));
printf("fabs(-3) = %f\n", fabs(-3.0));
// special values
printf("fabs(-0) = %f\n", fabs(-0.0));
printf("fabs(-Inf) = %f\n", fabs(-INFINITY));
printf("Area under sin(x) in [-PI, PI] = %f\n", integrate(sin, -PI, PI, 5101));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">fabs(+3) = 3.000000
fabs(-3) = 3.000000
fabs(-0) = 0.000000
fabs(-Inf) = inf
Area under sin(x) in [-PI, PI] = 4.000000</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 7.12.7.2 The fabs functions (p: TBD) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: TBD) </li>
<li> F.10.4.2 The fabs functions (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.7.2 The fabs functions (p: 181) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 272-273) </li>
<li> F.10.4.2 The fabs functions (p: 382) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.7.2 The fabs functions (p: 248) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.4.2 The fabs functions (p: 524) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.7.2 The fabs functions (p: 228-229) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.4.2 The fabs functions (p: 460) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.5.6.2 The fabs function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="abs" title="c/numeric/math/abs"> <span class="t-lines"><span>abs</span><span>labs</span><span>llabs</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes absolute value of an integral value (\(\small{|x|}\)|x|) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="copysign" title="c/numeric/math/copysign"> <span class="t-lines"><span>copysign</span><span>copysignf</span><span>copysignl</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> produces a value with the magnitude of a given value and the sign of another given value <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="signbit" title="c/numeric/math/signbit"> <span class="t-lines"><span>signbit</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> checks if the given number is negative <br> <span class="t-mark">(function macro)</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/fabs" title="cpp/numeric/math/fabs">C++ documentation</a></span> for <code>fabs</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© 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/fabs" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/fabs</a>
</p>
</div>
|