{"id":14713,"date":"2010-03-05T07:00:00","date_gmt":"2010-03-05T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/03\/05\/how-do-i-access-the-magic-ieee-floating-point-values-like-nan-in-code\/"},"modified":"2010-03-05T07:00:00","modified_gmt":"2010-03-05T07:00:00","slug":"how-do-i-access-the-magic-ieee-floating-point-values-like-nan-in-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100305-00\/?p=14713","title":{"rendered":"How do I access the magic IEEE floating point values like NaN in code?"},"content":{"rendered":"<p>\nThere are functions like\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms924276.aspx\">\n<code>_isnan<\/code>, <code>_isnanf<\/code><\/a>,\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/sb8es7a8.aspx\">\n<code>_finite<\/code><\/a>,\nand\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/39s1cck2.aspx\">\n<code>_fpclass<\/code><\/a>\nfor detecting that a floating point value is one of the special\nvalues like NaN,\nbut how do you actually generate one of these values?\n<\/p>\n<p>\nYou can access these values from the <code>std::numeric_limits<\/code>\ntemplate.<\/p>\n<pre>\nstd::numeric_limits&lt;float&gt;::infinity(); \/\/ positive infinity\nstd::numeric_limits&lt;float&gt;::quiet_NaN(); \/\/ non-signalling NaN\n<\/pre>\n<\/p>\n<p>\nWait, where&#8217;s negative infinity?\nThe compiler folks provided these handy little definitions\nfor when you need to generate a special value (as opposed to merely\ndetecting one),\nand for which the <code>numeric_limits<\/code> template comes up short.\n<\/p>\n<pre>\nDECLSPEC_SELECTANY extern const float FLOAT_POSITIVE_INFINITY = ((float)(1e308 * 10));\nDECLSPEC_SELECTANY extern const float FLOAT_NEGATIVE_INFINITY = ((float)(-1e308 * 10));\nDECLSPEC_SELECTANY extern const float FLOAT_NaN = ((float)((1e308 * 10)*0.));\n<\/pre>\n<p>\nDisclaimer:\nApplies to Microsoft Visual Studio.\nYour mileage may vary.\nUse the template when available.\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nNote that you must use functions like\n<code>_isnan<\/code> to detect special values,\nbecause floating point special values behave very\nstrangely in comparisons.\n(For example, NaN does not compare equal to itself!)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are functions like _isnan, _isnanf, _finite, and _fpclass for detecting that a floating point value is one of the special values like NaN, but how do you actually generate one of these values? You can access these values from the std::numeric_limits template. std::numeric_limits&lt;float&gt;::infinity(); \/\/ positive infinity std::numeric_limits&lt;float&gt;::quiet_NaN(); \/\/ non-signalling NaN Wait, where&#8217;s negative infinity? [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-14713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There are functions like _isnan, _isnanf, _finite, and _fpclass for detecting that a floating point value is one of the special values like NaN, but how do you actually generate one of these values? You can access these values from the std::numeric_limits template. std::numeric_limits&lt;float&gt;::infinity(); \/\/ positive infinity std::numeric_limits&lt;float&gt;::quiet_NaN(); \/\/ non-signalling NaN Wait, where&#8217;s negative infinity? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=14713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=14713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=14713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=14713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}