title: How to force gfortran to stop at First NaN?
body: <p>My Configuration: Cygwin64, Win7 64 bit, gfortran 4.8.3</p>
  
  <p>I did go through all the Q/A pairs I managed to find, this one is the closest to my problem</p>
  
  <p><a href="https://stackoverflow.com/questions/5636580/force-gfortran-to-stop-program-at-first-nan">Force gfortran to stop program at first NaN</a></p>
  
  <p>PROBLEM: The fortran code</p>
  
  <pre><code>real :: init_with_NaN  ! -finit-real=nan
  res = 10 * init_with_NaN 
  </code></pre>
  
  <p>The code is compiled with the following gfortran flags</p>
  
  <pre><code>COLLECT_GCC_OPTIONS='-funderscoring' '-O0' '-g' '-c' '-fmessage-length=0' '-O0' '-g' '-ffpe-trap=invalid,zero,overflow,underflow,precision,denormal' '-Wall' '-c' '-fmessage-length=0' '-Wpedantic' '-std=f2003' '-fall-intrinsics' '-fbacktrace' '-fimplicit-none' '-Wextra' '-Wformat=1' '-Wconversion' '-Wfatal-errors' '-finit-real=nan' '-Wuninitialized' '-fbounds-check' '-v' '-o' 'fsignal_test.o' '-mtune=generic' '-march=x86-64'
  </code></pre>
  
  <p>Once the code is run, instead of getting a SIGFPE, res is set to be NaN and execution continues.</p>
  
  <p><strong>This is a sub-problem of a million dollar question: 
  Does gfortran -ffpe-trap=invalid flag really stops the program once it detects a NaN anywhere in the program ? Especially on Cygwin64 env?</strong> </p>
  
  <p>A little research showed that:</p>
  
  <p>1) Acc.to <a href="https://gcc.gnu.org/onlinedocs/gfortran/Debugging-Options.html" rel="nofollow noreferrer">https://gcc.gnu.org/onlinedocs/gfortran/Debugging-Options.html</a>, -ffpe-trap works <strong>on MOST systems, not all :(</strong></p>
  
  <p>2) Acc. to <a href="https://stackoverflow.com/questions/7267838/division-by-zero-does-not-throw-sigfpe">Division by zero does not throw SIGFPE</a></p>
  
  <blockquote>
    <p>"You don't get a signal because the default behavior on most machines
    is to pollute your data with NaNs (not-a-number) and infinities. You
    have to enable floating point exceptions, and how you do that is
    machine specific. Look at the system header fenv.h, if you have one.
    The function fesettrapenable enables catching floating point
    exceptions on many machines.</p>
    
    <p>Unfortunately, there is no standard function to turn floating point
    exceptions handling on"</p>
  </blockquote>
  
  <p>3) I have checked my cygwin env, fenv.h exists in multiple places but there is no fesettrapenable function anywhere under any *.h file under cygwin64.</p>
  
  <p>4) I do get SIGFPEs: like sqrt(-1), div by zero, overflow, underflow, precision </p>
  
  <p>Here is the full Fortran test project in Eclipse, use it at your hearts content to learn about exceptions on your own platform:</p>
  
  <p><a href="https://drive.google.com/file/d/0B9U1dlb_UCPqU194XzhDNTNkUUU/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0B9U1dlb_UCPqU194XzhDNTNkUUU/view?usp=sharing</a></p>