xref: /freebsd/contrib/netbsd-tests/lib/libm/t_exp.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 /* $NetBSD: t_exp.c,v 1.9 2018/11/07 03:59:36 riastradh 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 <float.h>
34 #include <math.h>
35 #include "t_libm.h"
36 
37 /* y = exp(x) */
38 static const struct {
39 	double x;
40 	double y;
41 } exp_values[] = {
42 	{  -10, 0.4539992976248485e-4, },
43 	{   -5, 0.6737946999085467e-2, },
44 	{   -1, 0.3678794411714423,    },
45 	{ -0.1, 0.9048374180359595,    },
46 	{    0, 1.0000000000000000,    },
47 	{  0.1, 1.1051709180756477,    },
48 	{    1, 2.7182818284590452,    },
49 	{    5, 148.41315910257660,    },
50 	{   10, 22026.465794806718,    },
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 	const double eps = DBL_EPSILON;
242 	size_t i;
243 
244 	for (i = 0; i < __arraycount(exp_values); i++) {
245 		double x = exp_values[i].x;
246 		double e_x = exp_values[i].y;
247 
248 		if (!(fabs((exp(x) - e_x)/e_x) <= eps)) {
249 			atf_tc_fail_nonfatal("exp(%.17g) = %.17g != %.17g",
250 			    x, exp(x), e_x);
251 		}
252 	}
253 }
254 
255 ATF_TC(exp_zero_neg);
256 ATF_TC_HEAD(exp_zero_neg, tc)
257 {
258 	atf_tc_set_md_var(tc, "descr", "Test exp(-0.0) == 1.0");
259 }
260 
261 ATF_TC_BODY(exp_zero_neg, tc)
262 {
263 	const double x = -0.0L;
264 
265 	if (fabs(exp(x) - 1.0) > 0.0)
266 		atf_tc_fail_nonfatal("exp(-0.0) != 1.0");
267 }
268 
269 ATF_TC(exp_zero_pos);
270 ATF_TC_HEAD(exp_zero_pos, tc)
271 {
272 	atf_tc_set_md_var(tc, "descr", "Test exp(+0.0) == 1.0");
273 }
274 
275 ATF_TC_BODY(exp_zero_pos, tc)
276 {
277 	const double x = 0.0L;
278 
279 	if (fabs(exp(x) - 1.0) > 0.0)
280 		atf_tc_fail_nonfatal("exp(+0.0) != 1.0");
281 }
282 
283 /*
284  * expf(3)
285  */
286 ATF_TC(expf_nan);
287 ATF_TC_HEAD(expf_nan, tc)
288 {
289 	atf_tc_set_md_var(tc, "descr", "Test expf(NaN) == NaN");
290 }
291 
292 ATF_TC_BODY(expf_nan, tc)
293 {
294 	const float x = 0.0L / 0.0L;
295 
296 	if (isnan(expf(x)) == 0)
297 		atf_tc_fail_nonfatal("expf(NaN) != NaN");
298 }
299 
300 ATF_TC(expf_inf_neg);
301 ATF_TC_HEAD(expf_inf_neg, tc)
302 {
303 	atf_tc_set_md_var(tc, "descr", "Test expf(-Inf) == +0.0");
304 }
305 
306 ATF_TC_BODY(expf_inf_neg, tc)
307 {
308 	const float x = -1.0L / 0.0L;
309 	float y = expf(x);
310 
311 	if (fabsf(y) > 0.0 || signbit(y) != 0)
312 		atf_tc_fail_nonfatal("expf(-Inf) != +0.0");
313 }
314 
315 ATF_TC(expf_inf_pos);
316 ATF_TC_HEAD(expf_inf_pos, tc)
317 {
318 	atf_tc_set_md_var(tc, "descr", "Test expf(+Inf) == +Inf");
319 }
320 
321 ATF_TC_BODY(expf_inf_pos, tc)
322 {
323 	const float x = 1.0L / 0.0L;
324 	float y = expf(x);
325 
326 	if (isinf(y) == 0 || signbit(y) != 0)
327 		atf_tc_fail_nonfatal("expf(+Inf) != +Inf");
328 }
329 
330 ATF_TC(expf_product);
331 ATF_TC_HEAD(expf_product, tc)
332 {
333 	atf_tc_set_md_var(tc, "descr", "Test some selected expf(x)");
334 }
335 
336 ATF_TC_BODY(expf_product, tc)
337 {
338 	const float eps = FLT_EPSILON;
339 	size_t i;
340 
341 	for (i = 0; i < __arraycount(exp_values); i++) {
342 		float x = exp_values[i].x;
343 		float e_x = exp_values[i].y;
344 
345 		if (!(fabsf((expf(x) - e_x)/e_x) <= eps)) {
346 			atf_tc_fail_nonfatal("expf(%.8g) = %.8g != %.8g",
347 			    x, exp(x), e_x);
348 		}
349 	}
350 }
351 
352 ATF_TC(expf_zero_neg);
353 ATF_TC_HEAD(expf_zero_neg, tc)
354 {
355 	atf_tc_set_md_var(tc, "descr", "Test expf(-0.0) == 1.0");
356 }
357 
358 ATF_TC_BODY(expf_zero_neg, tc)
359 {
360 	const float x = -0.0L;
361 
362 	if (fabsf(expf(x) - 1.0f) > 0.0)
363 		atf_tc_fail_nonfatal("expf(-0.0) != 1.0");
364 }
365 
366 ATF_TC(expf_zero_pos);
367 ATF_TC_HEAD(expf_zero_pos, tc)
368 {
369 	atf_tc_set_md_var(tc, "descr", "Test expf(+0.0) == 1.0");
370 }
371 
372 ATF_TC_BODY(expf_zero_pos, tc)
373 {
374 	const float x = 0.0L;
375 
376 	if (fabsf(expf(x) - 1.0f) > 0.0)
377 		atf_tc_fail_nonfatal("expf(+0.0) != 1.0");
378 }
379 
380 /*
381  * expm1(3)
382  */
383 ATF_TC(expm1_nan);
384 ATF_TC_HEAD(expm1_nan, tc)
385 {
386 	atf_tc_set_md_var(tc, "descr", "Test expm1(NaN) == NaN");
387 }
388 
389 ATF_TC_BODY(expm1_nan, tc)
390 {
391 	const double x = 0.0L / 0.0L;
392 
393 	if (isnan(expm1(x)) == 0)
394 		atf_tc_fail_nonfatal("expm1(NaN) != NaN");
395 }
396 
397 ATF_TC(expm1_inf_neg);
398 ATF_TC_HEAD(expm1_inf_neg, tc)
399 {
400 	atf_tc_set_md_var(tc, "descr", "Test expm1(-Inf) == -1");
401 }
402 
403 ATF_TC_BODY(expm1_inf_neg, tc)
404 {
405 	const double x = -1.0L / 0.0L;
406 
407 	if (expm1(x) != -1.0)
408 		atf_tc_fail_nonfatal("expm1(-Inf) != -1.0");
409 }
410 
411 ATF_TC(expm1_inf_pos);
412 ATF_TC_HEAD(expm1_inf_pos, tc)
413 {
414 	atf_tc_set_md_var(tc, "descr", "Test expm1(+Inf) == +Inf");
415 }
416 
417 ATF_TC_BODY(expm1_inf_pos, tc)
418 {
419 	const double x = 1.0L / 0.0L;
420 	double y = expm1(x);
421 
422 	if (isinf(y) == 0 || signbit(y) != 0)
423 		atf_tc_fail_nonfatal("expm1(+Inf) != +Inf");
424 }
425 
426 ATF_TC(expm1_zero_neg);
427 ATF_TC_HEAD(expm1_zero_neg, tc)
428 {
429 	atf_tc_set_md_var(tc, "descr", "Test expm1(-0.0) == -0.0");
430 }
431 
432 ATF_TC_BODY(expm1_zero_neg, tc)
433 {
434 	const double x = -0.0L;
435 	double y = expm1(x);
436 
437 	if (fabs(y) > 0.0 || signbit(y) == 0)
438 		atf_tc_fail_nonfatal("expm1(-0.0) != -0.0");
439 }
440 
441 ATF_TC(expm1_zero_pos);
442 ATF_TC_HEAD(expm1_zero_pos, tc)
443 {
444 	atf_tc_set_md_var(tc, "descr", "Test expm1(+0.0) == 1.0");
445 }
446 
447 ATF_TC_BODY(expm1_zero_pos, tc)
448 {
449 	const double x = 0.0L;
450 	double y = expm1(x);
451 
452 	if (fabs(y) > 0.0 || signbit(y) != 0)
453 		atf_tc_fail_nonfatal("expm1(+0.0) != +0.0");
454 }
455 
456 /*
457  * expm1f(3)
458  */
459 ATF_TC(expm1f_nan);
460 ATF_TC_HEAD(expm1f_nan, tc)
461 {
462 	atf_tc_set_md_var(tc, "descr", "Test expm1f(NaN) == NaN");
463 }
464 
465 ATF_TC_BODY(expm1f_nan, tc)
466 {
467 	const float x = 0.0L / 0.0L;
468 
469 	if (isnan(expm1f(x)) == 0)
470 		atf_tc_fail_nonfatal("expm1f(NaN) != NaN");
471 }
472 
473 ATF_TC(expm1f_inf_neg);
474 ATF_TC_HEAD(expm1f_inf_neg, tc)
475 {
476 	atf_tc_set_md_var(tc, "descr", "Test expm1f(-Inf) == -1");
477 }
478 
479 ATF_TC_BODY(expm1f_inf_neg, tc)
480 {
481 	const float x = -1.0L / 0.0L;
482 
483 	if (expm1f(x) != -1.0)
484 		atf_tc_fail_nonfatal("expm1f(-Inf) != -1.0");
485 }
486 
487 ATF_TC(expm1f_inf_pos);
488 ATF_TC_HEAD(expm1f_inf_pos, tc)
489 {
490 	atf_tc_set_md_var(tc, "descr", "Test expm1f(+Inf) == +Inf");
491 }
492 
493 ATF_TC_BODY(expm1f_inf_pos, tc)
494 {
495 	const float x = 1.0L / 0.0L;
496 	float y = expm1f(x);
497 
498 	if (isinf(y) == 0 || signbit(y) != 0)
499 		atf_tc_fail_nonfatal("expm1f(+Inf) != +Inf");
500 }
501 
502 ATF_TC(expm1f_zero_neg);
503 ATF_TC_HEAD(expm1f_zero_neg, tc)
504 {
505 	atf_tc_set_md_var(tc, "descr", "Test expm1f(-0.0) == -0.0");
506 }
507 
508 ATF_TC_BODY(expm1f_zero_neg, tc)
509 {
510 	const float x = -0.0L;
511 	float y = expm1f(x);
512 
513 	if (fabsf(y) > 0.0 || signbit(y) == 0)
514 		atf_tc_fail_nonfatal("expm1f(-0.0) != -0.0");
515 }
516 
517 ATF_TC(expm1f_zero_pos);
518 ATF_TC_HEAD(expm1f_zero_pos, tc)
519 {
520 	atf_tc_set_md_var(tc, "descr", "Test expm1f(+0.0) == 1.0");
521 }
522 
523 ATF_TC_BODY(expm1f_zero_pos, tc)
524 {
525 	const float x = 0.0L;
526 	float y = expm1f(x);
527 
528 	if (fabsf(y) > 0.0 || signbit(y) != 0)
529 		atf_tc_fail_nonfatal("expm1f(+0.0) != +0.0");
530 }
531 
532 ATF_TP_ADD_TCS(tp)
533 {
534 
535 	ATF_TP_ADD_TC(tp, exp2_is_nan);
536 	ATF_TP_ADD_TC(tp, exp2_is_plus_zero);
537 	ATF_TP_ADD_TC(tp, exp2_values);
538 	ATF_TP_ADD_TC(tp, exp2_powers);
539 
540 	ATF_TP_ADD_TC(tp, exp_nan);
541 	ATF_TP_ADD_TC(tp, exp_inf_neg);
542 	ATF_TP_ADD_TC(tp, exp_inf_pos);
543 	ATF_TP_ADD_TC(tp, exp_product);
544 	ATF_TP_ADD_TC(tp, exp_zero_neg);
545 	ATF_TP_ADD_TC(tp, exp_zero_pos);
546 
547 	ATF_TP_ADD_TC(tp, expf_nan);
548 	ATF_TP_ADD_TC(tp, expf_inf_neg);
549 	ATF_TP_ADD_TC(tp, expf_inf_pos);
550 	ATF_TP_ADD_TC(tp, expf_product);
551 	ATF_TP_ADD_TC(tp, expf_zero_neg);
552 	ATF_TP_ADD_TC(tp, expf_zero_pos);
553 
554 	ATF_TP_ADD_TC(tp, expm1_nan);
555 	ATF_TP_ADD_TC(tp, expm1_inf_neg);
556 	ATF_TP_ADD_TC(tp, expm1_inf_pos);
557 	ATF_TP_ADD_TC(tp, expm1_zero_neg);
558 	ATF_TP_ADD_TC(tp, expm1_zero_pos);
559 
560 	ATF_TP_ADD_TC(tp, expm1f_nan);
561 	ATF_TP_ADD_TC(tp, expm1f_inf_neg);
562 	ATF_TP_ADD_TC(tp, expm1f_inf_pos);
563 	ATF_TP_ADD_TC(tp, expm1f_zero_neg);
564 	ATF_TP_ADD_TC(tp, expm1f_zero_pos);
565 
566 	return atf_no_error();
567 }
568