{"id":1198,"date":"2015-02-07T00:00:16","date_gmt":"2015-02-07T08:00:16","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1198"},"modified":"2015-01-31T09:52:56","modified_gmt":"2015-01-31T17:52:56","slug":"official-c-library-rounding-functions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1198","title":{"rendered":"Official C Library Rounding Functions"},"content":{"rendered":"<p>Nestled within the C Library are various functions mathematical, a handful of which are used to round floating point values. The most common of them are <em>ceil()<\/em>, <em>floor()<\/em>, and <em>rint()<\/em>.<br \/>\n<!--more--><br \/>\nThese three functions parallel those I asked you to create for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1185\">this month&#8217;s Exercise<\/a>, so I hope that you&#8217;ve already completed the exercise and are not reading this Lesson to cheat.<\/p>\n<p>All the rounding functions listed are declared in the <code>math.h<\/code> header file. Here&#8217;s how each works:<\/p>\n<p><em>ceil()<\/em> takes an floating point value, rounds it up to the next whole number (integer), and returns that value (as a floating point). This function&#8217;s name most likely comes from the word &#8220;ceiling.&#8221; Say &#8220;see ill&#8221; and you won&#8217;t forget how to spell it.<\/p>\n<p><em>floor()<\/em> takes a floating point value, rounds it down to the next whole number, and returns that value.<\/p>\n<p><em>rint()<\/em> is the &#8220;round integer&#8221; function. It takes a floating point value and rounds it up or down to the next integer. The man page claims that the function uses &#8220;the prevailing rounding mode&#8221; to determine whether to round up or down. I&#8217;ve no idea how that works.<\/p>\n<p>The following code is a modification of this month&#8217;s Exercise solution. It uses <em>ceil()<\/em>, <em>floor()<\/em>, and <em>rint()<\/em> to replace the <em>roundup()<\/em>, <em>rounddown()<\/em>, and <em>roundhalf()<\/em> functions required for that exercise:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\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            ceil(v1),\r\n            ceil(v2));\r\n    printf(\"Rounded down:\\t%f\\t%f\\n\",\r\n            floor(v1),\r\n            floor(v2));\r\n    printf(\"Rounded half:\\t%f\\t%f\\n\",\r\n            rint(v1),\r\n            rint(v2));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output from this code is identical to the solution for this month&#8217;s Exercise:<\/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","protected":false},"excerpt":{"rendered":"<p>Among them: <em>ciel()<\/em>, <em>floor()<\/em>, and <em>rint()<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1198\">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":[2],"tags":[],"class_list":["post-1198","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\/1198","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=1198"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1198\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1198\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}