xref: /minix3/tests/lib/libm/t_atan.c (revision 0b98e8aad89f2bd4ba80b523d73cf29e9dd82ce1)
1 /* $NetBSD: t_atan.c,v 1.9 2013/06/14 05:39:28 isaki Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <atf-c.h>
33 #include <math.h>
34 
35 static const struct {
36 	double x;
37 	double y;
38 } values[] = {
39 	{ -100, -1.560796660108231, },
40 	{  -10, -1.471127674303735, },
41 	{   -1, -M_PI / 4, },
42 	{ -0.1, -0.09966865249116204, },
43 	{  0.1,  0.09966865249116204, },
44 	{    1,  M_PI / 4, },
45 	{   10,  1.471127674303735, },
46 	{  100,  1.560796660108231, },
47 };
48 
49 /*
50  * atan(3)
51  */
52 ATF_TC(atan_nan);
53 ATF_TC_HEAD(atan_nan, tc)
54 {
55 	atf_tc_set_md_var(tc, "descr", "Test atan(NaN) == NaN");
56 }
57 
58 ATF_TC_BODY(atan_nan, tc)
59 {
60 #ifndef __vax__
61 	const double x = 0.0L / 0.0L;
62 
63 	if (isnan(atan(x)) == 0)
64 		atf_tc_fail_nonfatal("atan(NaN) != NaN");
65 #endif
66 }
67 
68 ATF_TC(atan_inf_neg);
69 ATF_TC_HEAD(atan_inf_neg, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "Test atan(-Inf) == -pi/2");
72 }
73 
74 ATF_TC_BODY(atan_inf_neg, tc)
75 {
76 #ifndef __vax__
77 	const double x = -1.0L / 0.0L;
78 	const double eps = 1.0e-15;
79 
80 	if (fabs(atan(x) + M_PI_2) > eps)
81 		atf_tc_fail_nonfatal("atan(-Inf) != -pi/2");
82 #endif
83 }
84 
85 ATF_TC(atan_inf_pos);
86 ATF_TC_HEAD(atan_inf_pos, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Test atan(+Inf) == pi/2");
89 }
90 
91 ATF_TC_BODY(atan_inf_pos, tc)
92 {
93 #ifndef __vax__
94 	const double x = +1.0L / 0.0L;
95 	const double eps = 1.0e-15;
96 
97 	if (fabs(atan(x) - M_PI_2) > eps)
98 		atf_tc_fail_nonfatal("atan(+Inf) != pi/2");
99 #endif
100 }
101 
102 ATF_TC(atan_inrange);
103 ATF_TC_HEAD(atan_inrange, tc)
104 {
105 	atf_tc_set_md_var(tc, "descr", "Test atan(x) for some values");
106 }
107 
108 ATF_TC_BODY(atan_inrange, tc)
109 {
110 #ifndef __vax__
111 	const double eps = 1.0e-15;
112 	size_t i;
113 
114 	for (i = 0; i < __arraycount(values); i++) {
115 		if (fabs(atan(values[i].x) - values[i].y) > eps)
116 			atf_tc_fail_nonfatal("atan(%g) != %g",
117 				values[i].x, values[i].y);
118 	}
119 #endif
120 }
121 
122 ATF_TC(atan_zero_neg);
123 ATF_TC_HEAD(atan_zero_neg, tc)
124 {
125 	atf_tc_set_md_var(tc, "descr", "Test atan(-0.0) == -0.0");
126 }
127 
128 ATF_TC_BODY(atan_zero_neg, tc)
129 {
130 #ifndef __vax__
131 	const double x = -0.0L;
132 	double y = atan(x);
133 
134 	if (fabs(y) > 0.0 || signbit(y) == 0)
135 		atf_tc_fail_nonfatal("atan(-0.0) != -0.0");
136 #endif
137 }
138 
139 ATF_TC(atan_zero_pos);
140 ATF_TC_HEAD(atan_zero_pos, tc)
141 {
142 	atf_tc_set_md_var(tc, "descr", "Test atan(+0.0) == +0.0");
143 }
144 
145 ATF_TC_BODY(atan_zero_pos, tc)
146 {
147 #ifndef __vax__
148 	const double x = 0.0L;
149 	double y = atan(x);
150 
151 	if (fabs(y) > 0.0 || signbit(y) != 0)
152 		atf_tc_fail_nonfatal("atan(+0.0) != +0.0");
153 #endif
154 }
155 
156 /*
157  * atanf(3)
158  */
159 ATF_TC(atanf_nan);
160 ATF_TC_HEAD(atanf_nan, tc)
161 {
162 	atf_tc_set_md_var(tc, "descr", "Test atanf(NaN) == NaN");
163 }
164 
165 ATF_TC_BODY(atanf_nan, tc)
166 {
167 #ifndef __vax__
168 	const float x = 0.0L / 0.0L;
169 
170 	if (isnan(atanf(x)) == 0)
171 		atf_tc_fail_nonfatal("atanf(NaN) != NaN");
172 #endif
173 }
174 
175 ATF_TC(atanf_inf_neg);
176 ATF_TC_HEAD(atanf_inf_neg, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr", "Test atanf(-Inf) == -pi/2");
179 }
180 
181 ATF_TC_BODY(atanf_inf_neg, tc)
182 {
183 #ifndef __vax__
184 	const float x = -1.0L / 0.0L;
185 	const float eps = 1.0e-7;
186 
187 	if (fabsf(atanf(x) + M_PI_2) > eps)
188 		atf_tc_fail_nonfatal("atanf(-Inf) != -pi/2");
189 #endif
190 }
191 
192 ATF_TC(atanf_inf_pos);
193 ATF_TC_HEAD(atanf_inf_pos, tc)
194 {
195 	atf_tc_set_md_var(tc, "descr", "Test atanf(+Inf) == pi/2");
196 }
197 
198 ATF_TC_BODY(atanf_inf_pos, tc)
199 {
200 #ifndef __vax__
201 	const float x = +1.0L / 0.0L;
202 	const float eps = 1.0e-7;
203 
204 	if (fabsf(atanf(x) - M_PI_2) > eps)
205 		atf_tc_fail_nonfatal("atanf(+Inf) != pi/2");
206 #endif
207 }
208 
209 ATF_TC(atanf_inrange);
210 ATF_TC_HEAD(atanf_inrange, tc)
211 {
212 	atf_tc_set_md_var(tc, "descr", "Test atanf(x) for some values");
213 }
214 
215 ATF_TC_BODY(atanf_inrange, tc)
216 {
217 #ifndef __vax__
218 	const float eps = 1.0e-7;
219 	float x;
220 	float y;
221 	size_t i;
222 
223 	for (i = 0; i < __arraycount(values); i++) {
224 		x = values[i].x;
225 		y = values[i].y;
226 		if (fabs(atanf(x) - y) > eps)
227 			atf_tc_fail_nonfatal("atan(%g) != %g", x, y);
228 	}
229 #endif
230 }
231 
232 ATF_TC(atanf_zero_neg);
233 ATF_TC_HEAD(atanf_zero_neg, tc)
234 {
235 	atf_tc_set_md_var(tc, "descr", "Test atanf(-0.0) == -0.0");
236 }
237 
238 ATF_TC_BODY(atanf_zero_neg, tc)
239 {
240 #ifndef __vax__
241 	const float x = -0.0L;
242 	float y = atanf(x);
243 
244 	if (fabsf(y) > 0.0 || signbit(y) == 0)
245 		atf_tc_fail_nonfatal("atanf(-0.0) != -0.0");
246 #endif
247 }
248 
249 ATF_TC(atanf_zero_pos);
250 ATF_TC_HEAD(atanf_zero_pos, tc)
251 {
252 	atf_tc_set_md_var(tc, "descr", "Test atanf(+0.0) == +0.0");
253 }
254 
255 ATF_TC_BODY(atanf_zero_pos, tc)
256 {
257 #ifndef __vax__
258 	const float x = 0.0L;
259 	float y = atanf(x);
260 
261 	if (fabsf(y) > 0.0 || signbit(y) != 0)
262 		atf_tc_fail_nonfatal("atanf(+0.0) != +0.0");
263 #endif
264 }
265 
266 ATF_TP_ADD_TCS(tp)
267 {
268 
269 	ATF_TP_ADD_TC(tp, atan_nan);
270 	ATF_TP_ADD_TC(tp, atan_inf_neg);
271 	ATF_TP_ADD_TC(tp, atan_inf_pos);
272 	ATF_TP_ADD_TC(tp, atan_inrange);
273 	ATF_TP_ADD_TC(tp, atan_zero_neg);
274 	ATF_TP_ADD_TC(tp, atan_zero_pos);
275 
276 	ATF_TP_ADD_TC(tp, atanf_nan);
277 	ATF_TP_ADD_TC(tp, atanf_inf_neg);
278 	ATF_TP_ADD_TC(tp, atanf_inf_pos);
279 	ATF_TP_ADD_TC(tp, atanf_inrange);
280 	ATF_TP_ADD_TC(tp, atanf_zero_neg);
281 	ATF_TP_ADD_TC(tp, atanf_zero_pos);
282 
283 	return atf_no_error();
284 }
285