1 /* $NetBSD: t_exp.c,v 1.7 2014/03/17 11:08:11 martin Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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, },
56 ATF_LIBM_TEST(exp2_is_nan, "Test exp2(x) == NaN")
59 T_LIBM_CHECK_NAN(0, exp2, T_LIBM_NAN);
60 T_LIBM_CHECK_NAN(0, exp2f, T_LIBM_NAN);
62 atf_tc_skip("no NaN on this machine");
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);
72 atf_tc_skip("no +/-Inf on this machine");
76 ATF_LIBM_TEST(exp2_powers, "Test exp2(x) is correct for some integer x")
87 { 100, 0x1p100, 0x1p100 },
88 { 125, 0x1p125, 0x1p125 },
89 { 126, 0x1p126, 0x1p126 },
90 #if __DBL_MAX_EXP__ > 129
91 { 127, 0x1p127, 0x1p127 },
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 },
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},
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 {
148 #if __DBL_MAX_EXP__ > 128
149 /* The largest double constant */
150 { 0x1.fffffffffffffp9, 0x1.ffffffffffd3ap1023,
152 /* The largest float constant */
153 { 0x1.fffffep6, 0x1.ffff4ep+127, 6e30, 0.0 },
155 #ifdef T_LIBM_PLUS_INF
156 { T_LIBM_PLUS_INF, T_LIBM_PLUS_INF, 0.0, 0.0 },
159 /* The few values from the old tests */
160 /* Results from i386/amd64, d_eps needed on i386 */
161 { 1.1, 0x1.125fbee250664p+1, 0x1p-52, 0x1.8p-22 },
162 { 2.2, 0x1.2611186bae675p+2, 0x1p-51, 0x1.8p-21 },
163 { 3.3, 0x1.3b2c47bff8328p+3, 0x1p-50, 0x1.8p-20 },
164 { 4.4, 0x1.51cb453b9536ep+4, 0x1p-49, 0x1.8p-19 },
165 { 5.5, 0x1.6a09e667f3bcdp+5, 0x1p-48, 0x1.8p-18 },
166 { 6.6, 0x1.8406003b2ae5bp+6, 0x1p-47, 0x1.8p-17 },
168 * These two currently fail for 'float'.
169 * 8.8 is definitely out by more than it should be.
171 { 7.7, 0x1.9fdf8bcce533ep+7, 0x1p-46, 0x1.8p-16 },
172 { 8.8, 0x1.bdb8cdadbe124p+8, 0x1p-45, 0x1.8p-15 },
176 for (i = 0; i < __arraycount(v); i++) {
177 T_LIBM_CHECK(i, exp2, v[i].x, v[i].y, v[i].d_eps);
179 T_LIBM_CHECK(i, exp2f, v[i].x, v[i].y, v[i].f_eps);
188 ATF_TC_HEAD(exp_nan, tc)
190 atf_tc_set_md_var(tc, "descr", "Test exp(NaN) == NaN");
193 ATF_TC_BODY(exp_nan, tc)
195 const double x = 0.0L / 0.0L;
197 if (isnan(exp(x)) == 0)
198 atf_tc_fail_nonfatal("exp(NaN) != NaN");
202 ATF_TC_HEAD(exp_inf_neg, tc)
204 atf_tc_set_md_var(tc, "descr", "Test exp(-Inf) == +0.0");
207 ATF_TC_BODY(exp_inf_neg, tc)
209 const double x = -1.0L / 0.0L;
212 if (fabs(y) > 0.0 || signbit(y) != 0)
213 atf_tc_fail_nonfatal("exp(-Inf) != +0.0");
217 ATF_TC_HEAD(exp_inf_pos, tc)
219 atf_tc_set_md_var(tc, "descr", "Test exp(+Inf) == +Inf");
222 ATF_TC_BODY(exp_inf_pos, tc)
224 const double x = 1.0L / 0.0L;
227 if (isinf(y) == 0 || signbit(y) != 0)
228 atf_tc_fail_nonfatal("exp(+Inf) != +Inf");
232 ATF_TC_HEAD(exp_product, tc)
234 atf_tc_set_md_var(tc, "descr", "Test some selected exp(x)");
237 ATF_TC_BODY(exp_product, tc)
244 for (i = 0; i < __arraycount(exp_values); i++) {
247 eps = 1e-15 * exp_values[i].e;
249 if (fabs(exp(x) - y) > eps)
250 atf_tc_fail_nonfatal("exp(%0.01f) != %18.18e", x, y);
254 ATF_TC(exp_zero_neg);
255 ATF_TC_HEAD(exp_zero_neg, tc)
257 atf_tc_set_md_var(tc, "descr", "Test exp(-0.0) == 1.0");
260 ATF_TC_BODY(exp_zero_neg, tc)
262 const double x = -0.0L;
264 if (fabs(exp(x) - 1.0) > 0.0)
265 atf_tc_fail_nonfatal("exp(-0.0) != 1.0");
268 ATF_TC(exp_zero_pos);
269 ATF_TC_HEAD(exp_zero_pos, tc)
271 atf_tc_set_md_var(tc, "descr", "Test exp(+0.0) == 1.0");
274 ATF_TC_BODY(exp_zero_pos, tc)
276 const double x = 0.0L;
278 if (fabs(exp(x) - 1.0) > 0.0)
279 atf_tc_fail_nonfatal("exp(+0.0) != 1.0");
286 ATF_TC_HEAD(expf_nan, tc)
288 atf_tc_set_md_var(tc, "descr", "Test expf(NaN) == NaN");
291 ATF_TC_BODY(expf_nan, tc)
293 const float x = 0.0L / 0.0L;
295 if (isnan(expf(x)) == 0)
296 atf_tc_fail_nonfatal("expf(NaN) != NaN");
299 ATF_TC(expf_inf_neg);
300 ATF_TC_HEAD(expf_inf_neg, tc)
302 atf_tc_set_md_var(tc, "descr", "Test expf(-Inf) == +0.0");
305 ATF_TC_BODY(expf_inf_neg, tc)
307 const float x = -1.0L / 0.0L;
310 if (fabsf(y) > 0.0 || signbit(y) != 0)
311 atf_tc_fail_nonfatal("expf(-Inf) != +0.0");
314 ATF_TC(expf_inf_pos);
315 ATF_TC_HEAD(expf_inf_pos, tc)
317 atf_tc_set_md_var(tc, "descr", "Test expf(+Inf) == +Inf");
320 ATF_TC_BODY(expf_inf_pos, tc)
322 const float x = 1.0L / 0.0L;
325 if (isinf(y) == 0 || signbit(y) != 0)
326 atf_tc_fail_nonfatal("expf(+Inf) != +Inf");
329 ATF_TC(expf_product);
330 ATF_TC_HEAD(expf_product, tc)
332 atf_tc_set_md_var(tc, "descr", "Test some selected expf(x)");
335 ATF_TC_BODY(expf_product, tc)
342 for (i = 0; i < __arraycount(exp_values); i++) {
345 eps = 1e-6 * exp_values[i].e;
347 if (fabsf(expf(x) - y) > eps)
348 atf_tc_fail_nonfatal("expf(%0.01f) != %18.18e", x, y);
352 ATF_TC(expf_zero_neg);
353 ATF_TC_HEAD(expf_zero_neg, tc)
355 atf_tc_set_md_var(tc, "descr", "Test expf(-0.0) == 1.0");
358 ATF_TC_BODY(expf_zero_neg, tc)
360 const float x = -0.0L;
362 if (fabsf(expf(x) - 1.0f) > 0.0)
363 atf_tc_fail_nonfatal("expf(-0.0) != 1.0");
366 ATF_TC(expf_zero_pos);
367 ATF_TC_HEAD(expf_zero_pos, tc)
369 atf_tc_set_md_var(tc, "descr", "Test expf(+0.0) == 1.0");
372 ATF_TC_BODY(expf_zero_pos, tc)
374 const float x = 0.0L;
376 if (fabsf(expf(x) - 1.0f) > 0.0)
377 atf_tc_fail_nonfatal("expf(+0.0) != 1.0");
384 ATF_TC_HEAD(expm1_nan, tc)
386 atf_tc_set_md_var(tc, "descr", "Test expm1(NaN) == NaN");
389 ATF_TC_BODY(expm1_nan, tc)
391 const double x = 0.0L / 0.0L;
393 if (isnan(expm1(x)) == 0)
394 atf_tc_fail_nonfatal("expm1(NaN) != NaN");
397 ATF_TC(expm1_inf_neg);
398 ATF_TC_HEAD(expm1_inf_neg, tc)
400 atf_tc_set_md_var(tc, "descr", "Test expm1(-Inf) == -1");
403 ATF_TC_BODY(expm1_inf_neg, tc)
405 const double x = -1.0L / 0.0L;
407 if (expm1(x) != -1.0)
408 atf_tc_fail_nonfatal("expm1(-Inf) != -1.0");
411 ATF_TC(expm1_inf_pos);
412 ATF_TC_HEAD(expm1_inf_pos, tc)
414 atf_tc_set_md_var(tc, "descr", "Test expm1(+Inf) == +Inf");
417 ATF_TC_BODY(expm1_inf_pos, tc)
419 const double x = 1.0L / 0.0L;
422 if (isinf(y) == 0 || signbit(y) != 0)
423 atf_tc_fail_nonfatal("expm1(+Inf) != +Inf");
426 ATF_TC(expm1_zero_neg);
427 ATF_TC_HEAD(expm1_zero_neg, tc)
429 atf_tc_set_md_var(tc, "descr", "Test expm1(-0.0) == -0.0");
432 ATF_TC_BODY(expm1_zero_neg, tc)
434 const double x = -0.0L;
437 if (fabs(y) > 0.0 || signbit(y) == 0)
438 atf_tc_fail_nonfatal("expm1(-0.0) != -0.0");
441 ATF_TC(expm1_zero_pos);
442 ATF_TC_HEAD(expm1_zero_pos, tc)
444 atf_tc_set_md_var(tc, "descr", "Test expm1(+0.0) == 1.0");
447 ATF_TC_BODY(expm1_zero_pos, tc)
449 const double x = 0.0L;
452 if (fabs(y) > 0.0 || signbit(y) != 0)
453 atf_tc_fail_nonfatal("expm1(+0.0) != +0.0");
460 ATF_TC_HEAD(expm1f_nan, tc)
462 atf_tc_set_md_var(tc, "descr", "Test expm1f(NaN) == NaN");
465 ATF_TC_BODY(expm1f_nan, tc)
467 const float x = 0.0L / 0.0L;
469 if (isnan(expm1f(x)) == 0)
470 atf_tc_fail_nonfatal("expm1f(NaN) != NaN");
473 ATF_TC(expm1f_inf_neg);
474 ATF_TC_HEAD(expm1f_inf_neg, tc)
476 atf_tc_set_md_var(tc, "descr", "Test expm1f(-Inf) == -1");
479 ATF_TC_BODY(expm1f_inf_neg, tc)
481 const float x = -1.0L / 0.0L;
483 if (expm1f(x) != -1.0)
484 atf_tc_fail_nonfatal("expm1f(-Inf) != -1.0");
487 ATF_TC(expm1f_inf_pos);
488 ATF_TC_HEAD(expm1f_inf_pos, tc)
490 atf_tc_set_md_var(tc, "descr", "Test expm1f(+Inf) == +Inf");
493 ATF_TC_BODY(expm1f_inf_pos, tc)
495 const float x = 1.0L / 0.0L;
498 if (isinf(y) == 0 || signbit(y) != 0)
499 atf_tc_fail_nonfatal("expm1f(+Inf) != +Inf");
502 ATF_TC(expm1f_zero_neg);
503 ATF_TC_HEAD(expm1f_zero_neg, tc)
505 atf_tc_set_md_var(tc, "descr", "Test expm1f(-0.0) == -0.0");
508 ATF_TC_BODY(expm1f_zero_neg, tc)
510 const float x = -0.0L;
513 if (fabsf(y) > 0.0 || signbit(y) == 0)
514 atf_tc_fail_nonfatal("expm1f(-0.0) != -0.0");
517 ATF_TC(expm1f_zero_pos);
518 ATF_TC_HEAD(expm1f_zero_pos, tc)
520 atf_tc_set_md_var(tc, "descr", "Test expm1f(+0.0) == 1.0");
523 ATF_TC_BODY(expm1f_zero_pos, tc)
525 const float x = 0.0L;
528 if (fabsf(y) > 0.0 || signbit(y) != 0)
529 atf_tc_fail_nonfatal("expm1f(+0.0) != +0.0");
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);
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);
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);
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);
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);
566 return atf_no_error();