{"id":2649,"date":"2017-08-19T00:01:09","date_gmt":"2017-08-19T07:01:09","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2649"},"modified":"2017-08-12T09:44:35","modified_gmt":"2017-08-12T16:44:35","slug":"the-fmod-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2649","title":{"rendered":"The <em>fmod()<\/em> Function"},"content":{"rendered":"<p>The <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2577\">Change Due Exercise<\/a>, presented earlier this year, offered a solution that uses the <em>fmod()<\/em> function. That function is to real numbers as the modulus operator is to integers, though it&#8217;s not a straight-across comparison. That&#8217;s because floating point numbers can be imprecise.<br \/>\n<!--more--><br \/>\nAs a review, the modulus operator <code>%<\/code> works with two integer values:<\/p>\n<p><code>m = a % b;<\/code><\/p>\n<p>Variable <code>m<\/code> is assigned the remainder of variable <code>a<\/code> divided by variable <code>b<\/code>. So <code>5 % 3<\/code> evaluates to 2.<\/p>\n<p>For floating-point values, the <em>fmod()<\/em> function calculates the remainder of two values. Here&#8217;s the format:<\/p>\n<p><code>fmod(a,b)<\/code><\/p>\n<p>The <em>fmod()<\/em> function returns the remainder for variable <code>a<\/code> divided by variable <code>b<\/code>. Both <code>a<\/code> and <code>b<\/code> are <em>float<\/em> or <em>double<\/em> values, as is the value returned. The <em>fmod()<\/em> function requires that the <code>math.h<\/code> header be included.<\/p>\n<p>In the Change Due Exercise, <em>fmod()<\/em> is presented as an alternative solution to truncate a floating-point value to 2 digits. To truncate, the second operator must be a power of 10; 0.01 truncates a value to two digits after the decimal. (0.01 is 10<sup>-2<\/sup>.) Then the result is subtracted from the original value for the Exercise&#8217;s solution.<\/p>\n<p>The following code demonstrates how <em>fmod()<\/em> could be used to truncate a value to the second digit after the decimal:<\/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 a=123.45678;\r\n    float r;\r\n\r\n    r = fmod(a,0.01);\r\n    printf(\"The remainder of %f and %f is %f\\n\",\r\n            a,\r\n            0.01,\r\n            r);\r\n    printf(\"%f is truncated to %f\\n\",\r\n            a,\r\n            a-r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The remainder of 123.456779 and 0.010000 is 0.006779\r\n123.456779 is truncated to 123.449997<\/code><\/pre>\n<p>Effectively, the result is 123.45. The value 123.449997 is within the degrees of precision, though it looks ugly when displayed.<\/p>\n<p>The following code plows through powers of 10 from 0.00001 (10<sup>-5<\/sup>) to 10 (10<sup>1<\/sup>), displaying the result of <em>fmod()<\/em> on the value 123.45678:<\/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 a=123.45678;\r\n    float m=0.0001;\r\n    \r\n    for(m=0.00001;m&lt;100;m*=10)\r\n    {       \r\n        printf(\"The remainder of %f and %f is %f\\n\",\r\n                a,\r\n                m,\r\n                fmod(a,m));\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop in Line 9 might get to 100 because of floating point precision, though it may not loop that high on your computer. Here&#8217;s the output:<\/p>\n<pre><code>The remainder of 123.456779 and 0.000010 is 0.000003\r\nThe remainder of 123.456779 and 0.000100 is 0.000083\r\nThe remainder of 123.456779 and 0.001000 is 0.000788\r\nThe remainder of 123.456779 and 0.010000 is 0.006782\r\nThe remainder of 123.456779 and 0.100000 is 0.056787\r\nThe remainder of 123.456779 and 1.000000 is 0.456787\r\nThe remainder of 123.456779 and 9.999999 is 3.456791\r\nThe remainder of 123.456779 and 99.999992 is 23.456787<\/code><\/pre>\n<p>The precision monster appears in the first line of output, showing 123.45678 as &#8220;123.456779.&#8221; The results are likewise fuzzy. For most mathematical operations, such results are tolerable, but for counting change at the cash register you might want to think about using integer values instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The modulus operator (<code>%<\/code>) works on integer values. For real numbers, use the <em>fmod()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2649\">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-2649","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\/2649","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=2649"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2649\/revisions"}],"predecessor-version":[{"id":2660,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2649\/revisions\/2660"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}