{"id":6806,"date":"2025-02-08T00:01:59","date_gmt":"2025-02-08T08:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6806"},"modified":"2025-02-01T12:39:13","modified_gmt":"2025-02-01T20:39:13","slug":"how-whacky-is-that-real-number","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6806","title":{"rendered":"How Whacky is that Real Number?"},"content":{"rendered":"<p>If you&#8217;ve tried your computer&#8217;s patience, you may have encountered some valid yet odd results when doing math. For example, you may see <code>NaN<\/code> output, which is computer-speak for &#8220;Not a number.&#8221; Or perhaps you&#8217;ve encountered <code>INF<\/code>, infinity. The C library offers a way to test for these results before they&#8217;re output.<br \/>\n<!--more--><br \/>\nThe <em>fpclassify()<\/em> function examines a floating point value or expression and returns an integer result based on the outcome. This function is declared in the <code>math.h<\/code> header file and has this <em>man<\/em> page format:<\/p>\n<p><code>int fpclassify(x);<\/code><\/p>\n<p>Argument <code>x<\/code> is a floating point expression. The integer value returned is represented by a defined constant that reports the result.<\/p>\n<p>The <em>fpclassify()<\/em> function is a POSIX function, not part of the standard C library. It&#8217;s also a macro, so be careful how you implement it.<\/p>\n<p>The following sample code demonstrates how to use the function, along with the defined constants available to interpret the result:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02_08-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_02_08-Lesson-a.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 a,b;\r\n    int r;\r\n\r\n    a = 1.0; b = 0.0;\r\n\r\n    <span class=\"comments\">\/* divide by zero *\/<\/span>\r\n    r = fpclassify(a\/b);\r\n\r\n    switch(r)\r\n    {\r\n        case FP_NAN:\r\n            printf(\"%.1f\/%.1f is not a number (NaN)\\n\",a,b);\r\n            break;\r\n        case FP_INFINITE:\r\n            printf(\"%.1f\/%.1f is positive or negative infinity\\n\",a,b);\r\n            break;\r\n        case FP_ZERO:\r\n            printf(\"%.1f\/%.1f is zero\\n\",a,b);\r\n            break;\r\n        case FP_SUBNORMAL:\r\n            printf(\"%.1f\/%.1f is too small to be represented\\n\",a,b);\r\n            break;\r\n        case FP_NORMAL:\r\n            printf(\"%.1f\/%.1f is a normal real number\\n\",a,b);\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the code, I use <em>double<\/em> variables <code>a<\/code> and <code>b<\/code> to divide by zero. The result isn&#8217;t saved, but the <em>fpclassify()<\/em> function examines the expression, reporting its findings in variable <code>r<\/code>. The <em>switch-case<\/em> structure examines the value returned and reports the results as compared with defined constants that represent the possibilities. Here&#8217;s a sample run:<\/p>\n<p><code>1.0\/0.0 is positive or negative infinity<\/code><\/p>\n<p>Mathematicians may take issue with this result, as dividing by zero is generally accepted as undefined. Regardless, the <em>fpclassify()<\/em> function reported its results.<\/p>\n<p>A sister function to <em>ipclassify()<\/em> is <em>isfinite()<\/em>. As you may guess, this function examines a floating point expression and reports whether the results are finite or infinite. Here&#8217;s sample code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02_08-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_02_08-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;float.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"isfinite(NAN)         = %s\\n\",\r\n            isfinite(NAN) ? \"TRUE\" : \"FALSE\");\r\n    printf(\"isfinite(INFINITY)    = %s\\n\",\r\n            isfinite(INFINITY) ? \"TRUE\" : \"FALSE\");\r\n    printf(\"isfinite(0.0)         = %s\\n\",\r\n            isfinite(0.0) ? \"TRUE\" : \"FALSE\");\r\n    printf(\"isfinite(DBL_MIN\/2.0) = %s\\n\",\r\n            isfinite(DBL_MIN \/ 2.0) ? \"TRUE\" : \"FALSE\");\r\n    printf(\"isfinite(1.0)         = %s\\n\"\r\n            ,isfinite(1.0) ? \"TRUE\" : \"FALSE\");\r\n    printf(\"isfinite(exp(800))    = %s\\n\",\r\n            isfinite(exp(800)) ? \"TRUE\" : \"FALSE\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the code, I&#8217;ve included the <code>float.h<\/code> header file, which defines the <code>DBL_MIN<\/code> constant. The <em>isfinite()<\/em> macro is used on various constants and expressions to determine whether the result is finite or infinite. Because I use the <em>exp()<\/em> function, this code must be built with the math library included; at the command prompt, add the <code>-lm<\/code> switch. Here&#8217;s the output:<\/p>\n<p><code>isfinite(NAN)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;FALSE<br \/>\nisfinite(INFINITY)&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;FALSE<br \/>\nisfinite(0.0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;TRUE<br \/>\nisfinite(DBL_MIN\/2.0)&nbsp;=&nbsp;TRUE<br \/>\nisfinite(1.0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;TRUE<br \/>\nisfinite(exp(800))&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;FALSE<\/code><\/p>\n<p>Values such as NaN and INF can be a surprise in a program&#8217;s output. If you want to avoid this condition, especially when you&#8217;re coding something severely important, such as launching a rocket into orbit or calculating a possibility for a first date. These functions can help spot conditions that may otherwise generate weird results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible for your code to catch some weird results when doing floating point math. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6806\">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-6806","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\/6806","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=6806"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6806\/revisions"}],"predecessor-version":[{"id":6829,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6806\/revisions\/6829"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}