How do I prevent/suppress SIGFPE in C++?
<p>I'm trying to convert a double to float as well as various integer types inside a dll, which is used as a Game Maker extension. I don't need a sensible result if the double doesn't fit the range of the target types, so I simply used a static_cast.</p>

<p>Everything works as intended when I call this code from my own test C++ application, but when it's called from Game Maker, range errors raise SIGFPE for some reason, which leads Game Maker to terminate my program with an error message.</p>

<p>I don't need sensible results for out-of-range conversions, but crashing is a no-no. I tried using llround instead of a cast, but it also raises the signal.</p>

<p>I also tried catching the signal myself by using signal(SIGFPE, SIG_IGN); right before the conversion, but it didn't change the behaviour at all. Maybe the ominous comment in the mingw signal.h has something to do with that: "SIGFPE doesn't seem to work?"</p>

<p>I checked the source code of a different dll used in a Game Maker extension, and the binary provided by the author performs simple cast conversions without a problem. When I compile the source myself however, the SIGFPE problem is present again. I am guessing that the author used a different compiler, but I'd prefer to stay with mingw if possible. </p>

<p>So, how do I either perform these conversions safely, or prevent the signal from being generated when I perform them with a simple cast? I'm using mingw-g++ 4.5.0 to compile at the moment.</p>

<p>Here's the function where the problem happens:</p>

<pre><code>template&lt;typename ValueType&gt;
static double writeIntValue(double handle, double value) {
    boost::shared_ptr&lt;Writable&gt; writable = handles.find&lt;Writable&gt;(handle);
    if(writable) {
        // Execution reaches this point
        ValueType converted = static_cast&lt;ValueType&gt;(value);
        // Execution doesn't reach this point if e.g. ValueType 
        // is short and value is 40000
        writable-&gt;write(reinterpret_cast&lt;uint8_t *&gt;(&amp;converted), sizeof(converted));
    }
    return 0;
}
</code></pre>
