Name

fegetenv

Synopsis

Stores a copy of the current floating-point environment

#include <fenv.h>
intfegetenv( fenv_t *envp );

The fegetenv() function saves the current state of the floating-point environment in the object referenced by the pointer argument. The function returns 0 if successful; a nonzero return value indicates that an error occurred.

The object type that represents the floating-point environment, fenv_t, is defined in fenv.h. It contains at least two kinds of information: floating-point status flags, which are set to indicate specific floating-point processing exceptions, and a floating-point control mode, which can be used to influence the behavior of floating-point arithmetic, such as the direction of rounding.

Example

The fegetenv() and fesetenv() functions can be used to provide continuity of the floating-point environment between different locations in a program:

static fenv_t  fpenv;   // Global environment variables.
static jmp_buf env;
/* ... */

#pragma STDC FENV_ACCESS ONfegetenv(&fpenv);        // Store a copy of the floating-point environment

if ( setjmp(env) == 0 )  // setjmp() returns 0 when actually called
{
  /* ... Proceed normally; floating-point environment unchanged ... */
}
else                  // Nonzero return value means longjmp() occurred
{
  fesetenv(&fpenv);   // Restore floating-point environment to known state
  /* ... */
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.