C library - fesetenv() function



The C fenv library fesetenv() function state the floating-point environment that contain the rounding or control mode. It is useful in many scenarios where we need to change the value of floating-point.

Syntax

Following is the C library syntax of the fesetenv() function.

fesetenv(const fenv_t *envp);

Parameters

This function accepts only a single parameter −

  • fenv_t: It takes a pointer to an fenv_t object that contains the state to be restored.

Return Value

This function returns an integer value, which is −

  • zero, if the program works.

  • non-zero, if environment cannot be set.

Example 1

Following is the C library function to see the demonstration of fesetenv() function.

#include <stdio.h>
#include <fenv.h>

int main() {
    fenv_t env;
    fegetenv(&env); 
    
    // Perform some floating-point operations here
    fesetenv(&env); 
    
    // Restore the saved environment
    printf("Environment restored.\n");
    return 0;
}

Output

The above code produces the following result −

Environment restored.

Example 2

Below the program illustrates the default behavior of floating-point environment using FE_DFL_ENV.

#include <stdio.h>
#include <fenv.h>

int main() {
   // Set default environment, clearing all exceptions
   fesetenv(FE_DFL_ENV); 
   printf("Default environment set, all exceptions cleared.\n");
   return 0;
}

Output

On execution of above code, we get the following result −

Default environment set, all exceptions cleared.
c_library_fenv_h.htm
Advertisements