xref: /petsc/src/sys/tests/ex59.c (revision 0baf8eba40dbc839082666f9f7396a225d6f663c)
1 static char help[] = "Tests not trapping an underflow\n\n";
2 
3 #include <petscsys.h>
4 #include <float.h>
5 #include <math.h>
6 
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
12 
13   const char *format = "%.10e %a\n";
14   printf(format, FLT_MIN, FLT_MIN);
15   printf(format, FLT_TRUE_MIN, FLT_TRUE_MIN);
16   */
17 
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 }
25 
26 int main(int argc, char **argv)
27 {
28   PetscFunctionBeginUser;
29   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
30   demo();
31   PetscCall(PetscFinalize());
32   return 0;
33 }
34 
35 /*TEST
36 
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
40 
41 TEST*/
42