summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fcomplex%2Fcmplx.html
blob: 12dde11f16789703c1aa963dbb93caa7184d2cc8 (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
    <h1 id="firstHeading" class="firstHeading">CMPLXF, CMPLX, CMPLXL</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;complex.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">float complex       CMPLXF( float real, float imag );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">double complex      CMPLX( double real, double imag );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">long double complex CMPLXL( long double real, long double imag );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <p>Each of these macros expands to an expression that evaluates to the value of the specified complex type, with the real part having the value of <code>real</code> (converted to the specified argument type) and the imaginary part having the value of <code>imag</code> (converted to the specified argument type)</p>
<p>The expressions are suitable for use as initializers for objects with static or thread storage duration, as long as the expressions <code>real</code> and <code>imag</code> are also suitable.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> real </td> <td> - </td> <td> the real part of the complex number to return </td>
</tr> <tr class="t-par"> <td> imag </td> <td> - </td> <td> the imaginary part of the complex number to return </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>A complex number composed of <code>real</code> and <code>imag</code> as the real and imaginary parts.</p>
<h3 id="Notes"> Notes</h3> <p>These macros are implemented as if the imaginary types are supported (even if they are otherwise not supported and <code><a href="imaginary_i" title="c/numeric/complex/Imaginary I">_Imaginary_I</a></code> is actually undefined) and as if defined as follows:</p>
<div class="c source-c"><pre data-language="c">#define CMPLX(x, y) ((double complex)((double)(x) + _Imaginary_I * (double)(y)))
#define CMPLXF(x, y) ((float complex)((float)(x) + _Imaginary_I * (float)(y)))
#define CMPLXL(x, y) ((long double complex)((long double)(x) + \
                      _Imaginary_I * (long double)(y)))</pre></div> <h3 id="Example"> Example</h3> <div class="t-example t-example-std-c11"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;complex.h&gt;
 
int main(void)
{
    double complex z = CMPLX(0.0, -0.0);
    printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">z = 0.0-0.0i</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.3.9.3 The CMPLX macros (p: 197) </li></ul>
</ul>   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="imaginary_i" title="c/numeric/complex/Imaginary I"> <span class="t-lines"><span>_Imaginary_I</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> the imaginary unit constant i <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/complex/complex" title="cpp/numeric/complex/complex">C++ documentation</a></span> for <code>complex</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/complex/CMPLX" class="_attribution-link">https://en.cppreference.com/w/c/numeric/complex/CMPLX</a>
  </p>
</div>