Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Resets the floating-point package.

void _fpreset( void );


The _fpreset function reinitializes the floating-point math package. _fpreset is usually used with signal, system, or the _exec or _spawn functions. If a program traps floating-point error signals (SIGFPE) with signal, it can safely recover from floating-point errors by invoking _fpreset and using longjmp.


Function Required header Compatibility
_fpreset <float.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_fpreset.c
// arguments: 5 0

/* This program uses signal to set up a
 * routine for handling floating-point errors.

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <string.h>

jmp_buf mark;              /* Address for long jump to jump to */
int     fperr;             /* Global error number */

void __cdecl fphandler( int sig, int num );   /* Prototypes */
void fpcheck( void );

int main( int ac, char* av[] )
   double n1, n2, r;
   int jmpret;

   if( ac != 3 )
      fprintf( stderr, "Usage: %s <number> <number>\n", av[0] );

   /* Unmask all floating-point exceptions. */
    _control87( 0, _MCW_EM );
   /* Set up floating-point error handler. The compiler
    * will generate a warning because it expects
    * signal-handling functions to take only one argument.
    if( signal( SIGFPE, (void (__cdecl *)(int)) fphandler ) == SIG_ERR )
       fprintf( stderr, "Couldn't set SIGFPE\n" );

   /* Save stack environment for return in case of error. First 
    * time through, jmpret is 0, so true conditional is executed. 
    * If an error occurs, jmpret will be set to -1 and false 
    * conditional will be executed.
   jmpret = setjmp( mark );
   if( jmpret == 0 )
      n1 = atof( av[1] );
      n2 = atof( av[2] );
      printf( "Dividing %4.3g by %4.3g...\n", n1, n2 );
      r = n1 / n2;
      /* This won't be reached if error occurs. */
      printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );

      r = n1 * n2;
      /* This won't be reached if error occurs. */
      printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );
/* fphandler handles SIGFPE (floating-point error) interrupt. Note
 * that this prototype accepts two arguments and that the 
 * prototype for signal in the run-time library expects a signal 
 * handler to have only one argument.
 * The second argument in this signal handler allows processing of
 * _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols 
 * that augment the information provided by SIGFPE. The compiler 
 * will generate a warning, which is harmless and expected.

void fphandler( int sig, int num )
   /* Set global for outside check since we don't want
    * to do I/O in the handler.
   fperr = num;
   /* Initialize floating-point package. */
   /* Restore calling environment and jump back to setjmp. Return 
    * -1 so that setjmp will return false for conditional test.
   longjmp( mark, -1 );
void fpcheck( void )
   char fpstr[30];
   switch( fperr )
   case _FPE_INVALID:
       strcpy( fpstr, "Invalid number" );
   case _FPE_OVERFLOW:
       strcpy( fpstr, "Overflow" );

       strcpy( fpstr, "Underflow" );
       strcpy( fpstr, "Divide by zero" );
       strcpy( fpstr, "Other floating point error" );
   printf( "Error %d: %s\n", fperr, fpstr );


Dividing    5 by    0...
Error 131: Divide by zero

See Also

Floating-Point Support Routines | _exec Function Overview | signal | _spawn Function Overview | system | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft