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