{"id":3110,"date":"2018-05-26T00:01:40","date_gmt":"2018-05-26T07:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3110"},"modified":"2018-05-19T10:55:55","modified_gmt":"2018-05-19T17:55:55","slug":"radians-to-the-rescue","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3110","title":{"rendered":"Radians to the Rescue!"},"content":{"rendered":"<p>The word <em>radian<\/em> comes from radius, which is half the diameter of a circle. If you take a radius and create an arc along the circle of the same length, you get one radian.<\/p>\n<p>Okay. So what?<br \/>\n<!--more--><br \/>\nFirst, to understand the radian, gander at Figure 1. You see how the radius of the circle on the left is mapped to an arc on the circle. This length is one radian, <em>r<\/em>.<\/p>\n<div id=\"attachment_3114\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3114\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure1_radians.png\" alt=\"\" width=\"550\" height=\"310\" class=\"size-full wp-image-3114\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure1_radians.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure1_radians-300x169.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure1_radians-500x282.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-3114\" class=\"wp-caption-text\">Figure 1. The relationship between radians and degrees.<\/p><\/div>\n<p>The radian is related to &pi;, which is the ratio of the circle&#8217;s diameter to its circumference. As the radius is half the diameter, its value is related to &pi;, as you see in the measurements in the circle on the left: For the entire circle, the radian wraps around the circumference 6.2831 times, which is 2&pi;. This number makes sense because the circle&#8217;s diameter wraps around the circumference 3.1415 times, which is 1&pi;.<\/p>\n<p>Degrees, shown on the right in Figure 1, were invented to divide a circle into handy portions. No one knows who came up with the idea, but the Babylonians used a base 60 (sexagesimal) numbering system, so they get the credit. Further, 360 is a number than can be divided by many other numbers and you still get a whole number answer.<\/p>\n<p>Second, why do computers use radians and not degrees?<\/p>\n<p>Consider the triangles shown in Figure 2.<\/p>\n<div id=\"attachment_3144\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3144\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure2_triangle.png\" alt=\"\" width=\"550\" height=\"238\" class=\"size-full wp-image-3144\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure2_triangle.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure2_triangle-300x130.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0526-figure2_triangle-500x216.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-3144\" class=\"wp-caption-text\">Figure 2. How a triangle looks when measured with radians versus degrees.<\/p><\/div>\n<p>On the right you see a triangle measured in degrees, which you probably studied in school, the common 30-60-90 triangle. On the left is the same triangle, but with measurements in radians. 30&deg; is &pi;\/6 radians, 60&deg; is &pi;\/3 radians, and 90&deg; is &pi;\/2 radians. As you might suspect, computers find these values far easier to deal with than degrees.<\/p>\n<p>Oh, and 45&deg; is &pi;\/4 radians.<\/p>\n<p>It&#8217;s not critical to know the specific decimal values of the &pi; ratios when dealing with radians. Instead, as a programmer, you just need to know the translation constants:<\/p>\n<p>To convert degrees to radians, multiply degrees by 0.0174532925. This value is 2&pi;\/360.<\/p>\n<p>To convert radians to degrees, multiply radians by 57.2957795. This value is 360\/2&pi;.<\/p>\n<p>The following code displays common angles as radians and degrees.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define PI 3.1415926\r\n\r\nint main()\r\n{\r\n    float degree,radian;\r\n\r\n    for(degree=0.0;degree&lt;=360.0;degree+=60.0)\r\n        printf(\"%.f degrees = %.4f radians\\n\",\r\n                degree,\r\n                degree*0.0174533\r\n              );\r\n\r\n    for(radian=0.0;radian&lt;=PI*2;radian+=(PI\/3.0))\r\n        printf(\"%.4f radians = %.f degrees\\n\",\r\n                radian,\r\n                radian*57.2958\r\n              );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Because of the way the loops work, I&#8217;m not able to display &pi; ratios in the code, but you can see the values of &pi; in the output:<\/p>\n<pre><code>0 degrees = 0.0000 radians\r\n60 degrees = 1.0472 radians\r\n120 degrees = 2.0944 radians\r\n180 degrees = 3.1416 radians\r\n240 degrees = 4.1888 radians\r\n300 degrees = 5.2360 radians\r\n360 degrees = 6.2832 radians\r\n0.0000 radians = 0 degrees\r\n1.0472 radians = 60 degrees\r\n2.0944 radians = 120 degrees\r\n3.1416 radians = 180 degrees\r\n4.1888 radians = 240 degrees\r\n5.2360 radians = 300 degrees\r\n6.2832 radians = 360 degrees<\/code><\/pre>\n<p>Back to the code from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3100\">last week&#8217;s Lesson<\/a>, here&#8217;s the proper way to do the math with radians instead of degrees:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#define DEG2RAD 0.0174532925\r\n\r\nint main()\r\n{\r\n    printf(\"The sine of 45 degrees is %f\\n\",sin(45*DEG2RAD));\r\n    printf(\"The cosine of 45 degrees is %f\\n\",cos(45*DEG2RAD));\r\n    printf(\"The tan of 45 degrees is %f\\n\",tan(45*DEG2RAD));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the code, I use the <code>DEG2RAD<\/code> constant expression to modify 45 from a degree value to radians.<\/p>\n<p>Here&#8217;s the proper output:<\/p>\n<pre><code>The sine of 45 degrees is 0.707107\r\nThe cosine of 45 degrees is 0.707107\r\nThe tan of 45 degrees is 1.000000<\/code><\/pre>\n<p>That looks better!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why the radian is a better tool for measuring a circle than the degree. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3110\">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-3110","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\/3110","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=3110"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3110\/revisions"}],"predecessor-version":[{"id":3145,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3110\/revisions\/3145"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}