{"id":93203,"date":"2016-03-25T07:00:00","date_gmt":"2016-03-25T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=93203"},"modified":"2019-03-13T10:31:49","modified_gmt":"2019-03-13T17:31:49","slug":"20160325-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160325-00\/?p=93203","title":{"rendered":"Fixing a floating point exception when operating on NaN"},"content":{"rendered":"<p>A customer had a DLL would sometimes fail to load into certain clients. Further investigation revealed that it failed to load into programs that had enabled floating point exceptions, <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2008\/07\/03\/8682463.aspx\">perhaps inadvertently<\/a>. The DLL initialized some global variables to hold convenient values, and one of the values it needed was NaN. <\/p>\n<p>This particular DLL did not use the C++ standard library, so it did not have access to <code>numeric_limits&lt;double&gt;::quiet_NaN()<\/code>. (And even if it did, those values are not compile-time constants.) Instead, it tried to generate a NaN on its own: <\/p>\n<pre>\ndouble const NaN = HUGE_VAL * 0;\n<\/pre>\n<p>The gotcha here is that <code>HUGE_VAL<\/code> is a static global variable in the Microsoft C runtime, so this performs a runtime calculation inside of <code>DLL_PROCESS_ATTACH<\/code>, and if floating point exceptions are enabled, the NaN-generating calculation causes the exception to be raised, which then causes the loader to treat the DLL as having failed to initialize. <\/p>\n<p>The DLL was precalculating NaN because there was a method that returns NaN to indicate that something isn&#8217;t available. They precalculated the value so that that method could return it. <\/p>\n<p>The customer&#8217;s solution was to move the NaN-generating calculation into the code that needed it. Unfortunately, that still raises the &#8220;invalid calculation&#8221; exception in the case where the function wants to report &#8220;No value&#8221;, which isn&#8217;t really a case of an invalid calculation; it&#8217;s just a sentinel value. <\/p>\n<p>Instead, the code can <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/03\/05\/9973225.aspx\">generate a NaN at compile time<\/a>. Everybody wins: There is no exception at DLL load time because there is no code running at DLL load time! It also means that the page containing the <code>NaN<\/code> variable does not get dirtied, which avoids a copy-on-write charge. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generate it at compile time.<\/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-93203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Generate it at compile time.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93203","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=93203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93203\/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=93203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=93203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=93203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}