{"id":6630,"date":"2024-10-26T00:01:43","date_gmt":"2024-10-26T07:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6630"},"modified":"2024-10-19T09:12:18","modified_gmt":"2024-10-19T16:12:18","slug":"seeing-whats-left-over-with-division","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6630","title":{"rendered":"Seeing What&#8217;s Left Over with Division"},"content":{"rendered":"<p>Learning division in school means long division. The process involves a quotient and a remainder. For example, 42&div;8 works out to 5 (quotient) with 2 remainder. On a computer, however, division renders the result as 5.25. So how do you get the remainder separated?<br \/>\n<!--more--><br \/>\nBefore rolling headlong into dreaded math, consider a review of terms as presented in Figure 1.<\/p>\n<div id=\"attachment_6632\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6632\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1026-figure1.png\" alt=\"\" width=\"550\" height=\"296\" class=\"size-full wp-image-6632\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1026-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1026-figure1-300x161.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1026-figure1-500x269.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-6632\" class=\"wp-caption-text\">Figure 1. Division and its jargon.<\/p><\/div>\n<p>With division, you have two values: The numerator or dividend, and the denominator or divisor. The numerator is on top. The denominator, like all demons, goes below. (That&#8217;s how I remember it.) The &#8220;gazinta&#8221; format for division is also shown in Figure 1 (item C). This is the form taught first in school, which is why the remainder becomes a thing.<\/p>\n<p>In programming, the <code>\/<\/code> operator handles division. In my C programming books, I recommend using <em>float<\/em> or <em>double<\/em> values to obtain the accurate results. When you use integers, the result is truncated.<\/p>\n<p>If you desire to discover the remainder, you can pull a few tricks. The first one is from a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2554\">blog post<\/a> on the <em>div()<\/em> function, which returns a structure containing the quotient and remainder of a division operation. When you just need to know the remainder, however, you can use the aptly-named <em>remainder()<\/em> function. Here&#8217;s its <em>man<\/em> page definition:<\/p>\n<p><a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/remainder.3.html\" rel=\"noopener\" target=\"_blank\"><code>double remainder(double x, double y)<\/code><\/a><\/p>\n<p>This function requires including the <code>math.h<\/code> header file. In Linux, you must specify the <code>-lm<\/code> switch when compiling to link in the math library.<\/p>\n<p>The following code demonstrates the <em>remainder()<\/em> function.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_10_26-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_10_26-Lesson.c<\/a><\/h3>\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    double dividend,divisor,r;\r\n\r\n    printf(\"Enter the dividend (numerator): \");\r\n    scanf(\"%lf\",&amp;dividend);\r\n    printf(\"Enter the divisor (denominator): \");\r\n    scanf(\"%lf\",&amp;divisor);\r\n\r\n    r = remainder(dividend,divisor);\r\n    printf(\"%.2f\/%.2f = %.2f with %.2f remainder\\n\",\r\n            dividend,\r\n            divisor,\r\n            dividend\/divisor,\r\n            r\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The function prompts for two <em>double<\/em> values, which are then thrust into the <em>remainder()<\/em> function:<\/p>\n<p><code>r = remainder(dividend,divisor);<\/code><\/p>\n<p>A <em>printf()<\/em> statement outputs the results, which include standard division to obtain the precise value. But the value of variable <code>r<\/code> is what&#8217;s left over &mdash; the remainder. Here&#8217;s a sample run:<\/p>\n<p><code>Enter the dividend (numerator): 87.0<br \/>\nEnter the divisor (denominator): 6.0<br \/>\n87.00\/6.00 = 14.50 with 3.00 remainder<\/code><\/p>\n<p>This test run threw me:<\/p>\n<p><code>Enter the dividend (numerator): 29.0<br \/>\nEnter the divisor (denominator): 3.0<br \/>\n29.00\/3.00 = 9.67 with -1.00 remainder<\/code><\/p>\n<p>The result of 29 &div; 3 is 9 with 2 remaining. But the -1.00 result is accurate; it&#8217;s the value 3 minus 1, which is 2. In fact, the documentation for the <em>remainder()<\/em> function explains the result like this:<\/p>\n<p><code>x-n*y<\/code><\/p>\n<p>Value <code>x<\/code> is the dividend and <code>y<\/code> is the divisor, with <code>n<\/code> as the result of <code>x\/y<\/code> rounded to the nearest integer. So:<\/p>\n<p><code>29-(29\/3)*3 = 29-(10)*3 = 29-30 = -1<\/code><\/p>\n<p>The -1.00 result makes sense given the parameters of the <em>remainder()<\/em> function.<\/p>\n<p>I still prefer the <em>div()<\/em> function to obtain a more realistic answer. But the <em>remainder()<\/em> function remains available, plus it works with real numbers and not just integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not every value divides evenly into another. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6630\">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-6630","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\/6630","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=6630"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6630\/revisions"}],"predecessor-version":[{"id":6639,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6630\/revisions\/6639"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}