xref: /petsc/src/sys/tests/ex59.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1 
2 static char help[] = "Tests not trapping an underflow\n\n";
3 
4 #include <petscsys.h>
5 #include <float.h>
6 #include <math.h>
7 
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
13 
14   const char *format = "%.10e %a\n";
15   printf(format, FLT_MIN, FLT_MIN);
16   printf(format, FLT_TRUE_MIN, FLT_TRUE_MIN);
17   */
18 
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 }
26 
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 }
35 
36 /*TEST
37 
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
41 
42 TEST*/
43