File: test-math.c

package info (click to toggle)
goffice 0.10.57-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,136 kB
  • sloc: ansic: 114,820; sh: 4,993; makefile: 1,241; perl: 235; xml: 232
file content (177 lines) | stat: -rw-r--r-- 5,119 bytes parent folder | download | duplicates (5)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <goffice/goffice.h>

#define REFTEST(a_,f_,r_, txt_)						\
	do {								\
		double a = (a_);					\
		double r = (r_);					\
		double fa = f_(a);					\
		g_printerr ("%s(%g) = %g  [%g]\n",			\
			    txt_, a, fa, r);				\
		g_assert (copysign (1,r) == copysign (1,fa));		\
		if (isnan (r))						\
			g_assert (isnan (fa));				\
		else if (r == floor (r))				\
			g_assert (r == fa);				\
		else							\
			g_assert (fabs (r - fa) / fabs (r) < 1e-14);	\
	} while (0)

#define REFTEST2(a_,b_,f_,r_, txt_)					\
	do {								\
		double a = (a_);					\
		double b = (b_);					\
		double r = (r_);					\
		double fab = f_(a, b);					\
		g_printerr ("%s(%g,%g) = %g  [%g]\n",			\
			    txt_, a, b, fab, r);			\
		g_assert (copysign (1,r) == copysign(1,fab));		\
		if (r * 256 == floor (r * 256))				\
			g_assert (r == fab);				\
		else							\
			g_assert (fabs (r - fab) / fabs (r) < 1e-14);	\
	} while (0)

#ifdef GOFFICE_WITH_LONG_DOUBLE

#define REFTESTl(a_,f_,r_, txt_)					\
	do {								\
		long double a = (a_);					\
		long double r = (r_);					\
		long double fa = f_(a);					\
		g_printerr ("%s(%Lg) = %Lg  [%Lg]\n",			\
			    txt_, a, fa, r);				\
		g_assert (copysignl (1,r) == copysignl (1,fa));		\
		if (isnanl (r))						\
			g_assert (isnanl (fa));				\
		else if (r == floorl (r))				\
			g_assert (r == fa);				\
		else							\
			g_assert (fabsl (r - fa) / fabsl (r) < 1e-14);	\
	} while (0)

#define REFTEST2l(a_,b_,f_,r_, txt_)					\
	do {								\
		long double a = (a_);					\
		long double b = (b_);					\
		long double r = (r_);					\
		long double fab = f_(a, b);				\
		g_printerr ("%s(%Lg,%Lg) = %Lg  [%Lg]\n",		\
			    txt_, a, b, fab, r);			\
		g_assert (copysignl (1,r) == copysignl(1,fab));		\
		if (r * 256 == floorl (r * 256))			\
			g_assert (r == fab);				\
		else							\
			g_assert (fabsl (r - fab) / fabsl (r) < 1e-14);	\
	} while (0)
#else
#define REFTESTl(a_,f_,r_, txt_)
#define REFTEST2l(a_,b_,f_,r_, txt_)
#endif

/* ------------------------------------------------------------------------- */

static double fake_sinpi (double x) { return x == floor (x) ? copysign(0,x) : sin (x * M_PI); }
static double fake_cospi (double x) { return x + 0.5 == floor (x + 0.5) ? +0 : cos (x * M_PI); }
static double fake_tanpi (double x) { x = fmod(x,1); return x + 0.5 == floor (x + 0.5) ? copysign (go_nan,x) : tan (x * M_PI); }
static double fake_cotpi (double x) { x = fmod(x,1); return x == 0 ? copysign (go_nan,x) : fake_cospi (x) / fake_sinpi (x); }
static double fake_atanpi (double x) { return atan (x) / M_PI; }
static double fake_atan2pi (double y, double x) { return atan2 (y,x) / M_PI; }


#define TEST1(a_) do {							\
	REFTEST(a_,go_sinpi,fake_sinpi(a_),"sinpi");			\
	REFTEST(a_,go_cospi,fake_cospi(a_),"cospi");			\
	REFTEST(a_,go_tanpi,fake_tanpi(a_),"tanpi");			\
	REFTEST(a_,go_cotpi,fake_cotpi(a_),"cotpi");			\
	REFTEST(a_,go_atanpi,fake_atanpi(a_),"atanpi");			\
									\
	REFTESTl(a_,go_sinpil,fake_sinpi(a_),"sinpil");			\
	REFTESTl(a_,go_cospil,fake_cospi(a_),"cospil");			\
	REFTESTl(a_,go_tanpil,fake_tanpi(a_),"tanpil");			\
	REFTESTl(a_,go_cotpil,fake_cotpi(a_),"cotpil");			\
	REFTESTl(a_,go_atanpil,fake_atanpi(a_),"atanpil");		\
} while (0)

#define TEST2(a_,b_) do {						\
		REFTEST2(a_,b_,go_atan2pi,fake_atan2pi(a_,b_),"atan2pi"); \
		REFTEST2l(a_,b_,go_atan2pil,fake_atan2pi(a_,b_),"atan2pil"); \
} while (0)

static void
trig_tests (void)
{
	double d;

	for (d = 0; d < 5; d += 0.125) {
		TEST1(d);
		TEST1(-d);
	}

	TEST2 (0, +2);
	TEST2 (0, -2);
	TEST2 (3, 0);
	TEST2 (-3, 0);
	TEST2 (0, 0);
	TEST2 (+1, +1);
	TEST2 (-1, +1);
	TEST2 (+1, -1);
	TEST2 (-1, -1);
	TEST2 (+2.3, +1.2);
	TEST2 (+2.3, -1.2);
	TEST2 (+2.3, +0.2);
	TEST2 (+2.3, -0.2);
	TEST2 (+0.2, +2.3);
	TEST2 (+0.2, -2.3);
	TEST2 (+2.3, +3);
	TEST2 (+2.3, -3);
	TEST2 (-2.3, +3);
	TEST2 (-2.3, -3);
	TEST2 (+2.3, +4);
	TEST2 (+2.3, -4);
	TEST2 (-2.3, +4);
	TEST2 (-2.3, -4);
	TEST2 (2, 100);
	TEST2 (2, -100);
	TEST2 (1.0/256, 1.0/1024);
	TEST2 (exp(1), 1);
	TEST2 (exp(1), -1);
	TEST2 (exp(1), log(2));
	TEST2 (exp(1), -log(2));

	/* Since the results are n/4 these are tested exactly.  */
	REFTEST2(0,1,go_atan2pi,0,"atan2pi");
	REFTEST2(1,1,go_atan2pi,0.25,"atan2pi");
	REFTEST2(1,0,go_atan2pi,0.5,"atan2pi");
	REFTEST2(1,-1,go_atan2pi,0.75,"atan2pi");
	REFTEST2(0,-1,go_atan2pi,1,"atan2pi");
	REFTEST2(-1,1,go_atan2pi,-0.25,"atan2pi");
	REFTEST2(-1,0,go_atan2pi,-0.5,"atan2pi");
	REFTEST2(-1,-1,go_atan2pi,-0.75,"atan2pi");

	/* Since the results are n/4 these are tested exactly.  */
	REFTEST2l(0,1,go_atan2pil,0,"atan2pil");
	REFTEST2l(1,1,go_atan2pil,0.25,"atan2pil");
	REFTEST2l(1,0,go_atan2pil,0.5,"atan2pil");
	REFTEST2l(1,-1,go_atan2pil,0.75,"atan2pil");
	REFTEST2l(0,-1,go_atan2pil,1,"atan2pil");
	REFTEST2l(-1,1,go_atan2pil,-0.25,"atan2pil");
	REFTEST2l(-1,0,go_atan2pil,-0.5,"atan2pil");
	REFTEST2l(-1,-1,go_atan2pil,-0.75,"atan2pil");
}

#undef TEST1
#undef TEST2

/* ------------------------------------------------------------------------- */

int
main (int argc, char **argv)
{
	libgoffice_init ();

	trig_tests ();

	libgoffice_shutdown ();

	return 0;
}