{"id":1203,"date":"2015-02-08T00:01:29","date_gmt":"2015-02-08T08:01:29","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1203"},"modified":"2015-01-31T09:56:32","modified_gmt":"2015-01-31T17:56:32","slug":"round-numbers-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1203","title":{"rendered":"Round Numbers &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1185\">Exercise<\/a> required you to create three rounding functions: <em>roundup()<\/em>, <em>rounddown()<\/em>, and <em>roundhalf()<\/em>. Each one deals with a floating-point value in a specific way, but they all handle the basic rounding chore by typecasting a floating point variable to an integer.<br \/>\n<!--more--><br \/>\n<a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/01\/02exercise.c\">Click here<\/a> to view my solution. Your solution can be subtly different, as long as it yields the same results.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Value v1 = 1234.567871\r\nValue v2 = 98765.429688\r\nRounded up:\t1235.000000\t98766.000000\r\nRounded down:\t1234.000000\t98765.000000\r\nRounded half:\t1235.000000\t98765.000000<\/code><\/pre>\n<p>For my approach with the <em>roundup()<\/em> function, I typecast the function&#8217;s variable <code>n<\/code> as an integer at Line 7. That value is increased by one (to round &#8220;up&#8221;), and returned as a <em>float<\/em> in Line 8:<\/p>\n<pre><code>    r = (int)n;\r\n    return( (float)(r+1) );<\/code><\/pre>\n<p>I could have split that process up into several lines, or I could have scrunched it all up into one. However it&#8217;s done, the result is the same.<\/p>\n<p>In the <em>rounddown()<\/em> function, the integer typecast for variable <code>n<\/code> is simply returned:<\/p>\n<pre><code>    r = (int)n;\r\n    return( (float)r );<\/code><\/pre>\n<p>Another way to do this, which is obfuscated but it works, is this:<\/p>\n<pre><code>    return( (float)((int)n) );<\/code><\/pre>\n<p>The first typecast, <code>(int)n<\/code>, must be enclosed in parentheses or the compiler gets all testy.<\/p>\n<p>Finally, for the <em>roundhalf()<\/em> function, I inserted the typecasting technique for rounding down in Lines 24 and 25:<\/p>\n<pre><code>    r = (int)n;\r\n    t = (float)r;<\/code><\/pre>\n<p>The purpose here is to get the round-down value of variable <code>n<\/code> into variable <code>t<\/code>. An alternative would have been to use the <em>rounddown()<\/em> function directly:<\/p>\n<pre><code>    t = rounddown(n);<\/code><\/pre>\n<p>Once you have that lower value in variable <code>t<\/code>, a simple comparison with 0.5 determines whether to round up or round down, and return the appropriate value:<\/p>\n<pre><code>    if( n - t < 0.5)\r\n        return(t);\r\n    else\r\n        return(t+1);<\/code><\/pre>\n<p>In my rendition of the <em>roundhalf()<\/em> function, any value 0.5 or greater is always rounded up. You could modify the code to specifically deal with the exact value 0.5, determining when and how it gets rounded. I'll leave that chore up to you to experiment with on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise required you to create three rounding functions: roundup(), rounddown(), and roundhalf(). Each one deals with a floating-point value in a specific way, but they all handle the basic rounding chore by typecasting a floating point variable to &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1203\">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":[5],"tags":[],"class_list":["post-1203","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1203","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=1203"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1203\/revisions"}],"predecessor-version":[{"id":1213,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1203\/revisions\/1213"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}