{"id":6933,"date":"2025-05-03T00:01:20","date_gmt":"2025-05-03T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6933"},"modified":"2025-06-03T21:52:21","modified_gmt":"2025-06-04T04:52:21","slug":"the-sqrt-1-0","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6933","title":{"rendered":"The <em>sqrt(-1.0)<\/em>"},"content":{"rendered":"<p>The four-letter word that made me avoid computers and programming for far too long is <em>math<\/em>.<br \/>\n<!--more--><br \/>\nI&#8217;m not good at math. Or I wasn&#8217;t good. But then I realized that computer programming isn&#8217;t just another complicated way to frustrate oneself with math. It&#8217;s different! This difference doesn&#8217;t mean, however, that programming and math never intersect. A case in point is the square root of -1.<\/p>\n<p>A square root is the inverse of a square. So 2<sup>2<\/sup> = 4. And &radic;4 = 2, which can also be written as 4<sup>&#189;<\/sup> = 2.<\/p>\n<p>But what about -4? Which value when multiplied by itself equals -4? It can&#8217;t be -2, because -2 &times; -2 = 4; a negative value multiplied by a negative value is a positive value. Also, -2 &times; 2 are two different values.<\/p>\n<p>To avoid killing each other, mathematicians devised the constant <em>i<\/em>, the imaginary unit. This value is defined as the square root of -1, or &radic;-1. So, &radic;-4 = 2<em>i<\/em>. In the end, all the math works out well and the lives of thousands of mathematicians have been spared.<\/p>\n<p>But what about your C programs?<\/p>\n<p>Consider the following:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_03-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_05_03-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\r\nint main()\r\n{\r\n    double i;\r\n\r\n    i = sqrt((double)-1.0);\r\n    printf(\"i = %f\\n\",i);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code attempts to find the square root of -1.0. The <em>sqrt()<\/em> function is called, which means you must link in the math library, <code>-lm<\/code> when compiling this code at the command prompt in Linux.<\/p>\n<p>A <em>printf()<\/em> statement outputs the result, stored in variable <code>i<\/code>:<\/p>\n<p><code>i = -nan<\/code><\/p>\n<p>The value of variable <code>i<\/code> is <code>-nan<\/code>, or negative &#8220;not a number.&#8221; Darn.<\/p>\n<p>Not being one to give up, I tried a second approach with this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_03-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_05_03-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\r\nint main()\r\n{\r\n    double i,x;\r\n\r\n    x = pow( sqrt((double)-1.0),(double)2.0);\r\n    printf(\"i^2 = %f\\n\",x);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The goal of this code is to raise -1 to the second power, to square -1. The result should be 1. Can the computer handle it?<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>i^2 = -nan<\/code><\/p>\n<p>Nope. Again, the result is negative not-a-number.<\/p>\n<p>In both cases, the computer is doing exactly what it&#8217;s told. As floating point values are (mostly) accurate, the result can&#8217;t be defined, so a <code>nan<\/code> (not a number) exception is thrown. This condition doesn&#8217;t mean that all hope is lost.<\/p>\n<p>It&#8217;s entirely possible for the C compiler to handle complex numbers. I explain how in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6948\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Can the C compiler calculate the square root of -1? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6933\">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-6933","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\/6933","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=6933"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6933\/revisions"}],"predecessor-version":[{"id":7023,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6933\/revisions\/7023"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}