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