{"id":1185,"date":"2015-02-01T00:01:53","date_gmt":"2015-02-01T08:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1185"},"modified":"2015-02-08T07:56:38","modified_gmt":"2015-02-08T15:56:38","slug":"round-numbers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1185","title":{"rendered":"Round Numbers"},"content":{"rendered":"<p>The C library holds functions that help you round-off floating point values. I can name a few, but then it would make this month&#8217;s rounding exercise too easy. So I won&#8217;t!<br \/>\n<!--more--><br \/>\nRounding is a science. It involves three approaches:<\/p>\n<ul>\n<li>To <strong>round up<\/strong>, you simply take any fraction over a integer and jump to the next highest integer.<\/li>\n<li>To <strong>round down<\/strong>, you take any fraction over a integer and ignore it.<\/li>\n<li>To <strong>round halfsies<\/strong> (my term) you evaluate the fractional part and if it&#8217;s under or over 0.5 you round up or down, respectively.<\/li>\n<\/ul>\n<p>The issue of how to deal with 0.5 exactly is a source of debate, a topic I don&#8217;t want to get into.<\/p>\n<p>For this month&#8217;s Exercise, you are to craft your own rounding functions. Two floating point values are given in the code skeleton, shown below. Your job is to round those values straight up, straight down, or split the rounding based on the fractional part of the number. You make that happen by filling in code for the <em>roundup()<\/em>, <em>rounddown()<\/em>, and <em>roundhalf()<\/em> functions.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nfloat roundup(float n)\r\n{\r\n}\r\n\r\nfloat rounddown(float n)\r\n{\r\n}\r\n\r\nfloat roundhalf(float n)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    float v1,v2;\r\n\r\n    v1 = 1234.56789;\r\n    v2 = 98765.4321;\r\n\r\n    printf(\"Value v1 = %f\\n\",v1);\r\n    printf(\"Value v2 = %f\\n\",v2);\r\n    printf(\"Rounded up:\\t%f\\t%f\\n\",\r\n            roundup(v1),\r\n            roundup(v2));\r\n    printf(\"Rounded down:\\t%f\\t%f\\n\",\r\n            rounddown(v1),\r\n            rounddown(v2));\r\n    printf(\"Rounded half:\\t%f\\t%f\\n\",\r\n            roundhalf(v1),\r\n            roundhalf(v2));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a major hint, which I&#8217;m providing gratis because I&#8217;m forbidding you from using any existing C library functions to handle the rounding process: You can easily lop off the fractional part of a floating point variable by typecasting it to an <em>int<\/em>.<\/p>\n<p>Also, once you figure out the <em>roundup()<\/em> function, doing <em>rounddown()<\/em> is a snap. And then doing the <em>roundhalf()<\/em> function is equally as easy.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1203\">Click here<\/a> to view my solution, although I recommend that you first try to solve the puzzle on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Round up. Round down. Round halfsies. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1185\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1185","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185","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=1185"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions\/1223"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}