Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is feholdexcept in C ?

Sheza Naveed

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The feholdexcept function saves the present floating point environment. It then sets the present values of floating point status flags again.

Prototype

The function prototype is as follows:

int feholdexcept( fenv_t* env )

Parameters

env - pointer to object of type fenv_t. It contains the present status of the floating point environment.

Return value

Figure 1: Return values of the function under different conditions.

Example

The code below shows the use of the feholdexcept function. The function is used to prevent exceptions in case of overflow and division by zero, as shown in line 12:

#include <stdio.h>
#include <float.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
void show_fe_excepts(void)
{
printf("exceptions raised: ");
if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
if(fetestexcept(FE_INVALID)) printf(" FE_INVALID");
if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
double func (double x)
{
fenv_t curr_excts;
// Save and clear current f-p environment.
feholdexcept(&curr_excts);
// Raise inexact exceptions.
printf("In func(): x = %f\n", x = x/0);
show_fe_excepts();
// clear exception
feclearexcept(FE_DIVBYZERO);
// calling the function
feupdateenv(&curr_excts);
return x;
}
int main(void)
{
show_fe_excepts();
printf("func(DBL_MAX) = %f\n", func(DBL_MAX));
show_fe_excepts();
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Naveed
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring