Actual source code: ex59.c


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

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

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

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

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

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

 36: /*TEST

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

 42: TEST*/