{"id":3695,"date":"2019-08-03T00:01:31","date_gmt":"2019-08-03T07:01:31","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3695"},"modified":"2019-07-27T10:14:43","modified_gmt":"2019-07-27T17:14:43","slug":"the-teeniest-of-tiniest","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3695","title":{"rendered":"The Teeniest of Tiniest"},"content":{"rendered":"<p>It took me a while to figure out the significance of the <em>nextafter()<\/em> function. Though I understand what it does now, I still can&#8217;t figure out how it&#8217;s useful, though I assume it has a valid purpose otherwise it wouldn&#8217;t be in the standard library.<br \/>\n<!--more--><br \/>\nThe <em>nextafter()<\/em> function returns the next valid real number after a given value, up or down. It&#8217;s man page format is:<\/p>\n<p><code>double nextafter(double x, double y);<\/code><\/p>\n<p>Argument <code>x<\/code> is a floating point value, <code>y<\/code> is a direction, greater than or less than value <code>x<\/code>. The value returned is the next valid real number before or after argument <code>x<\/code>.<\/p>\n<p>The <em>nextafter()<\/em> function is prototyped in the <code>math.h<\/code> header file. Its inclusion in your code may also require you to link in the math library.<\/p>\n<p>Here&#8217;s my test program:<\/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    double max,min,f;\r\n   \r\n    f = 0.0;\r\n    max = nextafter(f,1.0);\r\n    min = nextafter(f,-1.0);\r\n    printf(\"%e max is %e\\n\",f,max);\r\n    printf(\"%e min is %e\\n\",f,min);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>f<\/code> is set to zero. Variable <code>max<\/code> is assigned the next valid real number above zero and variable <code>min<\/code> is assigned the next value below. I use the <code>%e<\/code> placeholder for the output because the results are so small; if <code>%f<\/code> is used instead, output would be <code>0.000000<\/code>.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>0.000000e+00 max is 4.940656e-324<br \/>\n0.000000e+00 min is -4.940656e-324<\/code><\/p>\n<p>That&#8217;s a teensy, tiny number, which my mathematically-challenged mind questions as to why or how it&#8217;s even useful. Then again, I&#8217;m just documenting what I&#8217;ve found. I&#8217;m sure some chemist or astrophysicist would be orgasmic upon seeing a value like 4.940656e-324.<\/p>\n<p>Just to be curious. I changed the assignment of variable f to 100.0 at Line 8. Here&#8217;s the updated output:<\/p>\n<p><code>1.000000e+02 max is 1.000000e+02<br \/>\n1.000000e+02 min is 1.000000e+02<\/code><\/p>\n<p>So either the &#8220;next after&#8221; floating point value is so small it doesn&#8217;t register or it just doesn&#8217;t exist. Not being a propellerhead, I don&#8217;t know the difference. Further, I&#8217;m really at a loss as to how this function could prove useful. Perhaps someone at the Jet Propulsion Laboratory can help out?<\/p>\n<p>My goal wasn&#8217;t to poke fun at the <em>nextafter()<\/em> function, though I do enjoy poking fun at math. It&#8217;s enjoyable to play with real numbers and my curiosity with the function was to see how small such values really get. But neither the documentation nor my understanding extracts any useful information beyond the trivial tidbits.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just how small of a value can you represent as a floating point number? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3695\">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-3695","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\/3695","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=3695"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3695\/revisions"}],"predecessor-version":[{"id":3699,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3695\/revisions\/3699"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}