{"id":4263,"date":"2020-08-01T00:01:52","date_gmt":"2020-08-01T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4263"},"modified":"2020-08-01T08:12:14","modified_gmt":"2020-08-01T15:12:14","slug":"digits-of-significance","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4263","title":{"rendered":"Digits of Significance"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4250&#038;preview=true\">last week&#8217;s Lesson<\/a>, I lamented that C lacks a function to compare two floating point values based on a significant number of digits. Being a C programmer, I resolved this issue by writing a function that accomplishes this suddenly necessary task.<br \/>\n<!--more--><br \/>\nBecause computers use binary data to store real numbers, precision is an issue: The values 4.0 and 3.99999 might be considered equal by the computer, but values 4.00001 and 4.00001 might not. The issue is precision, which is related to the binary representation of a real number.<\/p>\n<p>To resolve this issue, I need a function that takes two <em>double<\/em> values and returns the number of matching digits. An example is illustrated in Figure 1.<\/p>\n<div id=\"attachment_4287\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4287\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/L0801-figure1.png\" alt=\"comparing digits\" width=\"350\" height=\"290\" class=\"size-full wp-image-4287\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/L0801-figure1.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/L0801-figure1-300x249.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><p id=\"caption-attachment-4287\" class=\"wp-caption-text\">Figure 1. Between the two values, four digits match.<\/p><\/div>\n<p>Given the two values above, the function returns 4, the number of matching digits including the decimal. This is the function I wanted when comparing the results of my alternative square root function from last week&#8217;s Lesson. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_01-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* return the number of matching digits *\/<\/span>\r\nint significance(double a, double b)\r\n{\r\n    const int size = 17;\r\n    char val1[size],val2[size];\r\n    int count;\r\n\r\n    <span class=\"comments\">\/* convert values to strings *\/<\/span>\r\n    snprintf(val1,size,\"%lf\",a);\r\n    snprintf(val2,size,\"%lf\",b);\r\n\r\n    <span class=\"comments\">\/* count the matching characters *\/<\/span>\r\n    count = 0;\r\n    while( val1[count]!='\\0' || val2[count]!='\\0' )\r\n    {\r\n        if( val1[count] != val2[count] )\r\n            <span class=\"comments\">\/* bail on mismatch *\/<\/span>\r\n            break;\r\n        count++;\r\n    }\r\n\r\n    return(count);\r\n}\r\n\r\nint main()\r\n{\r\n    int r;\r\n    double v1,v2;\r\n\r\n    v1 = 5.98642;\r\n    v2 = 5.98536;\r\n\r\n    r = significance(v1,v2);\r\n    printf(\"The values %lf and %lf show %d digits match\\n\",\r\n            v1,\r\n            v2,\r\n            r\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The wizard behind the curtain in the <em>significance()<\/em> function is <em>snprintf()<\/em>, appearing at Lines 11 and 12. This function converts the <em>double<\/em> values passed into strings, which are more easily compared. The <em>snprintf()<\/em> function is better than plain ol&#8217; <em>sprintf()<\/em> in that the <em>n<\/em> in the name refers to a character count. This extra check makes the function more secure.<\/p>\n<p>The <em>while<\/em> loop at Line 16 compares the strings, incrementing variable <code>count<\/code> for each match. This process repeats until the characters don&#8217;t match or either string&#8217;s terminating null character is encountered.<\/p>\n<p>The value of variable <code>count<\/code> is returned, which is shown in the output:<\/p>\n<p><code>The values 5.986420 and 5.985360 show 4 digits match<\/code><\/p>\n<p>Various improvements to the code are possible. For example, the function could examine only the portions of the number to the left or right of the decimal. As always, programmers can explore endless ways to keep writing code &mdash; especially without looming deadlines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seeing the necessity, I wrote a function to compare floating-point values for significant digits. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4263\">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-4263","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\/4263","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=4263"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4263\/revisions"}],"predecessor-version":[{"id":4294,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4263\/revisions\/4294"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}