summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fcopysign.html
blob: 3e67f811cbe12097e1aed7c97ffd10797cb9a0eb (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
    <h1 id="firstHeading" class="firstHeading">copysign, copysignf, copysignl</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       copysignf( 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      copysign( 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 copysignl( 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 copysign(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> Composes a floating point value with the magnitude of <code>x</code> and the sign of <code>y</code>. </div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If any argument has type <code>long double</code>, <code>copysignl</code> is called. Otherwise, if any argument has integer type or has type <code>double</code>, <code>copysign</code> is called. Otherwise, <code>copysignf</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 values </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the floating point value with the magnitude of <code>x</code> and the sign of <code>y</code> is returned.</p>
<p>If <code>x</code> is NaN, then NaN with the sign of <code>y</code> is returned.</p>
<p>If <code>y</code> is -0, the result is only negative if the implementation supports the signed zero consistently in arithmetic operations.</p>
<h3 id="Error_handling"> Error handling</h3> <p>This function is not subject to any errors specified in <a href="math_errhandling" title="c/numeric/math/math errhandling">math_errhandling</a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul><li> The returned value is exact (<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised) and independent of the current <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">rounding mode</a>. </li></ul> <h3 id="Notes"> Notes</h3> <p><code>copysign</code> is the only portable way to manipulate the sign of a NaN value (to examine the sign of a NaN, <code><a href="signbit" title="c/numeric/math/signbit">signbit</a></code> may also be used).</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;
 
int main(void)
{
    printf("copysign(1.0,+2.0)      = %+.1f\n", copysign(1.0,+2.0));
    printf("copysign(1.0,-2.0)      = %+.1f\n", copysign(1.0,-2.0));
    printf("copysign(INFINITY,-2.0) = %f\n",    copysign(INFINITY,-2.0));
    printf("copysign(NAN,-2.0)      = %f\n",    copysign(NAN,-2.0));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">copysign(1.0,+2.0)      = +1.0
copysign(1.0,-2.0)      = -1.0
copysign(INFINITY,-2.0) = -inf
copysign(NAN,-2.0)      = -nan</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.11.1 The copysign functions (p: 255) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.8.1 The copysign functions (p: 529) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.11.1 The copysign functions (p: 236) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.8.1 The copysign functions (p: 465) </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="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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/copysign" title="cpp/numeric/math/copysign">C++ documentation</a></span> for <code>copysign</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/copysign" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/copysign</a>
  </p>
</div>