C ++ fegetenv () - Libreria standard C ++

La funzione fegetenv () in C ++ tenta di memorizzare lo stato dell'ambiente in virgola mobile in un oggetto di tipo fenv_t.

La funzione fegetenv () è definita nel file di intestazione.

prototipo fegetenv ()

 int fegetenv (fenv_t * envp);

Questa funzione tenta di memorizzare l'ambiente in virgola mobile nell'oggetto pointer envp.

Parametri fegetenv ()

  • envp: puntatore a un oggetto di tipo fenv_t che memorizza lo stato dell'ambiente in virgola mobile.

fegetenv () Restituisce il valore

  • In caso di successo, la funzione fegetenv () restituisce 0.
  • In caso di errore, restituisce diverso da zero.

Esempio: come funziona la funzione 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; )

Quando esegui il programma, l'output sarà:

 Ambiente iniziale Eccezioni sollevate: nessuna Metodo di arrotondamento corrente: FE_TONEAREST Dopo la modifica dell'ambiente Eccezioni sollevate: FE_DIVBYZERO FE_INVALID Metodo di arrotondamento corrente: FE_DOWNWARD Ripristino dell'ambiente iniziale Eccezioni sollevate: nessuna Metodo di arrotondamento corrente: FE_TONEAREST

Articoli interessanti...