INFO: Handling Floating-Point Errors in C (43391)



The information in this article applies to:

  • The C Run-Time (CRT), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q43391

SUMMARY

With Microsoft C, floating-point error-handling routines may be written using the signal() function; however, "return" should not be used to go back to the calling process. If "return" is used, the floating-point state is left undefined.

Instead, use the setjmp() function to save the stack environment before each section of the calling process code that may potentially generate a floating-point error. In the error handler, use _fpreset() to reinitialize the floating-point package and longjmp() to return to the calling process.

WARNING: Neither the setjmp nor longjmp function is compatible with the C++ language.

The sample program on page 296 of the "Microsoft C/C++ Run-time Library Reference" that shipped with Microsoft C/C++ version 7.0 demonstrates the use of setjmp(), _fpreset(), and longjmp() to handle a floating-point error. The online help also provides samples for these functions.

The function setjmp() saves the environment (bp, si, di, sp, and ds registers) and return address in a buffer. The buffer is used by longjmp() to restore the environment.

Modification Type:MajorLast Reviewed:12/8/2003
Keywords:kbCRT kbinfo KB43391 kbAudDeveloper