{"id":4139,"date":"2020-05-23T00:01:02","date_gmt":"2020-05-23T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4139"},"modified":"2020-05-27T17:06:29","modified_gmt":"2020-05-28T00:06:29","slug":"degrees-to-radians-to-degrees","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4139","title":{"rendered":"Degrees to Radians to Degrees"},"content":{"rendered":"<p>The C language uses radians instead of degrees when calculating angles. Humans should use radians as well, as they&#8217;re logical and easy to work with (radians, not humans). What surprises me, however, is that the C library lacks a defined constant for making the degree-radian conversion.<br \/>\n<!--more--><br \/>\nYou can bone on up radians with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3110\">this blog post<\/a> from two years ago. This information won&#8217;t help you do the conversions in C, not as much as having a defined constant would.<\/p>\n<p>For example, the <code>math.h<\/code> header file defines the <code>M_PI<\/code> constant, which represents the value of &pi;. Just include <code>math.h<\/code> in your source code, and you can use <code>M_PI<\/code> to represent &pi; without having to rely upon your Mensa friend who is odious but has memorized &pi; out to the 250<sup>th<\/sup> digit.<\/p>\n<p>For the conversion between radians and degrees, however, you must rely upon written documentation &mdash; or that same odious nerd &mdash; to know these two formulas:<\/p>\n<p>Radians \/ 0.017453 = degrees<\/p>\n<p>Degrees \/ 57.29578 = radians<\/p>\n<p>So when Joe User types 180 degrees into your trigonometric program, you must convert this value into radians to use the various C library trig functions:<\/p>\n<p><code>radians = joe_user_degree_input \/ 57.29578;<\/code><\/p>\n<p>My argument is that it would be easier to plug in a defined constant, which is oddly missing from the <code>math.h<\/code> header. So why not create your own?<\/p>\n<p>The following code uses the <code>M_PI<\/code> constant from <code>math.h<\/code> to calculate two new defined constants, <code>RAD2DEG<\/code> for converting radians to degrees, and <code>DEG2RAD<\/code> for converting degrees to radians.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_05_23-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_05_23-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#ifndef M_PI\r\n#define M_PI 3.141592\r\n#endif\r\n#define RAD2DEG M_PI\/360.0*2.0\r\n#define DEG2RAD 360.0\/M_PI\/2.0\r\n\r\nint main()\r\n{\r\n    printf(\"Convert radians to degrees: %f\\n\",RAD2DEG);\r\n    printf(\"Convert degrees to radians: %f\\n\",DEG2RAD);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 4 through 6 ensure that the <code>M_PI<\/code> defined constant exists. If not, Line 5 defines it as a single-precision value &mdash; good enough for most common trig problems.<\/p>\n<p>Line 7 declares the <code>RAD2DEG<\/code> defined constant, and Line 8 declares the <code>DEG2RAD<\/code> constant.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>Convert radians to degrees: 0.017453<br \/>\nConvert degrees to radians: 57.295780<\/code><\/p>\n<p>Yes, it probably would be easier to just declare both defined constants like this:<\/p>\n<p><code>#define RAD2DEG 0.017453<br \/>\n#define DEG2RAD 57.295780<\/code><\/p>\n<p>This approach is cleaner, and it&#8217;s how the <code>math.h<\/code> header file defines other mathematical constants; values are specified directly. Still, if somehow the value of &pi; changes in the universe, the approach shown in the sample code remains valid. You never want to take a chance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It surprises me constants aren&#8217;t available for these conversions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4139\">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-4139","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\/4139","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=4139"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4139\/revisions"}],"predecessor-version":[{"id":4186,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4139\/revisions\/4186"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}