...
Wiki Markup |
---|
The most portable way of determining a floating point exceptional condition has occurred is to make use of the floating point exception faculties provided by C99 in {{fenv.h}} \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]. |
However, the C99 floating point exception functions are not without problems. The following caveats exist regarding the interaction between floating point exceptions and conversions:
- Conversion from floating point to integer may cause an "invalid" floating point exception. If this occurs, the value of that integer is undefined and should not be used.
- Most implementations fail to raise "invalid" for conversions from any negative or "large" positive FP values to unsigned integer types or to signed char. See www.tybor.com/tflt2int.c
- When a non-integer floating point value is converted into an integer the "inexact" floating point exception is raised.
For information regarding floating point number conversions, see FLP34-C. Ensure that floating point conversions are within range of the new type.
Though the notion of floating point exceptions guaranteed by C99, floating point exceptions generally only exist on architectures utilizing IEEE-754. Despite this, these functions are the only somewhat portable solution to handling floating point exceptions.
A less portable, but sometimes more intuitive and/or more informative, solution is to make use of the faculties provided by the underlying implementation. If this approach is taken, the caveats of that system need to be well understood. The following table can serve as a starting point for some common architectures:
Operating System | How to handle floating point errors | |||
---|---|---|---|---|
Linux | Use the C99 floating point exception functions. | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="56365e7069d51c65-83ae21dd-41e54dfa-9523a0d0-e9583b1baaede98919540513"><ac:plain-text-body><![CDATA[ | Windows | Either use the C99 floating point exception function or structured exception handling through | AA. C References#MSDN]] | ]]></ac:plain-text-body></ac:structured-macro> |
...