xref: /freebsd/contrib/netbsd-tests/lib/libm/t_exp.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /* $NetBSD: t_exp.c,v 1.8 2014/10/07 16:53:44 gson 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 #include "t_libm.h"
35 
36 /* y = exp(x) */
37 static const struct {
38 	double x;
39 	double y;
40 	double e;
41 } exp_values[] = {
42 	{  -10, 0.4539992976248485e-4, 1e-4, },
43 	{   -5, 0.6737946999085467e-2, 1e-2, },
44 	{   -1, 0.3678794411714423,    1e-1, },
45 	{ -0.1, 0.9048374180359595,    1e-1, },
46 	{    0, 1.0000000000000000,    1,    },
47 	{  0.1, 1.1051709180756477,    1,    },
48 	{    1, 2.7182818284590452,    1,    },
49 	{    5, 148.41315910257660,    1e2, },
50 	{   10, 22026.465794806718,    1e4, },
51 };
52 
53 /*
54  * exp2/exp2f(3)
55  */
56 ATF_LIBM_TEST(exp2_is_nan, "Test exp2(x) == NaN")
57 {
58 #ifdef T_LIBM_NAN
59 	T_LIBM_CHECK_NAN(0, exp2, T_LIBM_NAN);
60 	T_LIBM_CHECK_NAN(0, exp2f, T_LIBM_NAN);
61 #else
62 	atf_tc_skip("no NaN on this machine");
63 #endif
64 }
65 
66 ATF_LIBM_TEST(exp2_is_plus_zero, "Test exp2(x) == +0.0")
67 {
68 #ifdef T_LIBM_MINUS_INF
69 	T_LIBM_CHECK_PLUS_ZERO(0, exp2, T_LIBM_MINUS_INF);
70 	T_LIBM_CHECK_PLUS_ZERO(0, exp2f, T_LIBM_MINUS_INF);
71 #else
72 	atf_tc_skip("no +/-Inf on this machine");
73 #endif
74 }
75 
76 ATF_LIBM_TEST(exp2_powers, "Test exp2(x) is correct for some integer x")
77 {
78 	static const struct {
79 		double	x;
80 		double	d_y;
81 		double	f_y;
82 	} v[] = {
83 	    { +0.0,	1.0,	1.0 },
84 	    { -0.0,	1.0,	1.0 },
85 	    {    1,	0x1p1,	0x1p1 },
86 	    {    2,	0x1p2,	0x1p2 },
87 	    {  100,	0x1p100,	0x1p100 },
88 	    {  125,	0x1p125,	0x1p125 },
89 	    {  126,	0x1p126,	0x1p126 },
90 #if __DBL_MAX_EXP__ > 129
91 	    {  127,	0x1p127,	0x1p127 },
92 #endif
93 #ifdef T_LIBM_PLUS_INF
94 	    {  128,	0x1p128,	T_LIBM_PLUS_INF },
95 	    {  129,	0x1p129,	T_LIBM_PLUS_INF },
96 	    { 1000,	0x1p1000,	T_LIBM_PLUS_INF },
97 	    { 1020,	0x1p1020,	T_LIBM_PLUS_INF },
98 	    { 1023,	0x1p1023,	T_LIBM_PLUS_INF },
99 	    { 1024,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
100 	    { 1030,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
101 	    { 1050,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
102 	    { 2000,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
103 	    { 16383,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
104 	    { 16384,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
105 	    { 16385,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF },
106 #endif
107 	    {   -1,	0x1p-1,	0x1p-1 },
108 	    {   -2,	0x1p-2,	0x1p-2 },
109 	    { -100,	0x1p-100,	0x1p-100 },
110 	    { -127,	0x1p-127,	0x1p-127 },
111 	    { -128,	0x1p-128,	0x1p-128 },
112 #if __LDBL_MIN_EXP__ < -129
113 	    { -300,	0x1p-300,	0.0},
114 	    { -400,	0x1p-400,	0.0},
115 	    {-1000,	0x1p-1000,	0.0},
116 	    {-1022,	0x1p-1022,	0.0},
117 	    /* These should be denormal numbers */
118 	    {-1023,	0x1p-1023,	0.0},
119 	    {-1024,	0x1p-1024,	0.0},
120 	    {-1040,	0x1p-1040,	0.0},
121 	    {-1060,	0x1p-1060,	0.0},
122 	    /* This is the smallest result gcc will allow */
123 	    {-1074,	0x1p-1074,	0.0},
124 #endif
125 	    {-1075,	0x0,	0.0},
126 	    {-1080,	0x0,	0.0},
127 	    {-2000,	0x0,	0.0},
128 	    {-16382,	0x0,	0.0},
129 	    {-16383,	0x0,	0.0},
130 	    {-16384,	0x0,	0.0},
131 	};
132 	unsigned int i;
133 
134 #if defined(__FreeBSD__) && defined(__i386__)
135 	atf_tc_expect_fail("a number of the assertions fail on i386");
136 #endif
137 
138 	for (i = 0; i < __arraycount(v); i++) {
139 		T_LIBM_CHECK(i, exp2, v[i].x, v[i].d_y, 0.0);
140 		T_LIBM_CHECK(i, exp2f, v[i].x, v[i].f_y, 0.0);
141 	}
142 }
143 
144 ATF_LIBM_TEST(exp2_values, "Test exp2(x) is correct for some x")
145 {
146 	static const struct {
147 		double	x;
148 		double	d_y;
149 		float   f_y;
150 		double	d_eps;
151 		double	f_eps;
152 	} v[] = {
153 #if __DBL_MAX_EXP__ > 128
154 	    /* The largest double constant */
155 	    { 0x1.fffffffffffffp9,	0x1.ffffffffffd3ap1023,	0.00,
156 		0x1p969,	0.0 },
157 	    /* The largest float constant */
158 	    { 0x1.fffffep6,	0x1.ffff4ep+127,	0x1.ffff4ep+127,	6e30,	0.0 },
159 #endif
160 #ifdef T_LIBM_PLUS_INF
161 	    { T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF,	T_LIBM_PLUS_INF,	0.0,	0.0 },
162 #endif
163 
164 	    /* The few values from the old tests */
165 	    /* Results from i386/amd64, d_eps needed on i386 */
166 	    /* f_y values calculated using py-mpmath */
167 	    {  1.1,	0x1.125fbee250664p+1,	0x1.125fc0p+1,	0x1p-52,	0x1.8p-22 },
168 	    {  2.2,	0x1.2611186bae675p+2,	0x1.26111ap+2,	0x1p-51,	0x1.8p-21 },
169 	    {  3.3,	0x1.3b2c47bff8328p+3,	0x1.3b2c48p+3,	0x1p-50,	0x1.8p-20 },
170 	    {  4.4,	0x1.51cb453b9536ep+4,	0x1.51cb46p+4,	0x1p-49,	0x1.8p-19 },
171 	    {  5.5,	0x1.6a09e667f3bcdp+5,	0x1.6a09e6p+5,	0x1p-48,	0x1.8p-18 },
172 	    {  6.6,	0x1.8406003b2ae5bp+6,	0x1.8405fep+6,	0x1p-47,	0x1.8p-17 },
173 	    {  7.7,	0x1.9fdf8bcce533ep+7,	0x1.9fdf88p+7,	0x1p-46,	0x1.8p-16 },
174 	    {  8.8,	0x1.bdb8cdadbe124p+8,	0x1.bdb8d2p+8,	0x1p-45,	0x1.8p-15 },
175 	};
176 	unsigned int i;
177 
178 	for (i = 0; i < __arraycount(v); i++) {
179 		T_LIBM_CHECK(i, exp2, v[i].x, v[i].d_y, v[i].d_eps);
180 		if (i > 1)
181 			T_LIBM_CHECK(i, exp2f, v[i].x, v[i].f_y, v[i].f_eps);
182 	}
183 }
184 
185 
186 /*
187  * exp(3)
188  */
189 ATF_TC(exp_nan);
190 ATF_TC_HEAD(exp_nan, tc)
191 {
192 	atf_tc_set_md_var(tc, "descr", "Test exp(NaN) == NaN");
193 }
194 
195 ATF_TC_BODY(exp_nan, tc)
196 {
197 	const double x = 0.0L / 0.0L;
198 
199 	if (isnan(exp(x)) == 0)
200 		atf_tc_fail_nonfatal("exp(NaN) != NaN");
201 }
202 
203 ATF_TC(exp_inf_neg);
204 ATF_TC_HEAD(exp_inf_neg, tc)
205 {
206 	atf_tc_set_md_var(tc, "descr", "Test exp(-Inf) == +0.0");
207 }
208 
209 ATF_TC_BODY(exp_inf_neg, tc)
210 {
211 	const double x = -1.0L / 0.0L;
212 	double y = exp(x);
213 
214 	if (fabs(y) > 0.0 || signbit(y) != 0)
215 		atf_tc_fail_nonfatal("exp(-Inf) != +0.0");
216 }
217 
218 ATF_TC(exp_inf_pos);
219 ATF_TC_HEAD(exp_inf_pos, tc)
220 {
221 	atf_tc_set_md_var(tc, "descr", "Test exp(+Inf) == +Inf");
222 }
223 
224 ATF_TC_BODY(exp_inf_pos, tc)
225 {
226 	const double x = 1.0L / 0.0L;
227 	double y = exp(x);
228 
229 	if (isinf(y) == 0 || signbit(y) != 0)
230 		atf_tc_fail_nonfatal("exp(+Inf) != +Inf");
231 }
232 
233 ATF_TC(exp_product);
234 ATF_TC_HEAD(exp_product, tc)
235 {
236 	atf_tc_set_md_var(tc, "descr", "Test some selected exp(x)");
237 }
238 
239 ATF_TC_BODY(exp_product, tc)
240 {
241 	double eps;
242 	double x;
243 	double y;
244 	size_t i;
245 
246 	for (i = 0; i < __arraycount(exp_values); i++) {
247 		x = exp_values[i].x;
248 		y = exp_values[i].y;
249 		eps = 1e-15 * exp_values[i].e;
250 
251 		if (fabs(exp(x) - y) > eps)
252 			atf_tc_fail_nonfatal("exp(%0.01f) != %18.18e", x, y);
253 	}
254 }
255 
256 ATF_TC(exp_zero_neg);
257 ATF_TC_HEAD(exp_zero_neg, tc)
258 {
259 	atf_tc_set_md_var(tc, "descr", "Test exp(-0.0) == 1.0");
260 }
261 
262 ATF_TC_BODY(exp_zero_neg, tc)
263 {
264 	const double x = -0.0L;
265 
266 	if (fabs(exp(x) - 1.0) > 0.0)
267 		atf_tc_fail_nonfatal("exp(-0.0) != 1.0");
268 }
269 
270 ATF_TC(exp_zero_pos);
271 ATF_TC_HEAD(exp_zero_pos, tc)
272 {
273 	atf_tc_set_md_var(tc, "descr", "Test exp(+0.0) == 1.0");
274 }
275 
276 ATF_TC_BODY(exp_zero_pos, tc)
277 {
278 	const double x = 0.0L;
279 
280 	if (fabs(exp(x) - 1.0) > 0.0)
281 		atf_tc_fail_nonfatal("exp(+0.0) != 1.0");
282 }
283 
284 /*
285  * expf(3)
286  */
287 ATF_TC(expf_nan);
288 ATF_TC_HEAD(expf_nan, tc)
289 {
290 	atf_tc_set_md_var(tc, "descr", "Test expf(NaN) == NaN");
291 }
292 
293 ATF_TC_BODY(expf_nan, tc)
294 {
295 	const float x = 0.0L / 0.0L;
296 
297 	if (isnan(expf(x)) == 0)
298 		atf_tc_fail_nonfatal("expf(NaN) != NaN");
299 }
300 
301 ATF_TC(expf_inf_neg);
302 ATF_TC_HEAD(expf_inf_neg, tc)
303 {
304 	atf_tc_set_md_var(tc, "descr", "Test expf(-Inf) == +0.0");
305 }
306 
307 ATF_TC_BODY(expf_inf_neg, tc)
308 {
309 	const float x = -1.0L / 0.0L;
310 	float y = expf(x);
311 
312 	if (fabsf(y) > 0.0 || signbit(y) != 0)
313 		atf_tc_fail_nonfatal("expf(-Inf) != +0.0");
314 }
315 
316 ATF_TC(expf_inf_pos);
317 ATF_TC_HEAD(expf_inf_pos, tc)
318 {
319 	atf_tc_set_md_var(tc, "descr", "Test expf(+Inf) == +Inf");
320 }
321 
322 ATF_TC_BODY(expf_inf_pos, tc)
323 {
324 	const float x = 1.0L / 0.0L;
325 	float y = expf(x);
326 
327 	if (isinf(y) == 0 || signbit(y) != 0)
328 		atf_tc_fail_nonfatal("expf(+Inf) != +Inf");
329 }
330 
331 ATF_TC(expf_product);
332 ATF_TC_HEAD(expf_product, tc)
333 {
334 	atf_tc_set_md_var(tc, "descr", "Test some selected expf(x)");
335 }
336 
337 ATF_TC_BODY(expf_product, tc)
338 {
339 	float eps;
340 	float x;
341 	float y;
342 	size_t i;
343 
344 	for (i = 0; i < __arraycount(exp_values); i++) {
345 		x = exp_values[i].x;
346 		y = exp_values[i].y;
347 		eps = 1e-6 * exp_values[i].e;
348 
349 		if (fabsf(expf(x) - y) > eps)
350 			atf_tc_fail_nonfatal("expf(%0.01f) != %18.18e", x, y);
351 	}
352 }
353 
354 ATF_TC(expf_zero_neg);
355 ATF_TC_HEAD(expf_zero_neg, tc)
356 {
357 	atf_tc_set_md_var(tc, "descr", "Test expf(-0.0) == 1.0");
358 }
359 
360 ATF_TC_BODY(expf_zero_neg, tc)
361 {
362 	const float x = -0.0L;
363 
364 	if (fabsf(expf(x) - 1.0f) > 0.0)
365 		atf_tc_fail_nonfatal("expf(-0.0) != 1.0");
366 }
367 
368 ATF_TC(expf_zero_pos);
369 ATF_TC_HEAD(expf_zero_pos, tc)
370 {
371 	atf_tc_set_md_var(tc, "descr", "Test expf(+0.0) == 1.0");
372 }
373 
374 ATF_TC_BODY(expf_zero_pos, tc)
375 {
376 	const float x = 0.0L;
377 
378 	if (fabsf(expf(x) - 1.0f) > 0.0)
379 		atf_tc_fail_nonfatal("expf(+0.0) != 1.0");
380 }
381 
382 /*
383  * expm1(3)
384  */
385 ATF_TC(expm1_nan);
386 ATF_TC_HEAD(expm1_nan, tc)
387 {
388 	atf_tc_set_md_var(tc, "descr", "Test expm1(NaN) == NaN");
389 }
390 
391 ATF_TC_BODY(expm1_nan, tc)
392 {
393 	const double x = 0.0L / 0.0L;
394 
395 	if (isnan(expm1(x)) == 0)
396 		atf_tc_fail_nonfatal("expm1(NaN) != NaN");
397 }
398 
399 ATF_TC(expm1_inf_neg);
400 ATF_TC_HEAD(expm1_inf_neg, tc)
401 {
402 	atf_tc_set_md_var(tc, "descr", "Test expm1(-Inf) == -1");
403 }
404 
405 ATF_TC_BODY(expm1_inf_neg, tc)
406 {
407 	const double x = -1.0L / 0.0L;
408 
409 	if (expm1(x) != -1.0)
410 		atf_tc_fail_nonfatal("expm1(-Inf) != -1.0");
411 }
412 
413 ATF_TC(expm1_inf_pos);
414 ATF_TC_HEAD(expm1_inf_pos, tc)
415 {
416 	atf_tc_set_md_var(tc, "descr", "Test expm1(+Inf) == +Inf");
417 }
418 
419 ATF_TC_BODY(expm1_inf_pos, tc)
420 {
421 	const double x = 1.0L / 0.0L;
422 	double y = expm1(x);
423 
424 	if (isinf(y) == 0 || signbit(y) != 0)
425 		atf_tc_fail_nonfatal("expm1(+Inf) != +Inf");
426 }
427 
428 ATF_TC(expm1_zero_neg);
429 ATF_TC_HEAD(expm1_zero_neg, tc)
430 {
431 	atf_tc_set_md_var(tc, "descr", "Test expm1(-0.0) == -0.0");
432 }
433 
434 ATF_TC_BODY(expm1_zero_neg, tc)
435 {
436 	const double x = -0.0L;
437 	double y = expm1(x);
438 
439 	if (fabs(y) > 0.0 || signbit(y) == 0)
440 		atf_tc_fail_nonfatal("expm1(-0.0) != -0.0");
441 }
442 
443 ATF_TC(expm1_zero_pos);
444 ATF_TC_HEAD(expm1_zero_pos, tc)
445 {
446 	atf_tc_set_md_var(tc, "descr", "Test expm1(+0.0) == 1.0");
447 }
448 
449 ATF_TC_BODY(expm1_zero_pos, tc)
450 {
451 	const double x = 0.0L;
452 	double y = expm1(x);
453 
454 	if (fabs(y) > 0.0 || signbit(y) != 0)
455 		atf_tc_fail_nonfatal("expm1(+0.0) != +0.0");
456 }
457 
458 /*
459  * expm1f(3)
460  */
461 ATF_TC(expm1f_nan);
462 ATF_TC_HEAD(expm1f_nan, tc)
463 {
464 	atf_tc_set_md_var(tc, "descr", "Test expm1f(NaN) == NaN");
465 }
466 
467 ATF_TC_BODY(expm1f_nan, tc)
468 {
469 	const float x = 0.0L / 0.0L;
470 
471 	if (isnan(expm1f(x)) == 0)
472 		atf_tc_fail_nonfatal("expm1f(NaN) != NaN");
473 }
474 
475 ATF_TC(expm1f_inf_neg);
476 ATF_TC_HEAD(expm1f_inf_neg, tc)
477 {
478 	atf_tc_set_md_var(tc, "descr", "Test expm1f(-Inf) == -1");
479 }
480 
481 ATF_TC_BODY(expm1f_inf_neg, tc)
482 {
483 	const float x = -1.0L / 0.0L;
484 
485 	if (expm1f(x) != -1.0)
486 		atf_tc_fail_nonfatal("expm1f(-Inf) != -1.0");
487 }
488 
489 ATF_TC(expm1f_inf_pos);
490 ATF_TC_HEAD(expm1f_inf_pos, tc)
491 {
492 	atf_tc_set_md_var(tc, "descr", "Test expm1f(+Inf) == +Inf");
493 }
494 
495 ATF_TC_BODY(expm1f_inf_pos, tc)
496 {
497 	const float x = 1.0L / 0.0L;
498 	float y = expm1f(x);
499 
500 	if (isinf(y) == 0 || signbit(y) != 0)
501 		atf_tc_fail_nonfatal("expm1f(+Inf) != +Inf");
502 }
503 
504 ATF_TC(expm1f_zero_neg);
505 ATF_TC_HEAD(expm1f_zero_neg, tc)
506 {
507 	atf_tc_set_md_var(tc, "descr", "Test expm1f(-0.0) == -0.0");
508 }
509 
510 ATF_TC_BODY(expm1f_zero_neg, tc)
511 {
512 	const float x = -0.0L;
513 	float y = expm1f(x);
514 
515 	if (fabsf(y) > 0.0 || signbit(y) == 0)
516 		atf_tc_fail_nonfatal("expm1f(-0.0) != -0.0");
517 }
518 
519 ATF_TC(expm1f_zero_pos);
520 ATF_TC_HEAD(expm1f_zero_pos, tc)
521 {
522 	atf_tc_set_md_var(tc, "descr", "Test expm1f(+0.0) == 1.0");
523 }
524 
525 ATF_TC_BODY(expm1f_zero_pos, tc)
526 {
527 	const float x = 0.0L;
528 	float y = expm1f(x);
529 
530 	if (fabsf(y) > 0.0 || signbit(y) != 0)
531 		atf_tc_fail_nonfatal("expm1f(+0.0) != +0.0");
532 }
533 
534 ATF_TP_ADD_TCS(tp)
535 {
536 
537 	ATF_TP_ADD_TC(tp, exp2_is_nan);
538 	ATF_TP_ADD_TC(tp, exp2_is_plus_zero);
539 	ATF_TP_ADD_TC(tp, exp2_values);
540 	ATF_TP_ADD_TC(tp, exp2_powers);
541 
542 	ATF_TP_ADD_TC(tp, exp_nan);
543 	ATF_TP_ADD_TC(tp, exp_inf_neg);
544 	ATF_TP_ADD_TC(tp, exp_inf_pos);
545 	ATF_TP_ADD_TC(tp, exp_product);
546 	ATF_TP_ADD_TC(tp, exp_zero_neg);
547 	ATF_TP_ADD_TC(tp, exp_zero_pos);
548 
549 	ATF_TP_ADD_TC(tp, expf_nan);
550 	ATF_TP_ADD_TC(tp, expf_inf_neg);
551 	ATF_TP_ADD_TC(tp, expf_inf_pos);
552 	ATF_TP_ADD_TC(tp, expf_product);
553 	ATF_TP_ADD_TC(tp, expf_zero_neg);
554 	ATF_TP_ADD_TC(tp, expf_zero_pos);
555 
556 	ATF_TP_ADD_TC(tp, expm1_nan);
557 	ATF_TP_ADD_TC(tp, expm1_inf_neg);
558 	ATF_TP_ADD_TC(tp, expm1_inf_pos);
559 	ATF_TP_ADD_TC(tp, expm1_zero_neg);
560 	ATF_TP_ADD_TC(tp, expm1_zero_pos);
561 
562 	ATF_TP_ADD_TC(tp, expm1f_nan);
563 	ATF_TP_ADD_TC(tp, expm1f_inf_neg);
564 	ATF_TP_ADD_TC(tp, expm1f_inf_pos);
565 	ATF_TP_ADD_TC(tp, expm1f_zero_neg);
566 	ATF_TP_ADD_TC(tp, expm1f_zero_pos);
567 
568 	return atf_no_error();
569 }
570