Last active
October 15, 2018 20:10
-
-
Save bbrandt/93592d0af91f9d556d8d1a74a4a951a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //--------------------------------------------------------------------------- | |
| #ifndef FPExceptionDisablerH | |
| #define FPExceptionDisablerH | |
| //--------------------------------------------------------------------------- | |
| #if defined (_MSC_VER) | |
| // Microsoft Visual C++ | |
| // Declare an object of this type in a scope in order to suppress | |
| // all floating-point exceptions temporarily. The old exception | |
| // state will be reset at the end. | |
| // From https://randomascii.wordpress.com/2012/04/21/exceptional-floating-point/ | |
| class FPExceptionDisabler | |
| { | |
| public: | |
| FPExceptionDisabler() | |
| { | |
| // Retrieve the current state of the exception flags. This | |
| // must be done before changing them. _MCW_EM is a bit | |
| // mask representing all available exception masks. | |
| _controlfp_s(&mOldValues, 0, 0); | |
| // Set all of the exception flags, which suppresses FP | |
| // exceptions on the x87 and SSE units. | |
| _controlfp_s(0, _MCW_EM, _MCW_EM); | |
| } | |
| ~FPExceptionDisabler() | |
| { | |
| // Clear any pending FP exceptions. This must be done | |
| // prior to enabling FP exceptions since otherwise there | |
| // may be a ‘deferred crash’ as soon the exceptions are | |
| // enabled. | |
| _clearfp(); | |
| // Reset (possibly enabling) the exception status. | |
| _controlfp_s(0, mOldValues, _MCW_EM); | |
| } | |
| private: | |
| unsigned int mOldValues; | |
| // Make the copy constructor and assignment operator private | |
| // and unimplemented to prohibit copying. | |
| FPExceptionDisabler(const FPExceptionDisabler&); | |
| FPExceptionDisabler& operator=(const FPExceptionDisabler&); | |
| }; | |
| #elif defined __BORLANDC__ | |
| // Embadercadero C++ Builder | |
| #include <System.Math.hpp> | |
| class FPExceptionDisabler | |
| { | |
| public: | |
| FPExceptionDisabler() | |
| { | |
| mSavedArithmeticExceptionMask = GetExceptionMask(); | |
| SetExceptionMask(exAllArithmeticExceptions); | |
| } | |
| ~FPExceptionDisabler() | |
| { | |
| SetExceptionMask(mSavedArithmeticExceptionMask); | |
| } | |
| private: | |
| TArithmeticExceptionMask mSavedArithmeticExceptionMask; | |
| // Make the copy constructor and assignment operator private | |
| // and unimplemented to prohibit copying. | |
| FPExceptionDisabler(const FPExceptionDisabler&); | |
| FPExceptionDisabler& operator=(const FPExceptionDisabler&); | |
| }; | |
| #endif | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "FPExceptionDisabler.h" | |
| void funky_math() | |
| { | |
| FPExceptionDisabler fpExceptionDisabler; | |
| double infinite = 52.0 / 0.0; | |
| // Do more stuff | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out this excellent floating point reference:
https://randomascii.wordpress.com/2012/04/21/exceptional-floating-point/