1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}
|