{"id":6818,"date":"2025-02-15T00:01:40","date_gmt":"2025-02-15T08:01:40","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6818"},"modified":"2025-02-08T09:34:03","modified_gmt":"2025-02-08T17:34:03","slug":"splitting-a-decimal-value-2","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6818","title":{"rendered":"Splitting a Decimal Value"},"content":{"rendered":"<p>For whatever reason, it&#8217;s your desire to split a real number into its integer and fractional parts. Perhaps you&#8217;re angry with the value. Regardless, I can think of a few ways to perform this feat, but need not exercise a single brain cell in this effort as the <em>modf()<\/em> function performs the task automagically.<br \/>\n<!--more--><br \/>\nThe <em>modf()<\/em> function is defined in the <code>math.h<\/code> header file. It doesn&#8217;t require linking in the math library in Linux. Here is the function&#8217;s <em>man<\/em> page format:<\/p>\n<p><code>double modf(double x, double *iptr);<\/code><\/p>\n<p>Argument <code>x<\/code> is the <em>double<\/em> value for which you want to cleave its fractional and integer portions. Argument <code>iptr<\/code> is a <em>double<\/em> pointer to hold the integer portion of the result (expressed as a <em>double<\/em> value). The <em>double<\/em> value returned from the function is the fractional part. Yes, this function returns two values, using a pointer argument to hold the second value.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02_15-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_02_15-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 p,i;\r\n\r\n    p = modf(M_PI,&amp;i);\r\n\r\n    printf(\"%f is %f and %f\\n\",\r\n            M_PI,\r\n            i,\r\n            p\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>modf()<\/em> function examines the value <code>M_PI<\/code>, which is &pi; as defined in the <code>math.h<\/code> header file. The integer portion of the value is stored in <em>double<\/em> variable <code>i<\/code> (passed as an address, <code>&amp;i<\/code>), with the decimal portion returned in <em>double<\/em> variable <code>p<\/code>. A <em>printf()<\/em> statement outputs the original value and its two parts:<\/p>\n<p><code>3.141593 is 3.000000 and 0.141593<\/code><\/p>\n<p>As I wrote earlier, I could concoct a similar function myself, but why bother when it&#8217;s already in the library?<\/p>\n<p>Two companion functions handle <em>float<\/em> and <em>long double<\/em> values, <em>modff()<\/em> and <em>modfl()<\/em>, respecively. The arguments follow the same pattern, though their data types are <em>float<\/em> and <em>long double<\/em>.<\/p>\n<p>Other real number chop-chop functions include <em>frexp()<\/em>, <em>frexpf()<\/em>, and <em>frexpl()<\/em>. These seem to be the same as their <em>modf<\/em> counterparts, though they do require linking in the math library in Linux; use <code>-lm<\/code> at the command prompt or activate the math library in your IDE.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>modf()<\/em> function handles the job of splitting a decimal value into its integer and fractional portions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6818\">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-6818","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\/6818","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=6818"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6818\/revisions"}],"predecessor-version":[{"id":6837,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6818\/revisions\/6837"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}