feupdateenv() function in C++

The feupdateenv() function in C++ first saves currently raised floating-point exceptions. It restores the floating-point environment from the given fenv_t object and then raises the exceptions which were saved previously.

Syntax:

int feupdateenv( fenv_t* envp )

Parameters: It accepts a single mandatory parameter envp which specifies the pointer to the fenv_t object that is set by an earlier call to feholdexcept or fegetenv or is equal to FE_DFL_ENV. The function also accepts a pointer of type fenv_t as its argument which holds a floating point environment previously set by using feholdexcept or fegetenv and restores that floating point environment along with the current environment.



Return Value: The function returns two kinds of value described below:

  • It returns zero on success
  • It returns non-zero value on failure

Below programs illustrate the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// feupdateenv() function
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
  
// Function to use the fucntion
double answer(double y)
{
  
    // struct defined
    fenv_t trial;
  
    // use the function feholdexcept
    feholdexcept(&trial);
  
    // find log valye
    y = log(y);
  
    // clears exception
    feclearexcept(FE_OVERFLOW | FE_DIVBYZERO);
  
    // call the function for succes or not
    feupdateenv(&trial);
    return y;
}
  
int main()
{
    // It is a combination of all of
    // the possible floating-point exception
    feclearexcept(FE_ALL_EXCEPT);
  
    // it returns the log value
    // if it is to be found
    printf("log(0.0): %f\n", answer(0.0));
  
    // the function does not throws any exception
    if (!fetestexcept(FE_ALL_EXCEPT)) {
        printf("no exceptions raised");
    }
    return 0;
}

chevron_right


Output:

log(0.0): -inf
no exceptions raised

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// feupdateenv() function
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
  
// Function to use the fucntion
double answer(double y)
{
  
    // struct defined
    fenv_t trial;
  
    // use the function feholdexcept
    feholdexcept(&trial);
  
    // find log valye
    y = log(y);
  
    // clears exception
    feclearexcept(FE_OVERFLOW | FE_DIVBYZERO);
  
    // call the function for succes or not
    feupdateenv(&trial);
    return y;
}
  
int main()
{
    // It is a combination of all of
    // the possible floating-point exception
    feclearexcept(FE_ALL_EXCEPT);
  
    // it returns the log value
    // if it is to be found
    printf("log(10.0): %f\n", answer(10.0));
  
    // the function does not throws any exception
    if (!fetestexcept(FE_ALL_EXCEPT)) {
        printf("no exceptions raised");
    }
    else
        printf("exceptions raised");
  
    return 0;
}

chevron_right


Output:

log(10.0): 2.302585
exceptions raised


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.