- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - fegetenv() function
The C fenv library fegetenv() function sets the control flow of numbers with decimal values.
Imagine we are working on a math problem and we are using a calculator that can round numbers in different ways. Though, we need to switch to a different calculator with different rounding settings. So, this function is like saving the state of your first calculator and we can go back to it later and continue with the exact same settings as before.
Syntax
Following is the C library syntax of the fegetenv() function.
int fegetenv(fenv_t *envp);
Parameters
This function accepts only a single parameter −
- fen_v - The function takes a pointer to an fenv_t object where the current environment will be stored.
Return Value
This function returns the integers value which is,
zero, if it is succesful.
-
non-zero, when the environment cannot be obtained.
Example 1
Following is the C library fegetenv() function demonstrates the task of saving and restoring of Floating-Point Environment.
#include <stdio.h>
#include <fenv.h>
int main() {
fenv_t env;
// Save the current floating-point environment
fegetenv(&env);
// Perform some floating-point operations here
fesetenv(&env);
// Restore the saved environment
printf("Floating-point environment restored.\n");
return 0;
}
Output
The above code produces the following result −
Floating-point environment restored.
Example 2
In this example, the program perform the operations which may cause an overflow, restore the environment and it will check whether an overflow has occured or not.
#include <fenv.h>
#include <stdio.h>
// rounding direction mode
void rounding_mode()
{
printf("Rounding mode is ->");
switch (fegetround()) {
case FE_TONEAREST:
// Round to nearest
printf("FE_TONEAREST\n");
break;
case FE_DOWNWARD:
// Round downward
printf("FE_DOWNWARD\n");
break;
case FE_UPWARD:
// Round upward
printf("FE_UPWARD\n");
break;
case FE_TOWARDZERO:
// Round toward zero
printf("FE_TOWARDZERO\n");
break;
default:
printf("unknown\n");
};
}
int main(void)
{
fenv_t envp;
// initial environment
printf("Initial environment :\n");
// print the exception raised initially
printf("Exception raised -> \n");
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
printf("FE_DIVBYZERO \n");
if (fetestexcept(FE_INEXACT))
printf("FE_INEXACT \n");
if (fetestexcept(FE_INVALID))
printf("FE_INVALID \n");
if (fetestexcept(FE_OVERFLOW))
printf("FE_OVERFLOW \n");
if (fetestexcept(FE_UNDERFLOW))
printf("FE_UNDERFLOW \n");
}
else
printf("None\n");
// print the rounding direction mode
rounding_mode();
// Current environment
fegetenv(&envp);
feraiseexcept(FE_ALL_EXCEPT);
// Set rounding direction mode
fesetround(FE_DOWNWARD);
// after environment is change
printf("\nFinal environment :\n");
// print the exception raised
printf("Exception raised -> \n");
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
printf("FE_DIVBYZERO \n");
if (fetestexcept(FE_INEXACT))
printf("FE_INEXACT \n");
if (fetestexcept(FE_INVALID))
printf("FE_INVALID \n");
if (fetestexcept(FE_OVERFLOW))
printf("FE_OVERFLOW \n");
if (fetestexcept(FE_UNDERFLOW))
printf("FE_UNDERFLOW \n");
}
else
printf("None\n");
// print the rounding direction mode
rounding_mode();
return 0;
}
Output
On execution of above code, we get the following result −
Initial environment : Exception raised -> None Rounding mode is ->FE_TONEAREST Final environment : Exception raised -> FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW Rounding mode is ->FE_DOWNWARD