xref: /haiku/src/tests/system/libroot/posix/math/math_test.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2005, Andrew Bachmann, andrewbachmann@myrealbox.com
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <OS.h>
8 #include <image.h>
9 #include <cmath>
10 #include <cassert>
11 #include <cstdio>
12 
13 static image_id
14 get_libroot_id()
15 {
16 	image_info info;
17 	int32 cookie = 0;
18 	while (get_next_image_info(0, &cookie, &info) == B_OK) {
19 		if (strcmp(info.name, "/boot/beos/system/lib/libroot.so") == 0) {
20 			return info.id;
21 		}
22 	}
23 	return B_BAD_VALUE;
24 }
25 
26 
27 static double (*be_sin)(double);
28 static double (*be_cos)(double);
29 static double (*be_copysign)(double, double);
30 static double (*be_drem)(double, double);
31 
32 
33 int
34 main(int argc, char **argv)
35 {
36 	image_id libroot = get_libroot_id();
37 	assert(get_image_symbol(libroot, "sin", B_SYMBOL_TYPE_TEXT, (void**)&be_sin) == B_OK);
38 	assert(get_image_symbol(libroot, "cos", B_SYMBOL_TYPE_TEXT, (void**)&be_cos) == B_OK);
39 	assert(get_image_symbol(libroot, "copysign", B_SYMBOL_TYPE_TEXT, (void**)&be_copysign) == B_OK);
40 	assert(get_image_symbol(libroot, "drem", B_SYMBOL_TYPE_TEXT, (void**)&be_drem) == B_OK);
41 
42 	status_t result = B_OK;
43 
44 	// test cos2 + sin2 = 1
45 	fprintf(stdout, "value\tsin2()+cos2()\n");
46 	for (int i = -63 ; i <= 63 ; i++) {
47 		double f = (double)i/7.0;
48 		double x = sin(f);
49 		double y = cos(f);
50 		if (fabs(x*x + y*y - 1.0) > 0.000001) {
51 			fprintf(stdout, "%0.3f\t%0.10f", f, x*x + y*y);
52 			fprintf(stdout, " **");
53 			fprintf(stdout, "\n");
54 			result = B_ERROR;
55 		}
56 	}
57 	fprintf(stdout, "\n");
58 
59 	// test sin
60 	fprintf(stdout, "value\tsin(value)\tbe_sin(value)\n");
61 	for (int i = -63 ; i <= 63 ; i++) {
62 		double f = (double)i/7.0;
63 		double x = sin(f);
64 		double y = be_sin(f);
65 		if (fabs(x - y) > 0.000001) {
66 			fprintf(stdout, "%0.3f\t%0.10f\t%0.10f", f, x, y);
67 			fprintf(stdout, " **");
68 			fprintf(stdout, "\n");
69 			result = B_ERROR;
70 		}
71 	}
72 	fprintf(stdout, "\n");
73 
74 	fprintf(stdout, "value\tcos(value)\tbe_cos(value)\n");
75 	// test cos
76 	for (int i = -63 ; i <= 63 ; i++) {
77 		double f = (double)i/7.0;
78 		double x = cos(f);
79 		double y = be_cos(f);
80 		if (fabs(x - y) > 0.000001) {
81 			fprintf(stdout, "%0.3f\t%0.10f\t%0.10f", f, x, y);
82 			fprintf(stdout, " **");
83 			fprintf(stdout, "\n");
84 			result = B_ERROR;
85 		}
86 	}
87 	fprintf(stdout, "\n");
88 
89 	fprintf(stdout, "arg1\targ2\tcopysign()\tbe_copysign()\n");
90 	// test copysign
91 	for (int i = -21 ; i <= 21 ; i++) {
92 		for (int j = -36 ; j <= 36 ; j++) {
93 			double f = (double)i/3.0;
94 			double g = (double)j/4.0;
95 			double x = copysign(f, g);
96 			double y = be_copysign(f, g);
97 			if ((x != y) && !(isnan(x) && isnan(y))) {
98 				fprintf(stdout, "%0.1f\t%0.1f\t%0.6f\t%0.6f", f, g, x, y);
99 				fprintf(stdout, " **");
100 				fprintf(stdout, "\n");
101 				result = B_ERROR;
102 			}
103 		}
104 	}
105 	fprintf(stdout, "\n");
106 
107 	fprintf(stdout, "arg1\targ2\tdrem(values)\tbe_drem(values)\n");
108 	// test drem
109 	for (int i = -21 ; i <= 21 ; i++) {
110 		for (int j = -36 ; j <= 36 ; j++) {
111 			double f = (double)i/3.0;
112 			double g = (double)j/4.0;
113 			double x = drem(f, g);
114 			double y = be_drem(f, g);
115 			if ((x != y) && !(isnan(x) && isnan(y))) {
116 				fprintf(stdout, "%0.2f\t%0.2f\t%0.10f\t%0.10f", f, g, x, y);
117 				fprintf(stdout, " **");
118 				fprintf(stdout, "\n");
119 				result = B_ERROR;
120 			}
121 		}
122 	}
123 	fprintf(stdout, "\n");
124 
125 	fprintf(stdout, "result = %s\n", (result == B_OK ? "OK" : "ERROR"));
126 	return result;
127 }
128