How to use feclearexcept() function to clear the floating-point exception in C

3 Answers

0 votes
#include <stdio.h>     
#include <math.h>
#include <errno.h>
#include <fenv.h>
 
int main(int argc, char **argv)
{
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("asin(1.2) = %f\n", asin(1.2));
    if (errno == EDOM) perror("errno == EDOM");
    if (fetestexcept(FE_INVALID)) printf("FE_INVALID raised");
  
    return 0;
}
  
// 1.#QNAN0 = Quiet NaNs (qNaNs) do not raise any additional exception
// "NAN" is "not-a-number" - applies to float and double
  
/*
run:
  
asin(1.2) = -1.#IND00
errno == EDOM: Domain error
FE_INVALID raised
 
*/

 



answered Mar 12, 2016 by avibootz
edited Mar 13, 2016 by avibootz
0 votes
#include <stdio.h>     
#include <fenv.h>
 
int main(int argc, char **argv)
{
	double d = 0.0;

	feclearexcept(FE_ALL_EXCEPT);
	printf("1.0/0.0 = %.6f\n", 1.0 / d);
	if (fetestexcept(FE_DIVBYZERO))
		printf("division by zero\n");
  
    return 0;
}

// 1.#QNAN0 = Quiet NaNs (qNaNs) do not raise any additional exception
// "NAN" is "not-a-number" - applies to float and double

/*
run:
  
1.0/0.0 = 1.#INF00
division by zero

*/

 



answered Mar 14, 2016 by avibootz
0 votes
#include <stdio.h>     
#include <fenv.h>
 
int main(int argc, char **argv)
{
	double d = 1.0;

	feclearexcept(FE_ALL_EXCEPT);
	printf("1.0/10 = %.6f\n", d / 10);
	if (fetestexcept(FE_INEXACT))
		printf("inexact result\n");
  
    return 0;
}

/*
run:
  
1.0/10 = 0.100000
inexact result

*/

 



answered Mar 14, 2016 by avibootz
...