xref: /netbsd-src/tests/lib/libm/t_atan.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
1 /* $NetBSD: t_atan.c,v 1.3 2012/02/28 08:58:39 pgoyette 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 /*
36  * atan(3)
37  */
38 ATF_TC(atan_nan);
39 ATF_TC_HEAD(atan_nan, tc)
40 {
41 	atf_tc_set_md_var(tc, "descr", "Test atan(NaN) == NaN");
42 }
43 
44 ATF_TC_BODY(atan_nan, tc)
45 {
46 #ifndef __vax__
47 	const double x = 0.0L / 0.0L;
48 
49 	if (isnan(atan(x)) == 0)
50 		atf_tc_fail_nonfatal("atan(NaN) != NaN");
51 #endif
52 }
53 
54 ATF_TC(atan_inf_neg);
55 ATF_TC_HEAD(atan_inf_neg, tc)
56 {
57 	atf_tc_set_md_var(tc, "descr", "Test atan(-Inf) == -pi/2");
58 }
59 
60 ATF_TC_BODY(atan_inf_neg, tc)
61 {
62 #ifndef __vax__
63 	const double x = -1.0L / 0.0L;
64 	const double eps = 1.0e-40;
65 
66 	if (fabs(atan(x) + M_PI_2) > eps)
67 		atf_tc_fail_nonfatal("atan(-Inf) != -pi/2");
68 #endif
69 }
70 
71 ATF_TC(atan_inf_pos);
72 ATF_TC_HEAD(atan_inf_pos, tc)
73 {
74 	atf_tc_set_md_var(tc, "descr", "Test atan(+Inf) == pi/2");
75 }
76 
77 ATF_TC_BODY(atan_inf_pos, tc)
78 {
79 #ifndef __vax__
80 	const double x = +1.0L / 0.0L;
81 	const double eps = 1.0e-40;
82 
83 	if (fabs(atan(x) - M_PI_2) > eps)
84 		atf_tc_fail_nonfatal("atan(+Inf) != pi/2");
85 #endif
86 }
87 
88 ATF_TC(atan_tan);
89 ATF_TC_HEAD(atan_tan, tc)
90 {
91 	atf_tc_set_md_var(tc, "descr", "Test atan(tan(x)) == x");
92 }
93 
94 ATF_TC_BODY(atan_tan, tc)
95 {
96 #ifndef __vax__
97 	const double x[] = { 0.0, 1.0, M_PI / 2, M_PI / 3, M_PI / 6 };
98 	const double eps = 1.0e-40;
99 	double y;
100 	size_t i;
101 
102 	for (i = 0; i < __arraycount(x); i++) {
103 
104 		y = atan(tan(x[i]));
105 
106 		if (fabs(y - x[i]) > eps)
107 			atf_tc_fail_nonfatal("atan(tan(%0.03f)) != %0.03f",
108 			    x[i], x[i]);
109 	}
110 #endif
111 }
112 
113 ATF_TC(atan_zero_neg);
114 ATF_TC_HEAD(atan_zero_neg, tc)
115 {
116 	atf_tc_set_md_var(tc, "descr", "Test atan(-0.0) == -0.0");
117 }
118 
119 ATF_TC_BODY(atan_zero_neg, tc)
120 {
121 #ifndef __vax__
122 	const double x = -0.0L;
123 	double y = atan(x);
124 
125 	if (fabs(y) > 0.0 || signbit(y) == 0)
126 		atf_tc_fail_nonfatal("atan(-0.0) != -0.0");
127 #endif
128 }
129 
130 ATF_TC(atan_zero_pos);
131 ATF_TC_HEAD(atan_zero_pos, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "Test atan(+0.0) == +0.0");
134 }
135 
136 ATF_TC_BODY(atan_zero_pos, tc)
137 {
138 #ifndef __vax__
139 	const double x = 0.0L;
140 	double y = atan(x);
141 
142 	if (fabs(y) > 0.0 || signbit(y) != 0)
143 		atf_tc_fail_nonfatal("atan(+0.0) != +0.0");
144 #endif
145 }
146 
147 /*
148  * atanf(3)
149  */
150 ATF_TC(atanf_nan);
151 ATF_TC_HEAD(atanf_nan, tc)
152 {
153 	atf_tc_set_md_var(tc, "descr", "Test atanf(NaN) == NaN");
154 }
155 
156 ATF_TC_BODY(atanf_nan, tc)
157 {
158 #ifndef __vax__
159 	const float x = 0.0L / 0.0L;
160 
161 	if (isnan(atanf(x)) == 0)
162 		atf_tc_fail_nonfatal("atanf(NaN) != NaN");
163 #endif
164 }
165 
166 ATF_TC(atanf_inf_neg);
167 ATF_TC_HEAD(atanf_inf_neg, tc)
168 {
169 	atf_tc_set_md_var(tc, "descr", "Test atanf(-Inf) == -pi/2");
170 }
171 
172 ATF_TC_BODY(atanf_inf_neg, tc)
173 {
174 #ifndef __vax__
175 	const float x = -1.0L / 0.0L;
176 	const float eps = 1.0e-7;
177 
178 	if (fabsf(atanf(x) + M_PI_2) > eps)
179 		atf_tc_fail_nonfatal("atanf(-Inf) != -pi/2");
180 #endif
181 }
182 
183 ATF_TC(atanf_inf_pos);
184 ATF_TC_HEAD(atanf_inf_pos, tc)
185 {
186 	atf_tc_set_md_var(tc, "descr", "Test atanf(+Inf) == pi/2");
187 }
188 
189 ATF_TC_BODY(atanf_inf_pos, tc)
190 {
191 #ifndef __vax__
192 	const float x = +1.0L / 0.0L;
193 	const float eps = 1.0e-7;
194 
195 	if (fabsf(atanf(x) - M_PI_2) > eps)
196 		atf_tc_fail_nonfatal("atanf(+Inf) != pi/2");
197 #endif
198 }
199 
200 ATF_TC(atanf_tanf);
201 ATF_TC_HEAD(atanf_tanf, tc)
202 {
203 	atf_tc_set_md_var(tc, "descr", "Test atanf(tanf(x)) == x");
204 }
205 
206 ATF_TC_BODY(atanf_tanf, tc)
207 {
208 #ifndef __vax__
209 	const float x[] = { 0.0, 1.0, M_PI / 3, M_PI / 6 };
210 	const float eps = 1.0e-7;
211 	float y;
212 	size_t i;
213 
214 	for (i = 0; i < __arraycount(x); i++) {
215 
216 		y = atanf(tanf(x[i]));
217 
218 		if (fabsf(y - x[i]) > eps)
219 			atf_tc_fail_nonfatal("atanf(tanf(%0.03f)) != %0.03f",
220 			    x[i], x[i]);
221 	}
222 #endif
223 }
224 
225 ATF_TC(atanf_zero_neg);
226 ATF_TC_HEAD(atanf_zero_neg, tc)
227 {
228 	atf_tc_set_md_var(tc, "descr", "Test atanf(-0.0) == -0.0");
229 }
230 
231 ATF_TC_BODY(atanf_zero_neg, tc)
232 {
233 #ifndef __vax__
234 	const float x = -0.0L;
235 	float y = atanf(x);
236 
237 	if (fabsf(y) > 0.0 || signbit(y) == 0)
238 		atf_tc_fail_nonfatal("atanf(-0.0) != -0.0");
239 #endif
240 }
241 
242 ATF_TC(atanf_zero_pos);
243 ATF_TC_HEAD(atanf_zero_pos, tc)
244 {
245 	atf_tc_set_md_var(tc, "descr", "Test atanf(+0.0) == +0.0");
246 }
247 
248 ATF_TC_BODY(atanf_zero_pos, tc)
249 {
250 #ifndef __vax__
251 	const float x = 0.0L;
252 	float y = atanf(x);
253 
254 	if (fabsf(y) > 0.0 || signbit(y) != 0)
255 		atf_tc_fail_nonfatal("atanf(+0.0) != +0.0");
256 #endif
257 }
258 
259 ATF_TP_ADD_TCS(tp)
260 {
261 
262 	ATF_TP_ADD_TC(tp, atan_nan);
263 	ATF_TP_ADD_TC(tp, atan_inf_neg);
264 	ATF_TP_ADD_TC(tp, atan_inf_pos);
265 	ATF_TP_ADD_TC(tp, atan_tan);
266 	ATF_TP_ADD_TC(tp, atan_zero_neg);
267 	ATF_TP_ADD_TC(tp, atan_zero_pos);
268 
269 	ATF_TP_ADD_TC(tp, atanf_nan);
270 	ATF_TP_ADD_TC(tp, atanf_inf_neg);
271 	ATF_TP_ADD_TC(tp, atanf_inf_pos);
272 	ATF_TP_ADD_TC(tp, atanf_tanf);
273 	ATF_TP_ADD_TC(tp, atanf_zero_neg);
274 	ATF_TP_ADD_TC(tp, atanf_zero_pos);
275 
276 	return atf_no_error();
277 }
278