C ++ fegetenv () - Biblioteca estándar de C ++

La función fegetenv () en C ++ intenta almacenar el estado del entorno de punto flotante en un objeto de tipo fenv_t.

La función fegetenv () se define en el archivo de encabezado.

prototipo fegetenv ()

 int fegetenv (fenv_t * envp);

Esta función intenta almacenar el entorno de punto flotante en el envp del objeto puntero.

Parámetros de fegetenv ()

  • envp: puntero a un objeto de tipo fenv_t que almacena el estado del entorno de punto flotante.

fegetenv () Valor de retorno

  • Si tiene éxito, la función fegetenv () devuelve 0.
  • Si falla, devuelve un valor distinto de cero.

Ejemplo: ¿Cómo funciona la función fegetenv ()?

 #include #include #include #pragma STDC FENV_ACCESS ON using namespace std; void print_exceptions() ( cout << "Raised exceptions: "; if(fetestexcept(FE_ALL_EXCEPT)) ( if(fetestexcept(FE_DIVBYZERO)) cout << "FE_DIVBYZERO "; if(fetestexcept(FE_INEXACT)) cout << "FE_INEXACT "; if(fetestexcept(FE_INVALID)) cout << "FE_INVALID "; if(fetestexcept(FE_OVERFLOW)) cout << "FE_OVERFLOW "; if(fetestexcept(FE_UNDERFLOW)) cout << "FE_UNDERFLOW "; ) else cout << "None"; cout << endl; ) void print_current_rounding_direction() ( cout << "Current rounding method: "; switch (fegetround()) ( case FE_TONEAREST: cout << "FE_TONEAREST"; break; case FE_DOWNWARD: cout << "FE_DOWNWARD"; break; case FE_UPWARD: cout << "FE_UPWARD"; break; case FE_TOWARDZERO: cout << "FE_TOWARDZERO"; break; default: cout << "unknown"; ); cout << endl; ) void print_environment() ( print_exceptions(); print_current_rounding_direction(); ) int main(void) ( cout << "Initial environment " << endl; print_environment(); fenv_t envp; /* Save current environment */ fegetenv(&envp); feraiseexcept(FE_INVALID|FE_DIVBYZERO); fesetround(FE_DOWNWARD); cout << "After changing environment " << endl; print_environment(); /* Restores previous environment */ fesetenv(&envp); cout << "Restoring initial environment " << endl; print_environment(); return 0; )

Cuando ejecute el programa, la salida será:

 Entorno inicial Excepciones generadas: Ninguna Método de redondeo actual: FE_TONEAREST Después de cambiar el entorno Excepciones generadas: FE_DIVBYZERO FE_INVALID Método de redondeo actual: FE_DOWNWARD Restauración del entorno inicial Excepciones generadas: Ninguna Método de redondeo actual: FE_TONEAREST

Articulos interesantes...