etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libm / t_exp.c
blob73ae87b0150984e995f5bcb460a7428599b37fa5
1 /* $NetBSD: t_exp.c,v 1.8 2014/10/07 16:53:44 gson Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
32 #include <atf-c.h>
33 #include <math.h>
34 #include "t_libm.h"
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, },
54 * exp2/exp2f(3)
56 ATF_LIBM_TEST(exp2_is_nan, "Test exp2(x) == NaN")
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
66 ATF_LIBM_TEST(exp2_is_plus_zero, "Test exp2(x) == +0.0")
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
76 ATF_LIBM_TEST(exp2_powers, "Test exp2(x) is correct for some integer x")
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},
132 unsigned int i;
134 for (i = 0; i < __arraycount(v); i++) {
135 T_LIBM_CHECK(i, exp2, v[i].x, v[i].d_y, 0.0);
136 T_LIBM_CHECK(i, exp2f, v[i].x, v[i].f_y, 0.0);
140 ATF_LIBM_TEST(exp2_values, "Test exp2(x) is correct for some x")
142 static const struct {
143 double x;
144 double d_y;
145 float f_y;
146 double d_eps;
147 double f_eps;
148 } v[] = {
149 #if __DBL_MAX_EXP__ > 128
150 /* The largest double constant */
151 { 0x1.fffffffffffffp9, 0x1.ffffffffffd3ap1023, 0.00,
152 0x1p969, 0.0 },
153 /* The largest float constant */
154 { 0x1.fffffep6, 0x1.ffff4ep+127, 0x1.ffff4ep+127, 6e30, 0.0 },
155 #endif
156 #ifdef T_LIBM_PLUS_INF
157 { T_LIBM_PLUS_INF, T_LIBM_PLUS_INF, T_LIBM_PLUS_INF, 0.0, 0.0 },
158 #endif
160 /* The few values from the old tests */
161 /* Results from i386/amd64, d_eps needed on i386 */
162 /* f_y values calculated using py-mpmath */
163 { 1.1, 0x1.125fbee250664p+1, 0x1.125fc0p+1, 0x1p-52, 0x1.8p-22 },
164 { 2.2, 0x1.2611186bae675p+2, 0x1.26111ap+2, 0x1p-51, 0x1.8p-21 },
165 { 3.3, 0x1.3b2c47bff8328p+3, 0x1.3b2c48p+3, 0x1p-50, 0x1.8p-20 },
166 { 4.4, 0x1.51cb453b9536ep+4, 0x1.51cb46p+4, 0x1p-49, 0x1.8p-19 },
167 { 5.5, 0x1.6a09e667f3bcdp+5, 0x1.6a09e6p+5, 0x1p-48, 0x1.8p-18 },
168 { 6.6, 0x1.8406003b2ae5bp+6, 0x1.8405fep+6, 0x1p-47, 0x1.8p-17 },
169 { 7.7, 0x1.9fdf8bcce533ep+7, 0x1.9fdf88p+7, 0x1p-46, 0x1.8p-16 },
170 { 8.8, 0x1.bdb8cdadbe124p+8, 0x1.bdb8d2p+8, 0x1p-45, 0x1.8p-15 },
172 unsigned int i;
174 for (i = 0; i < __arraycount(v); i++) {
175 T_LIBM_CHECK(i, exp2, v[i].x, v[i].d_y, v[i].d_eps);
176 if (i > 1)
177 T_LIBM_CHECK(i, exp2f, v[i].x, v[i].f_y, v[i].f_eps);
183 * exp(3)
185 ATF_TC(exp_nan);
186 ATF_TC_HEAD(exp_nan, tc)
188 atf_tc_set_md_var(tc, "descr", "Test exp(NaN) == NaN");
191 ATF_TC_BODY(exp_nan, tc)
193 const double x = 0.0L / 0.0L;
195 if (isnan(exp(x)) == 0)
196 atf_tc_fail_nonfatal("exp(NaN) != NaN");
199 ATF_TC(exp_inf_neg);
200 ATF_TC_HEAD(exp_inf_neg, tc)
202 atf_tc_set_md_var(tc, "descr", "Test exp(-Inf) == +0.0");
205 ATF_TC_BODY(exp_inf_neg, tc)
207 const double x = -1.0L / 0.0L;
208 double y = exp(x);
210 if (fabs(y) > 0.0 || signbit(y) != 0)
211 atf_tc_fail_nonfatal("exp(-Inf) != +0.0");
214 ATF_TC(exp_inf_pos);
215 ATF_TC_HEAD(exp_inf_pos, tc)
217 atf_tc_set_md_var(tc, "descr", "Test exp(+Inf) == +Inf");
220 ATF_TC_BODY(exp_inf_pos, tc)
222 const double x = 1.0L / 0.0L;
223 double y = exp(x);
225 if (isinf(y) == 0 || signbit(y) != 0)
226 atf_tc_fail_nonfatal("exp(+Inf) != +Inf");
229 ATF_TC(exp_product);
230 ATF_TC_HEAD(exp_product, tc)
232 atf_tc_set_md_var(tc, "descr", "Test some selected exp(x)");
235 ATF_TC_BODY(exp_product, tc)
237 double eps;
238 double x;
239 double y;
240 size_t i;
242 for (i = 0; i < __arraycount(exp_values); i++) {
243 x = exp_values[i].x;
244 y = exp_values[i].y;
245 eps = 1e-15 * exp_values[i].e;
247 if (fabs(exp(x) - y) > eps)
248 atf_tc_fail_nonfatal("exp(%0.01f) != %18.18e", x, y);
252 ATF_TC(exp_zero_neg);
253 ATF_TC_HEAD(exp_zero_neg, tc)
255 atf_tc_set_md_var(tc, "descr", "Test exp(-0.0) == 1.0");
258 ATF_TC_BODY(exp_zero_neg, tc)
260 const double x = -0.0L;
262 if (fabs(exp(x) - 1.0) > 0.0)
263 atf_tc_fail_nonfatal("exp(-0.0) != 1.0");
266 ATF_TC(exp_zero_pos);
267 ATF_TC_HEAD(exp_zero_pos, tc)
269 atf_tc_set_md_var(tc, "descr", "Test exp(+0.0) == 1.0");
272 ATF_TC_BODY(exp_zero_pos, tc)
274 const double x = 0.0L;
276 if (fabs(exp(x) - 1.0) > 0.0)
277 atf_tc_fail_nonfatal("exp(+0.0) != 1.0");
281 * expf(3)
283 ATF_TC(expf_nan);
284 ATF_TC_HEAD(expf_nan, tc)
286 atf_tc_set_md_var(tc, "descr", "Test expf(NaN) == NaN");
289 ATF_TC_BODY(expf_nan, tc)
291 const float x = 0.0L / 0.0L;
293 if (isnan(expf(x)) == 0)
294 atf_tc_fail_nonfatal("expf(NaN) != NaN");
297 ATF_TC(expf_inf_neg);
298 ATF_TC_HEAD(expf_inf_neg, tc)
300 atf_tc_set_md_var(tc, "descr", "Test expf(-Inf) == +0.0");
303 ATF_TC_BODY(expf_inf_neg, tc)
305 const float x = -1.0L / 0.0L;
306 float y = expf(x);
308 if (fabsf(y) > 0.0 || signbit(y) != 0)
309 atf_tc_fail_nonfatal("expf(-Inf) != +0.0");
312 ATF_TC(expf_inf_pos);
313 ATF_TC_HEAD(expf_inf_pos, tc)
315 atf_tc_set_md_var(tc, "descr", "Test expf(+Inf) == +Inf");
318 ATF_TC_BODY(expf_inf_pos, tc)
320 const float x = 1.0L / 0.0L;
321 float y = expf(x);
323 if (isinf(y) == 0 || signbit(y) != 0)
324 atf_tc_fail_nonfatal("expf(+Inf) != +Inf");
327 ATF_TC(expf_product);
328 ATF_TC_HEAD(expf_product, tc)
330 atf_tc_set_md_var(tc, "descr", "Test some selected expf(x)");
333 ATF_TC_BODY(expf_product, tc)
335 float eps;
336 float x;
337 float y;
338 size_t i;
340 for (i = 0; i < __arraycount(exp_values); i++) {
341 x = exp_values[i].x;
342 y = exp_values[i].y;
343 eps = 1e-6 * exp_values[i].e;
345 if (fabsf(expf(x) - y) > eps)
346 atf_tc_fail_nonfatal("expf(%0.01f) != %18.18e", x, y);
350 ATF_TC(expf_zero_neg);
351 ATF_TC_HEAD(expf_zero_neg, tc)
353 atf_tc_set_md_var(tc, "descr", "Test expf(-0.0) == 1.0");
356 ATF_TC_BODY(expf_zero_neg, tc)
358 const float x = -0.0L;
360 if (fabsf(expf(x) - 1.0f) > 0.0)
361 atf_tc_fail_nonfatal("expf(-0.0) != 1.0");
364 ATF_TC(expf_zero_pos);
365 ATF_TC_HEAD(expf_zero_pos, tc)
367 atf_tc_set_md_var(tc, "descr", "Test expf(+0.0) == 1.0");
370 ATF_TC_BODY(expf_zero_pos, tc)
372 const float x = 0.0L;
374 if (fabsf(expf(x) - 1.0f) > 0.0)
375 atf_tc_fail_nonfatal("expf(+0.0) != 1.0");
379 * expm1(3)
381 ATF_TC(expm1_nan);
382 ATF_TC_HEAD(expm1_nan, tc)
384 atf_tc_set_md_var(tc, "descr", "Test expm1(NaN) == NaN");
387 ATF_TC_BODY(expm1_nan, tc)
389 const double x = 0.0L / 0.0L;
391 if (isnan(expm1(x)) == 0)
392 atf_tc_fail_nonfatal("expm1(NaN) != NaN");
395 ATF_TC(expm1_inf_neg);
396 ATF_TC_HEAD(expm1_inf_neg, tc)
398 atf_tc_set_md_var(tc, "descr", "Test expm1(-Inf) == -1");
401 ATF_TC_BODY(expm1_inf_neg, tc)
403 const double x = -1.0L / 0.0L;
405 if (expm1(x) != -1.0)
406 atf_tc_fail_nonfatal("expm1(-Inf) != -1.0");
409 ATF_TC(expm1_inf_pos);
410 ATF_TC_HEAD(expm1_inf_pos, tc)
412 atf_tc_set_md_var(tc, "descr", "Test expm1(+Inf) == +Inf");
415 ATF_TC_BODY(expm1_inf_pos, tc)
417 const double x = 1.0L / 0.0L;
418 double y = expm1(x);
420 if (isinf(y) == 0 || signbit(y) != 0)
421 atf_tc_fail_nonfatal("expm1(+Inf) != +Inf");
424 ATF_TC(expm1_zero_neg);
425 ATF_TC_HEAD(expm1_zero_neg, tc)
427 atf_tc_set_md_var(tc, "descr", "Test expm1(-0.0) == -0.0");
430 ATF_TC_BODY(expm1_zero_neg, tc)
432 const double x = -0.0L;
433 double y = expm1(x);
435 if (fabs(y) > 0.0 || signbit(y) == 0)
436 atf_tc_fail_nonfatal("expm1(-0.0) != -0.0");
439 ATF_TC(expm1_zero_pos);
440 ATF_TC_HEAD(expm1_zero_pos, tc)
442 atf_tc_set_md_var(tc, "descr", "Test expm1(+0.0) == 1.0");
445 ATF_TC_BODY(expm1_zero_pos, tc)
447 const double x = 0.0L;
448 double y = expm1(x);
450 if (fabs(y) > 0.0 || signbit(y) != 0)
451 atf_tc_fail_nonfatal("expm1(+0.0) != +0.0");
455 * expm1f(3)
457 ATF_TC(expm1f_nan);
458 ATF_TC_HEAD(expm1f_nan, tc)
460 atf_tc_set_md_var(tc, "descr", "Test expm1f(NaN) == NaN");
463 ATF_TC_BODY(expm1f_nan, tc)
465 const float x = 0.0L / 0.0L;
467 if (isnan(expm1f(x)) == 0)
468 atf_tc_fail_nonfatal("expm1f(NaN) != NaN");
471 ATF_TC(expm1f_inf_neg);
472 ATF_TC_HEAD(expm1f_inf_neg, tc)
474 atf_tc_set_md_var(tc, "descr", "Test expm1f(-Inf) == -1");
477 ATF_TC_BODY(expm1f_inf_neg, tc)
479 const float x = -1.0L / 0.0L;
481 if (expm1f(x) != -1.0)
482 atf_tc_fail_nonfatal("expm1f(-Inf) != -1.0");
485 ATF_TC(expm1f_inf_pos);
486 ATF_TC_HEAD(expm1f_inf_pos, tc)
488 atf_tc_set_md_var(tc, "descr", "Test expm1f(+Inf) == +Inf");
491 ATF_TC_BODY(expm1f_inf_pos, tc)
493 const float x = 1.0L / 0.0L;
494 float y = expm1f(x);
496 if (isinf(y) == 0 || signbit(y) != 0)
497 atf_tc_fail_nonfatal("expm1f(+Inf) != +Inf");
500 ATF_TC(expm1f_zero_neg);
501 ATF_TC_HEAD(expm1f_zero_neg, tc)
503 atf_tc_set_md_var(tc, "descr", "Test expm1f(-0.0) == -0.0");
506 ATF_TC_BODY(expm1f_zero_neg, tc)
508 const float x = -0.0L;
509 float y = expm1f(x);
511 if (fabsf(y) > 0.0 || signbit(y) == 0)
512 atf_tc_fail_nonfatal("expm1f(-0.0) != -0.0");
515 ATF_TC(expm1f_zero_pos);
516 ATF_TC_HEAD(expm1f_zero_pos, tc)
518 atf_tc_set_md_var(tc, "descr", "Test expm1f(+0.0) == 1.0");
521 ATF_TC_BODY(expm1f_zero_pos, tc)
523 const float x = 0.0L;
524 float y = expm1f(x);
526 if (fabsf(y) > 0.0 || signbit(y) != 0)
527 atf_tc_fail_nonfatal("expm1f(+0.0) != +0.0");
530 ATF_TP_ADD_TCS(tp)
533 ATF_TP_ADD_TC(tp, exp2_is_nan);
534 ATF_TP_ADD_TC(tp, exp2_is_plus_zero);
535 ATF_TP_ADD_TC(tp, exp2_values);
536 ATF_TP_ADD_TC(tp, exp2_powers);
538 ATF_TP_ADD_TC(tp, exp_nan);
539 ATF_TP_ADD_TC(tp, exp_inf_neg);
540 ATF_TP_ADD_TC(tp, exp_inf_pos);
541 ATF_TP_ADD_TC(tp, exp_product);
542 ATF_TP_ADD_TC(tp, exp_zero_neg);
543 ATF_TP_ADD_TC(tp, exp_zero_pos);
545 ATF_TP_ADD_TC(tp, expf_nan);
546 ATF_TP_ADD_TC(tp, expf_inf_neg);
547 ATF_TP_ADD_TC(tp, expf_inf_pos);
548 ATF_TP_ADD_TC(tp, expf_product);
549 ATF_TP_ADD_TC(tp, expf_zero_neg);
550 ATF_TP_ADD_TC(tp, expf_zero_pos);
552 ATF_TP_ADD_TC(tp, expm1_nan);
553 ATF_TP_ADD_TC(tp, expm1_inf_neg);
554 ATF_TP_ADD_TC(tp, expm1_inf_pos);
555 ATF_TP_ADD_TC(tp, expm1_zero_neg);
556 ATF_TP_ADD_TC(tp, expm1_zero_pos);
558 ATF_TP_ADD_TC(tp, expm1f_nan);
559 ATF_TP_ADD_TC(tp, expm1f_inf_neg);
560 ATF_TP_ADD_TC(tp, expm1f_inf_pos);
561 ATF_TP_ADD_TC(tp, expm1f_zero_neg);
562 ATF_TP_ADD_TC(tp, expm1f_zero_pos);
564 return atf_no_error();