Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I know, you have two choices for floating point exception handling in C and C++:</p> <p>First, if you disable/mask floating point exceptions (which most environments do by default), you can see whether any floating point exceptions have occurred by calling <a href="http://linux.die.net/man/3/fetestexcept" rel="nofollow noreferrer">fetestexcept</a>. fetestexcept isn't available in Visual C++, but you can steal the MinGW Runtime's implementation easily enough. (It's in the public domain.) Once an exception has been flagged, it's not cleared until you call feclearexcept, so you can call fetestexcept at the end of a series of calculations to see if any of them raised an exception. This doesn't give you the traps that you asked for, but it does let you test if problems like NaN or +/-inf have occurred and react as needed.</p> <p>Second, you can enable/unmask floating point exceptions by calling <a href="http://linux.die.net/man/3/feenableexcept" rel="nofollow noreferrer">feenableexcept</a> in Linux or <a href="http://msdn.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx" rel="nofollow noreferrer">_controlfp</a> in Windows. How the operating system handles a processor-generated floating point exception depends on your operating system.</p> <ul> <li>In Linux, the OS sends a SIGFPE signal, so you can install a signal handler to catch that and set a flag that tells your routine to react appropriately.</li> <li>In Windows, the OS invokes Structured Exception Handling to convert the processor exception into a language exception that you can catch using a <code>__try</code> / <code>__catch</code> block in C or <code>try</code> / <code>catch</code> block in C++.</li> <li><strong>Update:</strong> For Mac OS X, as described in <a href="https://stackoverflow.com/questions/247053/enabling-floating-point-interrupts-on-mac-os-x-intel/340683#340683">this answer</a>, you should be able to enable/unmask exceptions using <code>_MM_SET_EXCEPTION_MASK</code> from <code>xmmintrin.h</code>, and as long as you use the default compiler options (i.e., don't disable SSE), you should be able to catch exceptions using SIGFPE. </li> </ul> <p>(I've written a bit more on this and other floating point issues in C and C++ in <a href="http://codingcastles.blogspot.com/2009/01/floating-point-reference.html" rel="nofollow noreferrer">this blog posting</a> if you're curious.)</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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