{"id":3015,"date":"2018-03-17T00:01:02","date_gmt":"2018-03-17T07:01:02","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3015"},"modified":"2018-03-10T10:32:12","modified_gmt":"2018-03-10T18:32:12","slug":"when-there-is-no","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3015","title":{"rendered":"When There is No &pi;"},"content":{"rendered":"<p>Reader Chris pointed out a fun and common C language inconsistency in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2995\">last week&#8217;s Lesson<\/a>: The constant <code>M_PI<\/code> isn&#8217;t always available. It&#8217;s not defined as part of the C standard, which means your compiler may lack this handy shortcut, located in the <code>math.h<\/code> header file. What to do, what to do?<br \/>\n<!--more--><br \/>\nOne of the problems with &#8220;standards&#8221; in the computer industry is that they are anything but standards. Some exist historically, but are never officially declared. And then you have Microsoft, which declares industry standards that only Microsoft adheres to. It&#8217;s a mess.<\/p>\n<p>With C, the primary standard is ANSI. The most recent versions are C99 (1999) and C11 (2011). The GNU standard also exists, implemented in the ubiquitous <em>gcc<\/em> compiler. Going back further is the K&#038;R standard, based on C as documented in Kernighan and Ritchie&#8217;s book, <em>The C Programming Language<\/em>, aka &#8220;the manual.&#8221;<\/p>\n<p>In the GNU standard, many constants are <a href=\"http:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Mathematical-Constants.html\" rel=\"noopener\" target=\"_blank\">declared<\/a> in the <code>math.h<\/code> header file, including <code>M_E<\/code>, <code>M_LOG10E<\/code>, <code>M_SQRT2<\/code>, and <code>M_PI<\/code>.<\/p>\n<p>The C99 standard doesn&#8217;t specify <code>M_PI<\/code>, neither does C11. Its absence doesn&#8217;t imply that it&#8217;s illegal to use the <code>M_PI<\/code> constant, just that a programmer shouldn&#8217;t assume that <code>M_PI<\/code> is available in all compilers.<\/p>\n<p>To resolve this issue in your code, add the following preprocessor instructions in addition to including the <code>math.h<\/code> header file:<\/p>\n<pre class=\"screen\">\r\n#ifndef M_PI\r\n#define M_PI (3.14159265358979323846264338327950288)\r\n#endif <span class=\"comments\">\/* M_PI *\/<\/span><\/pre>\n<p>The <code>#ifndef<\/code> preprocessor directive resolves as TRUE when <code>M_PI<\/code> is unavailable. If so, the next line defines the constant. The <code>#endif<\/code> directive closes the statements. When <code>M_PI<\/code> is defined, the other two statements are skipped. (<a href=\"http:\/\/c-for-dummies.com\/blog\/?page_id=2\">Click here<\/a> to read more about preprocessor directives.)<\/p>\n<p>It doesn&#8217;t hurt to add this type of checking for any constant you use, especially for code you plan on releasing into the wild.<\/p>\n<p>Another problem exists when functions are available in some libraries but not others. For example, the <em>random()<\/em> function is available in <em>clang<\/em>, but not in <em>gcc<\/em> (at least the last time I checked). If you&#8217;re practicing on your own, overlooking what&#8217;s standard and what&#8217;s not is okay. But when you must write a program and distribute its source, ensure that you check for all possibilities, confirm what&#8217;s standard and what&#8217;s not, and provide resolutions in your code for what&#8217;s unavailable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to deal with situations where your compiler may lack a constant available to other C compilers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3015\">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-3015","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\/3015","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=3015"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3015\/revisions"}],"predecessor-version":[{"id":3020,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3015\/revisions\/3020"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}