probe_math_exception.c 836 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* Small test program for probing how various math functions
  2. * with specific operands set floating point exceptions
  3. */
  4. #define _ISOC99_SOURCE 1
  5. #define _GNU_SOURCE 1
  6. #include <math.h>
  7. #include <fenv.h>
  8. #include <stdio.h>
  9. int main(int argc, char **argv)
  10. {
  11. float infF = HUGE_VALF * 2;
  12. feclearexcept(FE_ALL_EXCEPT);
  13. // printf("%.40e\n", 1.0 / 0.0); // FE_DIVBYZERO
  14. // printf("%.40e\n", nextafterf(HUGE_VALF, infF)); // no exceptions in glibc 2.4
  15. #define PREX(ex) do { if (fetestexcept(ex)) printf(#ex); } while(0)
  16. #ifdef FE_INEXACT
  17. PREX(FE_INEXACT);
  18. #endif
  19. #ifdef FE_DIVBYZERO
  20. PREX(FE_DIVBYZERO);
  21. #endif
  22. #ifdef FE_UNDERFLOW
  23. PREX(FE_UNDERFLOW);
  24. #endif
  25. #ifdef FE_OVERFLOW
  26. PREX(FE_OVERFLOW);
  27. #endif
  28. #ifdef FE_INVALID
  29. PREX(FE_INVALID);
  30. #endif
  31. if (fetestexcept(FE_ALL_EXCEPT))
  32. printf("\n");
  33. printf("done\n");
  34. return 0;
  35. }