{"id":6948,"date":"2025-05-10T00:01:02","date_gmt":"2025-05-10T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6948"},"modified":"2025-04-26T13:32:20","modified_gmt":"2025-04-26T20:32:20","slug":"numbers-complex-and-imaginary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6948","title":{"rendered":"Numbers Complex and Imaginary"},"content":{"rendered":"<p>Your C programs aren&#8217;t meant to suffer with an inability to handle values such as the square root of -1, the imaginary number <em>i<\/em>. No, you can easily manage such mathematical mysteries, making rare the possibility of a <code>-nan<\/code> result, as shown in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6933\">last week&#8217;s Lesson<\/a>.<br \/>\n<!--more--><br \/>\nI suppose somewhere at some time, a programmer desperately needed to calculate the &radic;-1 and then gleefully gave up when it was discovered that the computer itself was at a loss. Then along comes the C99 standard.<\/p>\n<p>The C99 standard introduced the possibility for programs to deal with complex numbers. A complex number consists of a real number plus an imaginary portion. This configuration happens often in mathematics, where the result of an operation includes the imaginary value, but also some other number. For example, 4.0 + 2<em>i<\/em>.<\/p>\n<p>For C programming, the C99 standard defines several things: the <code>I<\/code> constant representing the imaginary number <em>i<\/em>, a <em>complex<\/em> data type, complex number versions of common math functions, and the <code>complex.h<\/code> header to support all this fun stuff.<\/p>\n<p>By the way, big <code>I<\/code> is chosen to represent the imaginary number as little <code>i<\/code> is used to often in C code.<\/p>\n<p>The following code outputs the real and imaginary parts of complex constant <code>I<\/code>:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_10-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_05_10-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n#include &lt;complex.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"I = %f + %.0fi\\n\",creal(I),cimag(I));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Two functions are used to obtain the complex number real and imaginary parts, <em>creal()<\/em> and <em>cimag()<\/em> respectively. Both functions accept a single <em>complex<\/em> data type as their argument; they return a <em>double<\/em>. Above, the constant <code>I<\/code> represents the <em>complex<\/em> data type. Its value is output for both real and imaginary parts:<\/p>\n<p><code>I = 0.000000 + 1i<\/code><\/p>\n<p>The complex constant <code>I<\/code> has no real portion, no floating point value attached. It has only one <em>i<\/em>, which makes sense as <code>I<\/code> represents the complex number <em>i<\/em>.<\/p>\n<p>The following code calculates <em>i<\/em><sup>2<\/sup>. Complex constant <code>I<\/code> is multiplied by itself, with the result stored in <em>complex<\/em> variable <code>z<\/code>. The real and imaginary portions of <code>z<\/code> are then output.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_10-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_05_10-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n#include &lt;complex.h&gt;\r\n\r\nint main()\r\n{\r\n    double complex z;\r\n\r\n    z = I * I;\r\n    printf(\"I * I = %f + %.0fi\\n\",creal(z),cimag(z));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<p><code>I * I = -1.000000 + 0i<\/code><\/p>\n<p>The value of <em>i<\/em><sup>2<\/sup> is -1, with no imaginary portion. This result makes sense as (&radic;-1)<sup>2<\/sup> is -1.<\/p>\n<p>The following code calculates &radic;-4 and outputs the result:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_10-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2025_05_10-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n#include &lt;complex.h&gt;\r\n\r\nint main()\r\n{\r\n    double complex z;\r\n\r\n    z = csqrt(-4.0);\r\n    printf(\"sqrt(-4.0) = %f + %.0fi\\n\",creal(z),cimag(z));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>csqrt()<\/em> function is the complex version of the standard C library <em>sqrt()<\/em> function. Its argument is a <em>complex<\/em> data type, declared as a <em>complex<\/em> value, -4.0. The function returns a <em>complex<\/em> value, declared as <code>z<\/code> in the above code. The <em>printf()<\/em> statement outputs the real and imaginary parts of the result:<\/p>\n<p><code>sqrt(-4.0) = 0.000000 + 2i<\/code><\/p>\n<p>The square root of -4 is 2<em>i<\/em>.<\/p>\n<p>Additional complex math functions are available, including <em>cpow()<\/em> to obtain powers of complex values. To see the gamut of complex functions available, view the complex <em>man<\/em> page: <strong>man complex<\/strong><\/p>\n<p>Remember that in Linux, you must link in the math library to build some of these programs. At the command prompt, specify the <strong>-lm<\/strong> switch as the final argument.<\/p>\n<p>I could go on about these complex functions, but the topic frightens me. The documentation elsewhere on the Interwebs is rather weak and repetitive. Still, it&#8217;s a fun math thing that your C compiler can do, should you cross into those dimensions and time warps that require use of the complex number <em>i<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, it&#8217;s entirely possible for your C programs to hand a value like the square root of -1. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6948\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6948","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6948"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6948\/revisions"}],"predecessor-version":[{"id":6975,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6948\/revisions\/6975"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}