1 /* $NetBSD: t_cbrt.c,v 1.6 2024/04/03 01:52:28 christos 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_cbrt.c,v 1.6 2024/04/03 01:52:28 christos Exp $");
33
34 #include <atf-c.h>
35 #include <float.h>
36 #include <math.h>
37 #include <stdio.h>
38
39 /*
40 * cbrt(3)
41 */
42 ATF_TC(cbrt_nan);
ATF_TC_HEAD(cbrt_nan,tc)43 ATF_TC_HEAD(cbrt_nan, tc)
44 {
45 atf_tc_set_md_var(tc, "descr", "Test cbrt(NaN) == NaN");
46 }
47
ATF_TC_BODY(cbrt_nan,tc)48 ATF_TC_BODY(cbrt_nan, tc)
49 {
50 const double x = 0.0L / 0.0L;
51
52 ATF_CHECK(isnan(x) != 0);
53 ATF_CHECK(isnan(cbrt(x)) != 0);
54 }
55
56 ATF_TC(cbrt_pow);
ATF_TC_HEAD(cbrt_pow,tc)57 ATF_TC_HEAD(cbrt_pow, tc)
58 {
59 atf_tc_set_md_var(tc, "descr", "Test cbrt(3) vs. pow(3)");
60 }
61
ATF_TC_BODY(cbrt_pow,tc)62 ATF_TC_BODY(cbrt_pow, tc)
63 {
64 const double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
65 /* Neither cbrt nor pow is required to be correctly rounded. */
66 const double eps = 2*DBL_EPSILON;
67 size_t i;
68
69 for (i = 0; i < __arraycount(x); i++) {
70 double x_cbrt = cbrt(x[i]);
71 double x_pow13 = pow(x[i], 1.0 / 3.0);
72 bool ok;
73
74 if (x[i] == 0) {
75 ok = (x_cbrt == x_pow13);
76 } else {
77 ok = (fabs((x_cbrt - x_pow13)/x_cbrt) <= eps);
78 }
79
80 if (!ok) {
81 atf_tc_fail_nonfatal("cbrt(%.17g) = %.17g != "
82 "pow(%.17g, 1/3) = %.17g\n",
83 x[i], x_cbrt, x[i], x_pow13);
84 }
85 }
86 }
87
88 ATF_TC(cbrt_inf_neg);
ATF_TC_HEAD(cbrt_inf_neg,tc)89 ATF_TC_HEAD(cbrt_inf_neg, tc)
90 {
91 atf_tc_set_md_var(tc, "descr", "Test cbrt(-Inf) == -Inf");
92 }
93
ATF_TC_BODY(cbrt_inf_neg,tc)94 ATF_TC_BODY(cbrt_inf_neg, tc)
95 {
96 const double x = -1.0L / 0.0L;
97 double y = cbrt(x);
98
99 ATF_CHECK(isinf(y) != 0);
100 ATF_CHECK(signbit(y) != 0);
101 }
102
103 ATF_TC(cbrt_inf_pos);
ATF_TC_HEAD(cbrt_inf_pos,tc)104 ATF_TC_HEAD(cbrt_inf_pos, tc)
105 {
106 atf_tc_set_md_var(tc, "descr", "Test cbrt(+Inf) == +Inf");
107 }
108
ATF_TC_BODY(cbrt_inf_pos,tc)109 ATF_TC_BODY(cbrt_inf_pos, tc)
110 {
111 const double x = 1.0L / 0.0L;
112 double y = cbrt(x);
113
114 ATF_CHECK(isinf(y) != 0);
115 ATF_CHECK(signbit(y) == 0);
116 }
117
118 ATF_TC(cbrt_zero_neg);
ATF_TC_HEAD(cbrt_zero_neg,tc)119 ATF_TC_HEAD(cbrt_zero_neg, tc)
120 {
121 atf_tc_set_md_var(tc, "descr", "Test cbrt(-0.0) == -0.0");
122 }
123
ATF_TC_BODY(cbrt_zero_neg,tc)124 ATF_TC_BODY(cbrt_zero_neg, tc)
125 {
126 const double x = -0.0L;
127 double y = cbrt(x);
128
129 if (fabs(y) > 0.0 || signbit(y) == 0)
130 atf_tc_fail_nonfatal("cbrt(-0.0) != -0.0");
131 }
132
133 ATF_TC(cbrt_zero_pos);
ATF_TC_HEAD(cbrt_zero_pos,tc)134 ATF_TC_HEAD(cbrt_zero_pos, tc)
135 {
136 atf_tc_set_md_var(tc, "descr", "Test cbrt(+0.0) == +0.0");
137 }
138
ATF_TC_BODY(cbrt_zero_pos,tc)139 ATF_TC_BODY(cbrt_zero_pos, tc)
140 {
141 const double x = 0.0L;
142 double y = cbrt(x);
143
144 if (fabs(y) > 0.0 || signbit(y) != 0)
145 atf_tc_fail_nonfatal("cbrt(+0.0) != +0.0");
146 }
147
148 /*
149 * cbrtf(3)
150 */
151 ATF_TC(cbrtf_nan);
ATF_TC_HEAD(cbrtf_nan,tc)152 ATF_TC_HEAD(cbrtf_nan, tc)
153 {
154 atf_tc_set_md_var(tc, "descr", "Test cbrtf(NaN) == NaN");
155 }
156
ATF_TC_BODY(cbrtf_nan,tc)157 ATF_TC_BODY(cbrtf_nan, tc)
158 {
159 const float x = 0.0L / 0.0L;
160
161 ATF_CHECK(isnan(x) != 0);
162 ATF_CHECK(isnan(cbrtf(x)) != 0);
163 }
164
165 ATF_TC(cbrtf_powf);
ATF_TC_HEAD(cbrtf_powf,tc)166 ATF_TC_HEAD(cbrtf_powf, tc)
167 {
168 atf_tc_set_md_var(tc, "descr", "Test cbrtf(3) vs. powf(3)");
169 }
170
ATF_TC_BODY(cbrtf_powf,tc)171 ATF_TC_BODY(cbrtf_powf, tc)
172 {
173 const float x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
174 /* Neither cbrt nor pow is required to be correctly rounded. */
175 const float eps = 2*FLT_EPSILON;
176 size_t i;
177
178 for (i = 0; i < __arraycount(x); i++) {
179 float x_cbrt = cbrtf(x[i]);
180 float x_pow13 = powf(x[i], 1.0 / 3.0);
181 bool ok;
182
183 if (x[i] == 0) {
184 ok = (x_cbrt == x_pow13);
185 } else {
186 ok = (fabsf((x_cbrt - x_pow13)/x_cbrt) <= eps);
187 }
188
189 if (!ok) {
190 atf_tc_fail_nonfatal("cbrtf(%.9g) = %.9g. != "
191 "powf(%.9g, 1/3) = %.9g\n",
192 (double)x[i], (double)x_cbrt,
193 (double)x[i], (double)x_pow13);
194 }
195 }
196 }
197
198 ATF_TC(cbrtf_inf_neg);
ATF_TC_HEAD(cbrtf_inf_neg,tc)199 ATF_TC_HEAD(cbrtf_inf_neg, tc)
200 {
201 atf_tc_set_md_var(tc, "descr", "Test cbrtf(-Inf) == -Inf");
202 }
203
ATF_TC_BODY(cbrtf_inf_neg,tc)204 ATF_TC_BODY(cbrtf_inf_neg, tc)
205 {
206 const float x = -1.0L / 0.0L;
207 float y = cbrtf(x);
208
209 ATF_CHECK(isinf(y) != 0);
210 ATF_CHECK(signbit(y) != 0);
211 }
212
213 ATF_TC(cbrtf_inf_pos);
ATF_TC_HEAD(cbrtf_inf_pos,tc)214 ATF_TC_HEAD(cbrtf_inf_pos, tc)
215 {
216 atf_tc_set_md_var(tc, "descr", "Test cbrtf(+Inf) == +Inf");
217 }
218
ATF_TC_BODY(cbrtf_inf_pos,tc)219 ATF_TC_BODY(cbrtf_inf_pos, tc)
220 {
221 const float x = 1.0L / 0.0L;
222 float y = cbrtf(x);
223
224 ATF_CHECK(isinf(y) != 0);
225 ATF_CHECK(signbit(y) == 0);
226 }
227
228 ATF_TC(cbrtf_zero_neg);
ATF_TC_HEAD(cbrtf_zero_neg,tc)229 ATF_TC_HEAD(cbrtf_zero_neg, tc)
230 {
231 atf_tc_set_md_var(tc, "descr", "Test cbrtf(-0.0) == -0.0");
232 }
233
ATF_TC_BODY(cbrtf_zero_neg,tc)234 ATF_TC_BODY(cbrtf_zero_neg, tc)
235 {
236 const float x = -0.0L;
237 float y = cbrtf(x);
238
239 if (fabsf(y) > 0.0 || signbit(y) == 0)
240 atf_tc_fail_nonfatal("cbrtf(-0.0) != -0.0");
241 }
242
243 ATF_TC(cbrtf_zero_pos);
ATF_TC_HEAD(cbrtf_zero_pos,tc)244 ATF_TC_HEAD(cbrtf_zero_pos, tc)
245 {
246 atf_tc_set_md_var(tc, "descr", "Test cbrtf(+0.0) == +0.0");
247 }
248
ATF_TC_BODY(cbrtf_zero_pos,tc)249 ATF_TC_BODY(cbrtf_zero_pos, tc)
250 {
251 const float x = 0.0L;
252 float y = cbrtf(x);
253
254 if (fabsf(y) > 0.0 || signbit(y) != 0)
255 atf_tc_fail_nonfatal("cbrtf(+0.0) != +0.0");
256 }
257
258 /*
259 * cbrtl(3)
260 */
261 ATF_TC(cbrtl_nan);
ATF_TC_HEAD(cbrtl_nan,tc)262 ATF_TC_HEAD(cbrtl_nan, tc)
263 {
264 atf_tc_set_md_var(tc, "descr", "Test cbrtl(NaN) == NaN");
265 }
266
ATF_TC_BODY(cbrtl_nan,tc)267 ATF_TC_BODY(cbrtl_nan, tc)
268 {
269 const long double x = 0.0L / 0.0L;
270
271 ATF_CHECK(isnan(x) != 0);
272 ATF_CHECK(isnan(cbrtl(x)) != 0);
273 }
274
275 ATF_TC(cbrtl_powl);
ATF_TC_HEAD(cbrtl_powl,tc)276 ATF_TC_HEAD(cbrtl_powl, tc)
277 {
278 atf_tc_set_md_var(tc, "descr", "Test cbrtl(3) vs. powl(3)");
279 }
280
ATF_TC_BODY(cbrtl_powl,tc)281 ATF_TC_BODY(cbrtl_powl, tc)
282 {
283 const long double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
284 /* Neither cbrt nor pow is required to be correctly rounded. */
285 const long double eps = 2*LDBL_EPSILON;
286 size_t i;
287
288 for (i = 0; i < __arraycount(x); i++) {
289 long double x_cbrt = cbrtl(x[i]);
290 long double x_pow13 = powl(x[i], 1.0L / 3.0L);
291 bool ok;
292
293 if (x[i] == 0) {
294 ok = (x_cbrt == x_pow13);
295 } else {
296 ok = (fabsl((x_cbrt - x_pow13)/x_cbrt) <= eps);
297 }
298
299 if (!ok) {
300 atf_tc_fail_nonfatal("cbrtl(%.35Lg) = %.35Lg != "
301 "powl(%.35Lg, 1/3) = %.35Lg\n",
302 x[i], x_cbrt, x[i], x_pow13);
303 }
304 }
305 }
306
307 ATF_TC(cbrtl_inf_neg);
ATF_TC_HEAD(cbrtl_inf_neg,tc)308 ATF_TC_HEAD(cbrtl_inf_neg, tc)
309 {
310 atf_tc_set_md_var(tc, "descr", "Test cbrtl(-Inf) == -Inf");
311 }
312
ATF_TC_BODY(cbrtl_inf_neg,tc)313 ATF_TC_BODY(cbrtl_inf_neg, tc)
314 {
315 const long double x = -1.0L / 0.0L;
316 long double y = cbrtl(x);
317
318 ATF_CHECK(isinf(y) != 0);
319 ATF_CHECK(signbit(y) != 0);
320 }
321
322 ATF_TC(cbrtl_inf_pos);
ATF_TC_HEAD(cbrtl_inf_pos,tc)323 ATF_TC_HEAD(cbrtl_inf_pos, tc)
324 {
325 atf_tc_set_md_var(tc, "descr", "Test cbrtl(+Inf) == +Inf");
326 }
327
ATF_TC_BODY(cbrtl_inf_pos,tc)328 ATF_TC_BODY(cbrtl_inf_pos, tc)
329 {
330 const long double x = 1.0L / 0.0L;
331 long double y = cbrtl(x);
332
333 ATF_CHECK(isinf(y) != 0);
334 ATF_CHECK(signbit(y) == 0);
335 }
336
337 ATF_TC(cbrtl_zero_neg);
ATF_TC_HEAD(cbrtl_zero_neg,tc)338 ATF_TC_HEAD(cbrtl_zero_neg, tc)
339 {
340 atf_tc_set_md_var(tc, "descr", "Test cbrtl(-0.0) == -0.0");
341 }
342
ATF_TC_BODY(cbrtl_zero_neg,tc)343 ATF_TC_BODY(cbrtl_zero_neg, tc)
344 {
345 const long double x = -0.0L;
346 long double y = cbrtl(x);
347
348 if (fabsl(y) > 0.0 || signbit(y) == 0)
349 atf_tc_fail_nonfatal("cbrtl(-0.0) != -0.0");
350 }
351
352 ATF_TC(cbrtl_zero_pos);
ATF_TC_HEAD(cbrtl_zero_pos,tc)353 ATF_TC_HEAD(cbrtl_zero_pos, tc)
354 {
355 atf_tc_set_md_var(tc, "descr", "Test cbrtl(+0.0) == +0.0");
356 }
357
ATF_TC_BODY(cbrtl_zero_pos,tc)358 ATF_TC_BODY(cbrtl_zero_pos, tc)
359 {
360 const long double x = 0.0L;
361 long double y = cbrtl(x);
362
363 if (fabsl(y) > 0.0 || signbit(y) != 0)
364 atf_tc_fail_nonfatal("cbrtl(+0.0) != +0.0");
365 }
366
ATF_TP_ADD_TCS(tp)367 ATF_TP_ADD_TCS(tp)
368 {
369
370 ATF_TP_ADD_TC(tp, cbrt_nan);
371 ATF_TP_ADD_TC(tp, cbrt_pow);
372 ATF_TP_ADD_TC(tp, cbrt_inf_neg);
373 ATF_TP_ADD_TC(tp, cbrt_inf_pos);
374 ATF_TP_ADD_TC(tp, cbrt_zero_neg);
375 ATF_TP_ADD_TC(tp, cbrt_zero_pos);
376
377 ATF_TP_ADD_TC(tp, cbrtf_nan);
378 ATF_TP_ADD_TC(tp, cbrtf_powf);
379 ATF_TP_ADD_TC(tp, cbrtf_inf_neg);
380 ATF_TP_ADD_TC(tp, cbrtf_inf_pos);
381 ATF_TP_ADD_TC(tp, cbrtf_zero_neg);
382 ATF_TP_ADD_TC(tp, cbrtf_zero_pos);
383
384 ATF_TP_ADD_TC(tp, cbrtl_nan);
385 ATF_TP_ADD_TC(tp, cbrtl_powl);
386 ATF_TP_ADD_TC(tp, cbrtl_inf_neg);
387 ATF_TP_ADD_TC(tp, cbrtl_inf_pos);
388 ATF_TP_ADD_TC(tp, cbrtl_zero_neg);
389 ATF_TP_ADD_TC(tp, cbrtl_zero_pos);
390
391 return atf_no_error();
392 }
393