summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Ffdim.html
blob: 4d64f3ac8ff9fa629635566192e819855f1f5a49 (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
    <h1 id="firstHeading" class="firstHeading">fdim, fdimf, fdiml</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       fdimf( 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      fdim( 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 fdiml( 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 fdim( 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> Returns the positive difference between <code>x</code> and <code>y</code>, that is, if <code>x&gt;y</code>, returns <code>x-y</code>, otherwise (if <code>x≤y</code>), returns +0.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If any argument has type <code>long double</code>, <code>fdiml</code> is called. Otherwise, if any argument has integer type or has type <code>double</code>, <code>fdim</code> is called. Otherwise, <code>fdimf</code> is called.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x, y </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If successful, returns the positive difference between x and y.</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 value (after rounding) is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <code>math_errhandling</code>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul><li> If either argument is NaN, NaN is returned </li></ul> <h3 id="Notes"> Notes</h3> <p>Equivalent to <code><a href="http://en.cppreference.com/w/c/numeric/math/fmax"><span class="kw651">fmax</span></a><span class="br0">(</span>x<span class="sy2">-</span>y, <span class="nu0">0</span><span class="br0">)</span></code> except for the NaN handling requirements.</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)
{
    printf("fdim(4, 1) = %f, fdim(1, 4)=%f\n", fdim(4,1), fdim(1,4));
    printf("fdim(4,-1) = %f, fdim(1,-4)=%f\n", fdim(4,-1), fdim(1,-4));
    //error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fdim(1e308, -1e308) = %f\n", fdim(1e308, -1e308));
    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">fdim(4, 1) = 3.000000, fdim(1, 4)=0.000000
fdim(4,-1) = 5.000000, fdim(1,-4)=5.000000
fdim(1e308, -1e308) = 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.12.1 The fdim functions (p: 187-188) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.9.1 The fdim functions (p: 386) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.12.1 The fdim functions (p: 257) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.9.1 The fdim functions (p: 530) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.12.1 The fdim functions (p: 238) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.9.1 The fdim functions (p: 466) </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="fmax" title="c/numeric/math/fmax"> <span class="t-lines"><span>fmax</span><span>fmaxf</span><span>fmaxl</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> determines larger of two floating-point values <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/fdim" title="cpp/numeric/math/fdim">C++ documentation</a></span> for <code>fdim</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/fdim" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/fdim</a>
  </p>
</div>