INFO: Handling Floating-Point Errors in C

Last reviewed: September 2, 1997
Article ID: Q43391
The information in this article applies to:
  • The C Run-time (CRT) included with: - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, 6.0ax - Microsoft C for OS/2, versions 5.1, 6.0, 6.0a - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 2.2, 4.0,

         4.1, 5.0
    

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.

Keywords          : CRTIss kbfasttip
Version           : MS-DOS:6.00ax,7.0; OS/2:5.1,6.00a; WINDOWS:1.0,1.5;  WINDOWS NT:1.0,2.0,4.0,4.1,5.0
Platform          : MS-DOS NT OS/2 WINDOWS
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.