summaryrefslogtreecommitdiff
path: root/awklib/eg/test-programs/gen-float-table.c
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2020-11-15 11:54:52 +0200
committerArnold D. Robbins <arnold@skeeve.com>2020-11-15 11:54:52 +0200
commit6d1274d1d0c9b2f0feb109876c7c250951035a3c (patch)
tree8166de2cc662c0f54c82ecce98e8061bb5b216b1 /awklib/eg/test-programs/gen-float-table.c
parent3aadf44caf614edcdb7bb9ccc2a689db9a784a21 (diff)
downloadgawk-6d1274d1d0c9b2f0feb109876c7c250951035a3c.tar.gz
Add test programs embedded in the doc.feature/cmp_scalars
Diffstat (limited to 'awklib/eg/test-programs/gen-float-table.c')
-rw-r--r--awklib/eg/test-programs/gen-float-table.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/awklib/eg/test-programs/gen-float-table.c b/awklib/eg/test-programs/gen-float-table.c
new file mode 100644
index 00000000..ae1d5dd4
--- /dev/null
+++ b/awklib/eg/test-programs/gen-float-table.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <math.h>
+#include <stdbool.h>
+
+#define def_func(name, op) \
+ bool name(double left, double right) { \
+ return left op right; \
+ }
+
+def_func(eq, ==)
+def_func(ne, !=)
+def_func(lt, <)
+def_func(le, <=)
+def_func(gt, >)
+def_func(ge, >=)
+
+struct {
+ const char *name;
+ bool (*func)(double left, double right);
+} functions[] = {
+ { "==", eq },
+ { "!=", ne },
+ { "< ", lt },
+ { "<=", le },
+ { "> ", gt },
+ { ">=", ge },
+ { 0, 0 }
+};
+
+int main()
+{
+ double values[] = {
+ -sqrt(-1), // nan
+ sqrt(-1), // -nan
+ -log(0.0), // inf
+ log(0.0) // -inf
+ };
+ double compare[] = { 2.0,
+ -sqrt(-1), // nan
+ sqrt(-1), // -nan
+ -log(0.0), // inf
+ log(0.0) // -inf
+ };
+
+ int i, j, k;
+
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 5; j++) {
+ for (k = 0; functions[k].name != NULL; k++) {
+ printf("%g %s %g -> %s\n", values[i],
+ functions[k].name,
+ compare[j],
+ functions[k].func(values[i], compare[j]) ? "true" : "false");
+ }
+ printf("\n");
+ }
+ }
+
+ return 0;
+}