{"id":22902,"date":"2015-09-04T14:40:38","date_gmt":"2015-09-04T14:40:38","guid":{"rendered":"http:\/\/ok-cleek.com\/blogs\/?p=22902"},"modified":"2015-09-04T15:32:29","modified_gmt":"2015-09-04T15:32:29","slug":"drat-of-the-day","status":"publish","type":"post","link":"https:\/\/ok-cleek.com\/blogs\/?p=22902","title":{"rendered":"Drat Of The Day"},"content":{"rendered":"<p>Which is the minimum of the two:<\/p>\n<p>A: -3.4028235e+038<br \/>\nB: 1.1754944e-038<\/p>\n<p>That depends on how you define minimum. If you want the smallest <em>magnitude<\/em>, it's B. B is so close to 0 it might as well be 0 for most practical uses (0.000000000000000000000000000000000000011754944). But A is smaller if you think 'minimum' means 'leftmost on the number line'. A is so far to the left of B there aren't enough pixels on all the monitors in the world to draw that number line.<\/p>\n<p>In C++ the result of std::numeric_limits&lt;float&gt;::min() is B: the <em>smallest<\/em> value a 'float' can store is B. <\/p>\n<p>But the <em>most-negative<\/em> number a float can store is A. And A is <strong>-<\/strong>std::numeric_limits&lt;float&gt;::max().<\/p>\n<p>So, if you are trying to limit the results of a function to the range of values that a float can hold, and you do this:<\/p>\n<pre>double result = doSomething();\r\ndouble t = max(std::numeric_limits::min(), result);\r\nt = min(std::numeric_limits::max(), t);\r\nreturn (float)t;\r\n<\/pre>\n<p>You're going to very surprised when you discover that you never see a value smaller than 1.1754944e-038. You'll never even get 0.<\/p>\n<p>What you really needed to write is this:<\/p>\n<pre>double result = doSomething();\r\ndouble t = max(-std::numeric_limits::max(), result);\r\nt = min(std::numeric_limits::max(), t);\r\nreturn (float)t;\r\n<\/pre>\n<p>But, that trickery only applies to floating point types. The smallest magnitude you can store in an int is 0. But std::numeric_limits&lt;int&gt;::min() is -2147483648. That's the most-negative value, not the value with least magnitude.<\/p>\n<p>And now you know. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which is the minimum of the two: A: -3.4028235e+038 B: 1.1754944e-038 That depends on how you define minimum. If you want the smallest magnitude, it's B. B is so close to 0 it might as well be 0 for most practical uses (0.000000000000000000000000000000000000011754944). But A is smaller if you think 'minimum' means 'leftmost on the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-22902","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/22902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22902"}],"version-history":[{"count":6,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/22902\/revisions"}],"predecessor-version":[{"id":22908,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/22902\/revisions\/22908"}],"wp:attachment":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}