xref: /freebsd/contrib/netbsd-tests/lib/libm/t_sqrt.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /* $NetBSD: t_sqrt.c,v 1.7 2014/03/12 21:40:07 martin 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sqrt.c,v 1.7 2014/03/12 21:40:07 martin Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 #include <float.h>
37 #include <stdio.h>
38 
39 /*
40  * sqrt(3)
41  */
42 ATF_TC(sqrt_nan);
43 ATF_TC_HEAD(sqrt_nan, tc)
44 {
45 	atf_tc_set_md_var(tc, "descr", "Test sqrt(NaN) == NaN");
46 }
47 
48 ATF_TC_BODY(sqrt_nan, tc)
49 {
50 	const double x = 0.0L / 0.0L;
51 
52 	ATF_CHECK(isnan(x) != 0);
53 	ATF_CHECK(isnan(sqrt(x)) != 0);
54 }
55 
56 ATF_TC(sqrt_pow);
57 ATF_TC_HEAD(sqrt_pow, tc)
58 {
59 	atf_tc_set_md_var(tc, "descr", "Test sqrt(3) vs. pow(3)");
60 }
61 
62 ATF_TC_BODY(sqrt_pow, tc)
63 {
64 	const double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
65 #if __DBL_MIN_10_EXP__ <= -40
66 	const double eps = 1.0e-40;
67 #else
68 	const double eps = __DBL_MIN__*4.0;
69 #endif
70 	double y, z;
71 	size_t i;
72 
73 	for (i = 0; i < __arraycount(x); i++) {
74 
75 		y = sqrt(x[i]);
76 		z = pow(x[i], 1.0 / 2.0);
77 
78 		if (fabs(y - z) > eps)
79 			atf_tc_fail_nonfatal("sqrt(%0.03f) != "
80 			    "pow(%0.03f, 1/2)\n", x[i], x[i]);
81 	}
82 }
83 
84 ATF_TC(sqrt_inf_neg);
85 ATF_TC_HEAD(sqrt_inf_neg, tc)
86 {
87 	atf_tc_set_md_var(tc, "descr", "Test sqrt(-Inf) == NaN");
88 }
89 
90 ATF_TC_BODY(sqrt_inf_neg, tc)
91 {
92 	const double x = -1.0L / 0.0L;
93 	double y = sqrt(x);
94 
95 	ATF_CHECK(isnan(y) != 0);
96 }
97 
98 ATF_TC(sqrt_inf_pos);
99 ATF_TC_HEAD(sqrt_inf_pos, tc)
100 {
101 	atf_tc_set_md_var(tc, "descr", "Test sqrt(+Inf) == +Inf");
102 }
103 
104 ATF_TC_BODY(sqrt_inf_pos, tc)
105 {
106 	const double x = 1.0L / 0.0L;
107 	double y = sqrt(x);
108 
109 	ATF_CHECK(isinf(y) != 0);
110 	ATF_CHECK(signbit(y) == 0);
111 }
112 
113 ATF_TC(sqrt_zero_neg);
114 ATF_TC_HEAD(sqrt_zero_neg, tc)
115 {
116 	atf_tc_set_md_var(tc, "descr", "Test sqrt(-0.0) == -0.0");
117 }
118 
119 ATF_TC_BODY(sqrt_zero_neg, tc)
120 {
121 	const double x = -0.0L;
122 	double y = sqrt(x);
123 
124 	if (fabs(y) > 0.0 || signbit(y) == 0)
125 		atf_tc_fail_nonfatal("sqrt(-0.0) != -0.0");
126 }
127 
128 ATF_TC(sqrt_zero_pos);
129 ATF_TC_HEAD(sqrt_zero_pos, tc)
130 {
131 	atf_tc_set_md_var(tc, "descr", "Test sqrt(+0.0) == +0.0");
132 }
133 
134 ATF_TC_BODY(sqrt_zero_pos, tc)
135 {
136 	const double x = 0.0L;
137 	double y = sqrt(x);
138 
139 	if (fabs(y) > 0.0 || signbit(y) != 0)
140 		atf_tc_fail_nonfatal("sqrt(+0.0) != +0.0");
141 }
142 
143 /*
144  * sqrtf(3)
145  */
146 ATF_TC(sqrtf_nan);
147 ATF_TC_HEAD(sqrtf_nan, tc)
148 {
149 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(NaN) == NaN");
150 }
151 
152 ATF_TC_BODY(sqrtf_nan, tc)
153 {
154 	const float x = 0.0L / 0.0L;
155 
156 	ATF_CHECK(isnan(x) != 0);
157 	ATF_CHECK(isnan(sqrtf(x)) != 0);
158 }
159 
160 ATF_TC(sqrtf_powf);
161 ATF_TC_HEAD(sqrtf_powf, tc)
162 {
163 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(3) vs. powf(3)");
164 }
165 
166 ATF_TC_BODY(sqrtf_powf, tc)
167 {
168 	const float x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
169 	const float eps = 1.0e-30;
170 	volatile float y, z;
171 	size_t i;
172 
173 	for (i = 0; i < __arraycount(x); i++) {
174 
175 		y = sqrtf(x[i]);
176 		z = powf(x[i], 1.0 / 2.0);
177 
178 		if (fabsf(y - z) > eps)
179 			atf_tc_fail_nonfatal("sqrtf(%0.03f) != "
180 			    "powf(%0.03f, 1/2)\n", x[i], x[i]);
181 	}
182 }
183 
184 ATF_TC(sqrtf_inf_neg);
185 ATF_TC_HEAD(sqrtf_inf_neg, tc)
186 {
187 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(-Inf) == NaN");
188 }
189 
190 ATF_TC_BODY(sqrtf_inf_neg, tc)
191 {
192 	const float x = -1.0L / 0.0L;
193 	float y = sqrtf(x);
194 
195 	ATF_CHECK(isnan(y) != 0);
196 }
197 
198 ATF_TC(sqrtf_inf_pos);
199 ATF_TC_HEAD(sqrtf_inf_pos, tc)
200 {
201 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(+Inf) == +Inf");
202 }
203 
204 ATF_TC_BODY(sqrtf_inf_pos, tc)
205 {
206 	const float x = 1.0L / 0.0L;
207 	float y = sqrtf(x);
208 
209 	ATF_CHECK(isinf(y) != 0);
210 	ATF_CHECK(signbit(y) == 0);
211 }
212 
213 ATF_TC(sqrtf_zero_neg);
214 ATF_TC_HEAD(sqrtf_zero_neg, tc)
215 {
216 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(-0.0) == -0.0");
217 }
218 
219 ATF_TC_BODY(sqrtf_zero_neg, tc)
220 {
221 	const float x = -0.0L;
222 	float y = sqrtf(x);
223 
224 	if (fabsf(y) > 0.0 || signbit(y) == 0)
225 		atf_tc_fail_nonfatal("sqrtf(-0.0) != -0.0");
226 }
227 
228 ATF_TC(sqrtf_zero_pos);
229 ATF_TC_HEAD(sqrtf_zero_pos, tc)
230 {
231 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(+0.0) == +0.0");
232 }
233 
234 ATF_TC_BODY(sqrtf_zero_pos, tc)
235 {
236 	const float x = 0.0L;
237 	float y = sqrtf(x);
238 
239 	if (fabsf(y) > 0.0 || signbit(y) != 0)
240 		atf_tc_fail_nonfatal("sqrtf(+0.0) != +0.0");
241 }
242 
243 /*
244  * sqrtl(3)
245  */
246 ATF_TC(sqrtl_nan);
247 ATF_TC_HEAD(sqrtl_nan, tc)
248 {
249 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(NaN) == NaN");
250 }
251 
252 ATF_TC_BODY(sqrtl_nan, tc)
253 {
254 	const long double x = 0.0L / 0.0L;
255 
256 	ATF_CHECK(isnan(x) != 0);
257 	ATF_CHECK(isnan(sqrtl(x)) != 0);
258 }
259 
260 ATF_TC(sqrtl_powl);
261 ATF_TC_HEAD(sqrtl_powl, tc)
262 {
263 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(3) vs. powl(3)");
264 }
265 
266 ATF_TC_BODY(sqrtl_powl, tc)
267 {
268 	const long double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
269 	const long double eps = 5.0*DBL_EPSILON; /* XXX powl == pow for now */
270 	volatile long double y, z;
271 	size_t i;
272 
273 	for (i = 0; i < __arraycount(x); i++) {
274 
275 		y = sqrtl(x[i]);
276 		z = powl(x[i], 1.0 / 2.0);
277 
278 		if (fabsl(y - z) > eps)
279 			atf_tc_fail_nonfatal("sqrtl(%0.03Lf) != "
280 			    "powl(%0.03Lf, 1/2)\n", x[i], x[i]);
281 	}
282 }
283 
284 ATF_TC(sqrtl_inf_neg);
285 ATF_TC_HEAD(sqrtl_inf_neg, tc)
286 {
287 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(-Inf) == NaN");
288 }
289 
290 ATF_TC_BODY(sqrtl_inf_neg, tc)
291 {
292 	const long double x = -1.0L / 0.0L;
293 	long double y = sqrtl(x);
294 
295 	ATF_CHECK(isnan(y) != 0);
296 }
297 
298 ATF_TC(sqrtl_inf_pos);
299 ATF_TC_HEAD(sqrtl_inf_pos, tc)
300 {
301 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(+Inf) == +Inf");
302 }
303 
304 ATF_TC_BODY(sqrtl_inf_pos, tc)
305 {
306 	const long double x = 1.0L / 0.0L;
307 	long double y = sqrtl(x);
308 
309 	ATF_CHECK(isinf(y) != 0);
310 	ATF_CHECK(signbit(y) == 0);
311 }
312 
313 ATF_TC(sqrtl_zero_neg);
314 ATF_TC_HEAD(sqrtl_zero_neg, tc)
315 {
316 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(-0.0) == -0.0");
317 }
318 
319 ATF_TC_BODY(sqrtl_zero_neg, tc)
320 {
321 	const long double x = -0.0L;
322 	long double y = sqrtl(x);
323 
324 	if (fabsl(y) > 0.0 || signbit(y) == 0)
325 		atf_tc_fail_nonfatal("sqrtl(-0.0) != -0.0");
326 }
327 
328 ATF_TC(sqrtl_zero_pos);
329 ATF_TC_HEAD(sqrtl_zero_pos, tc)
330 {
331 	atf_tc_set_md_var(tc, "descr", "Test sqrtl(+0.0) == +0.0");
332 }
333 
334 ATF_TC_BODY(sqrtl_zero_pos, tc)
335 {
336 	const long double x = 0.0L;
337 	long double y = sqrtl(x);
338 
339 	if (fabsl(y) > 0.0 || signbit(y) != 0)
340 		atf_tc_fail_nonfatal("sqrtl(+0.0) != +0.0");
341 }
342 
343 ATF_TP_ADD_TCS(tp)
344 {
345 
346 	ATF_TP_ADD_TC(tp, sqrt_nan);
347 	ATF_TP_ADD_TC(tp, sqrt_pow);
348 	ATF_TP_ADD_TC(tp, sqrt_inf_neg);
349 	ATF_TP_ADD_TC(tp, sqrt_inf_pos);
350 	ATF_TP_ADD_TC(tp, sqrt_zero_neg);
351 	ATF_TP_ADD_TC(tp, sqrt_zero_pos);
352 
353 	ATF_TP_ADD_TC(tp, sqrtf_nan);
354 	ATF_TP_ADD_TC(tp, sqrtf_powf);
355 	ATF_TP_ADD_TC(tp, sqrtf_inf_neg);
356 	ATF_TP_ADD_TC(tp, sqrtf_inf_pos);
357 	ATF_TP_ADD_TC(tp, sqrtf_zero_neg);
358 	ATF_TP_ADD_TC(tp, sqrtf_zero_pos);
359 
360 	ATF_TP_ADD_TC(tp, sqrtl_nan);
361 	ATF_TP_ADD_TC(tp, sqrtl_powl);
362 	ATF_TP_ADD_TC(tp, sqrtl_inf_neg);
363 	ATF_TP_ADD_TC(tp, sqrtl_inf_pos);
364 	ATF_TP_ADD_TC(tp, sqrtl_zero_neg);
365 	ATF_TP_ADD_TC(tp, sqrtl_zero_pos);
366 
367 	return atf_no_error();
368 }
369