Actual source code: ex59.c

  1: static char help[] = "Tests not trapping an underflow\n\n";

  3: #include <petscsys.h>
  4: #include <float.h>
  5: #include <math.h>

  7: /* From https://stackoverflow.com/questions/37193363/float-underflow-in-c-explanation */
  8: void demo(void)
  9: {
 10:   /*
 11:   FLT_MIN, FLT_MIN and the display of the floating point numbers are not portable

 13:   const char *format = "%.10e %a\n";
 14:   printf(format, FLT_MIN, FLT_MIN);
 15:   printf(format, FLT_TRUE_MIN, FLT_TRUE_MIN);
 16:   */

 18:   float f = nextafterf(1.0f, 2.0f);
 19:   do {
 20:     /* if trapping of underflow is turned on then this will generate an exception */
 21:     f /= 2;
 22:     /* printf(format, f, f); */
 23:   } while (f);
 24: }

 26: int main(int argc, char **argv)
 27: {
 28:   PetscFunctionBeginUser;
 29:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 30:   demo();
 31:   PetscCall(PetscFinalize());
 32:   return 0;
 33: }

 35: /*TEST

 37:    test:
 38:      TODO: Doesn't work on AArch64 targets. There's a known hardware limitation. arch-ci-linux-cmplx-single
 39:      args: -fp_trap

 41: TEST*/