xref: /petsc/src/sys/tests/ex59.c (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
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   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   PetscFunctionBeginUser;
28   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
29   demo();
30   PetscCall(PetscFinalize());
31   return 0;
32 }
33 
34 /*TEST
35 
36    test:
37      TODO: Doesn't work on AArch64 targets. There's a known hardware limitation. arch-ci-linux-cmplx-single
38      args: -fp_trap
39 
40 TEST*/
41