Note that there are some explanatory texts on larger screens.

plurals
  1. POFloating-point NaN depending on uncorrelated exception handling in C++
    primarykey
    data
    text
    <p>Really weird:</p> <pre><code>double *data; // uncorrelated double a,b,c; double sigma = 1e-309; // denormalized number try { data = new double[10]; } // uncorrelated catch(...) { cout &lt;&lt; "error"; return 1; } a = 1/sigma; // infinite b = exp(-1/sigma); // 0 c = a * b; // NaN cout &lt;&lt; c &lt;&lt; endl; c = (1/sigma) * exp(-1/sigma); // 0 cout &lt;&lt; c &lt;&lt; endl; </code></pre> <p>Ok, the second c result could be 0 because of some optimization.</p> <p><strong>BUT</strong>: when I delete the try/catch block, the second c remains NaN! Why this different behaviour??? My compiler is VC++ 2010 Express. OS Windows 7 64-bit. I use only standard libs like iostream and cmath.</p> <p><strong>Edit</strong>: my first observation was with Debug+Win32 default settings for an empty console application. With Release+Win32 the results are: first c 0, second c NaN - no matter if try/catch present or not! Summary:</p> <pre><code> //Debug+Win32 // Release+Win32 //with try //without //with try //without c = a * b; // NaN NaN 0 0 c = (1/sigma) * exp(-1/sigma); // 0 NaN NaN NaN </code></pre> <p><strong>Edit 2</strong>: When I set the <code>/fp:strict</code> switch in C++/codegeneration, the result is same with Debug+Win32, but with Release+Win32 it changes to <code>c = a * b; // NaN</code> and <code>c = (1/sigma) * exp(-1/sigma); // 0</code> no matter if with try or not. I don't get why it stays <code>NaN+NaN</code> with Debug+Win32 and no preceding try. How to debug a program that has to be floating-point safe, when results differ dispite <code>/fp:strict</code> from Release depending on preceding try?</p> <p><strong>Edit 3</strong>: Here a full program:</p> <pre><code>// On VC++ 2010 Express in default Win32-Debug mode for empty console application. // OS: Windows 7 Pro 64-Bit, CPU: Intel Core i5. // Even when /fp:strict is set, same behaviour. // // Win32-Release mode: first c == 0, second c == NaN (independent of try) // with /fp:strict: first c == NaN, second c == 0 (also independent of try) #include &lt;iostream&gt; #include &lt;cmath&gt; using namespace std; int main() { double *data; // uncorrelated double a,b,c; double sigma = 1e-309; // denormalized number try { data = new double[10]; } // uncorrelated catch(...) { cout &lt;&lt; "error"; return 1; } a = 1/sigma; // infinite b = exp(-1/sigma); // 0 c = a * b; // NaN cout &lt;&lt; c &lt;&lt; endl; c = (1/sigma) * exp(-1/sigma); // 0 with preceding try or cout &lt;&lt; c &lt;&lt; endl; // NaN without preceding try cin.get(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload