1 /* $NetBSD: t_exp.c,v 1.3 2013/04/09 11:42:56 isaki 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.
41 { -10, 0.4539992976248485e-4, 1e-4, },
42 { -5, 0.6737946999085467e-2, 1e-2, },
43 { -1, 0.3678794411714423, 1e-1, },
44 { -0.1, 0.9048374180359595, 1e-1, },
45 { 0, 1.0000000000000000, 1, },
46 { 0.1, 1.1051709180756477, 1, },
47 { 1, 2.7182818284590452, 1, },
48 { 5, 148.41315910257660, 1e2
, },
49 { 10, 22026.465794806718, 1e4
, },
56 ATF_TC_HEAD(exp2_nan
, tc
)
58 atf_tc_set_md_var(tc
, "descr", "Test exp2(NaN) == NaN");
61 ATF_TC_BODY(exp2_nan
, tc
)
64 const double x
= 0.0L / 0.0L;
66 if (isnan(exp2(x
)) == 0)
67 atf_tc_fail_nonfatal("exp2(NaN) != NaN");
72 ATF_TC_HEAD(exp2_inf_neg
, tc
)
74 atf_tc_set_md_var(tc
, "descr", "Test exp2(-Inf) == +0.0");
77 ATF_TC_BODY(exp2_inf_neg
, tc
)
80 const double x
= -1.0L / 0.0L;
83 if (fabs(y
) > 0.0 || signbit(y
) != 0)
84 atf_tc_fail_nonfatal("exp2(-Inf) != +0.0");
89 ATF_TC_HEAD(exp2_inf_pos
, tc
)
91 atf_tc_set_md_var(tc
, "descr", "Test exp2(+Inf) == +Inf");
94 ATF_TC_BODY(exp2_inf_pos
, tc
)
97 const double x
= 1.0L / 0.0L;
100 if (isinf(y
) == 0 || signbit(y
) != 0)
101 atf_tc_fail_nonfatal("exp2(+Inf) != +Inf");
105 ATF_TC(exp2_product
);
106 ATF_TC_HEAD(exp2_product
, tc
)
108 atf_tc_set_md_var(tc
, "descr", "Test exp2(x + y) == exp2(x) * exp2(y)");
111 ATF_TC_BODY(exp2_product
, tc
)
114 const double x
[] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 };
115 const double y
[] = { 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0 };
116 const double eps
= 1.0e-11;
119 for (i
= 0; i
< __arraycount(x
); i
++) {
121 if (fabs(exp2(x
[i
] + y
[i
]) - (exp2(x
[i
]) * exp2(y
[i
]))) > eps
)
122 atf_tc_fail_nonfatal("exp2(%0.01f + %0.01f) != exp2("
123 "%0.01f) * exp2(%0.01f)", x
[i
], y
[i
], x
[i
], y
[i
]);
128 ATF_TC(exp2_zero_neg
);
129 ATF_TC_HEAD(exp2_zero_neg
, tc
)
131 atf_tc_set_md_var(tc
, "descr", "Test exp2(-0.0) == 1.0");
134 ATF_TC_BODY(exp2_zero_neg
, tc
)
137 const double x
= -0.0L;
139 if (fabs(exp2(x
) - 1.0) > 0.0)
140 atf_tc_fail_nonfatal("exp2(-0.0) != 1.0");
144 ATF_TC(exp2_zero_pos
);
145 ATF_TC_HEAD(exp2_zero_pos
, tc
)
147 atf_tc_set_md_var(tc
, "descr", "Test exp2(+0.0) == 1.0");
150 ATF_TC_BODY(exp2_zero_pos
, tc
)
153 const double x
= 0.0L;
155 if (fabs(exp2(x
) - 1.0) > 0.0)
156 atf_tc_fail_nonfatal("exp2(+0.0) != 1.0");
164 ATF_TC_HEAD(exp2f_nan
, tc
)
166 atf_tc_set_md_var(tc
, "descr", "Test exp2f(NaN) == NaN");
169 ATF_TC_BODY(exp2f_nan
, tc
)
172 const float x
= 0.0L / 0.0L;
174 if (isnan(exp2f(x
)) == 0)
175 atf_tc_fail_nonfatal("exp2f(NaN) != NaN");
179 ATF_TC(exp2f_inf_neg
);
180 ATF_TC_HEAD(exp2f_inf_neg
, tc
)
182 atf_tc_set_md_var(tc
, "descr", "Test exp2f(-Inf) == +0.0");
185 ATF_TC_BODY(exp2f_inf_neg
, tc
)
188 const float x
= -1.0L / 0.0L;
191 if (fabsf(y
) > 0.0 || signbit(y
) != 0)
192 atf_tc_fail_nonfatal("exp2f(-Inf) != +0.0");
196 ATF_TC(exp2f_inf_pos
);
197 ATF_TC_HEAD(exp2f_inf_pos
, tc
)
199 atf_tc_set_md_var(tc
, "descr", "Test exp2f(+Inf) == +Inf");
202 ATF_TC_BODY(exp2f_inf_pos
, tc
)
205 const float x
= 1.0L / 0.0L;
208 if (isinf(y
) == 0 || signbit(y
) != 0)
209 atf_tc_fail_nonfatal("exp2f(+Inf) != +Inf");
213 ATF_TC(exp2f_product
);
214 ATF_TC_HEAD(exp2f_product
, tc
)
216 atf_tc_set_md_var(tc
, "descr", "Test exp2f(x+y) == exp2f(x) * exp2f(y)");
219 ATF_TC_BODY(exp2f_product
, tc
)
222 const float x
[] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 };
223 const float y
[] = { 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0 };
224 const float eps
= 1.0e-2;
227 for (i
= 0; i
< __arraycount(x
); i
++) {
229 if (fabsf(exp2f(x
[i
] + y
[i
]) -
230 (exp2f(x
[i
]) * exp2f(y
[i
]))) > eps
)
231 atf_tc_fail_nonfatal("exp2f(%0.01f + %0.01f) != exp2f("
232 "%0.01f) * exp2f(%0.01f)", x
[i
], y
[i
], x
[i
], y
[i
]);
237 ATF_TC(exp2f_zero_neg
);
238 ATF_TC_HEAD(exp2f_zero_neg
, tc
)
240 atf_tc_set_md_var(tc
, "descr", "Test exp2f(-0.0) == 1.0");
243 ATF_TC_BODY(exp2f_zero_neg
, tc
)
246 const float x
= -0.0L;
248 if (fabsf(exp2f(x
) - 1.0) > 0.0)
249 atf_tc_fail_nonfatal("exp2f(-0.0) != 1.0");
253 ATF_TC(exp2f_zero_pos
);
254 ATF_TC_HEAD(exp2f_zero_pos
, tc
)
256 atf_tc_set_md_var(tc
, "descr", "Test exp2f(+0.0) == 1.0");
259 ATF_TC_BODY(exp2f_zero_pos
, tc
)
262 const float x
= 0.0L;
264 if (fabsf(exp2f(x
) - 1.0) > 0.0)
265 atf_tc_fail_nonfatal("exp2f(+0.0) != 1.0");
273 ATF_TC_HEAD(exp_nan
, tc
)
275 atf_tc_set_md_var(tc
, "descr", "Test exp(NaN) == NaN");
278 ATF_TC_BODY(exp_nan
, tc
)
281 const double x
= 0.0L / 0.0L;
283 if (isnan(exp(x
)) == 0)
284 atf_tc_fail_nonfatal("exp(NaN) != NaN");
289 ATF_TC_HEAD(exp_inf_neg
, tc
)
291 atf_tc_set_md_var(tc
, "descr", "Test exp(-Inf) == +0.0");
294 ATF_TC_BODY(exp_inf_neg
, tc
)
297 const double x
= -1.0L / 0.0L;
300 if (fabs(y
) > 0.0 || signbit(y
) != 0)
301 atf_tc_fail_nonfatal("exp(-Inf) != +0.0");
306 ATF_TC_HEAD(exp_inf_pos
, tc
)
308 atf_tc_set_md_var(tc
, "descr", "Test exp(+Inf) == +Inf");
311 ATF_TC_BODY(exp_inf_pos
, tc
)
314 const double x
= 1.0L / 0.0L;
317 if (isinf(y
) == 0 || signbit(y
) != 0)
318 atf_tc_fail_nonfatal("exp(+Inf) != +Inf");
323 ATF_TC_HEAD(exp_product
, tc
)
325 atf_tc_set_md_var(tc
, "descr", "Test some selected exp(x)");
328 ATF_TC_BODY(exp_product
, tc
)
336 for (i
= 0; i
< __arraycount(exp_values
); i
++) {
339 eps
= 1e-15 * exp_values
[i
].e
;
341 if (fabs(exp(x
) - y
) > eps
)
342 atf_tc_fail_nonfatal("exp(%0.01f) != %18.18e", x
, y
);
347 ATF_TC(exp_zero_neg
);
348 ATF_TC_HEAD(exp_zero_neg
, tc
)
350 atf_tc_set_md_var(tc
, "descr", "Test exp(-0.0) == 1.0");
353 ATF_TC_BODY(exp_zero_neg
, tc
)
356 const double x
= -0.0L;
358 if (fabs(exp(x
) - 1.0) > 0.0)
359 atf_tc_fail_nonfatal("exp(-0.0) != 1.0");
363 ATF_TC(exp_zero_pos
);
364 ATF_TC_HEAD(exp_zero_pos
, tc
)
366 atf_tc_set_md_var(tc
, "descr", "Test exp(+0.0) == 1.0");
369 ATF_TC_BODY(exp_zero_pos
, tc
)
372 const double x
= 0.0L;
374 if (fabs(exp(x
) - 1.0) > 0.0)
375 atf_tc_fail_nonfatal("exp(+0.0) != 1.0");
383 ATF_TC_HEAD(expf_nan
, tc
)
385 atf_tc_set_md_var(tc
, "descr", "Test expf(NaN) == NaN");
388 ATF_TC_BODY(expf_nan
, tc
)
391 const float x
= 0.0L / 0.0L;
393 if (isnan(expf(x
)) == 0)
394 atf_tc_fail_nonfatal("expf(NaN) != NaN");
398 ATF_TC(expf_inf_neg
);
399 ATF_TC_HEAD(expf_inf_neg
, tc
)
401 atf_tc_set_md_var(tc
, "descr", "Test expf(-Inf) == +0.0");
404 ATF_TC_BODY(expf_inf_neg
, tc
)
407 const float x
= -1.0L / 0.0L;
410 if (fabsf(y
) > 0.0 || signbit(y
) != 0)
411 atf_tc_fail_nonfatal("expf(-Inf) != +0.0");
415 ATF_TC(expf_inf_pos
);
416 ATF_TC_HEAD(expf_inf_pos
, tc
)
418 atf_tc_set_md_var(tc
, "descr", "Test expf(+Inf) == +Inf");
421 ATF_TC_BODY(expf_inf_pos
, tc
)
424 const float x
= 1.0L / 0.0L;
427 if (isinf(y
) == 0 || signbit(y
) != 0)
428 atf_tc_fail_nonfatal("expf(+Inf) != +Inf");
432 ATF_TC(expf_product
);
433 ATF_TC_HEAD(expf_product
, tc
)
435 atf_tc_set_md_var(tc
, "descr", "Test some selected expf(x)");
438 ATF_TC_BODY(expf_product
, tc
)
446 for (i
= 0; i
< __arraycount(exp_values
); i
++) {
449 eps
= 1e-6 * exp_values
[i
].e
;
451 if (fabsf(expf(x
) - y
) > eps
)
452 atf_tc_fail_nonfatal("expf(%0.01f) != %18.18e", x
, y
);
457 ATF_TC(expf_zero_neg
);
458 ATF_TC_HEAD(expf_zero_neg
, tc
)
460 atf_tc_set_md_var(tc
, "descr", "Test expf(-0.0) == 1.0");
463 ATF_TC_BODY(expf_zero_neg
, tc
)
466 const float x
= -0.0L;
468 if (fabsf(expf(x
) - 1.0) > 0.0)
469 atf_tc_fail_nonfatal("expf(-0.0) != 1.0");
473 ATF_TC(expf_zero_pos
);
474 ATF_TC_HEAD(expf_zero_pos
, tc
)
476 atf_tc_set_md_var(tc
, "descr", "Test expf(+0.0) == 1.0");
479 ATF_TC_BODY(expf_zero_pos
, tc
)
482 const float x
= 0.0L;
484 if (fabsf(expf(x
) - 1.0) > 0.0)
485 atf_tc_fail_nonfatal("expf(+0.0) != 1.0");
493 ATF_TC_HEAD(expm1_nan
, tc
)
495 atf_tc_set_md_var(tc
, "descr", "Test expm1(NaN) == NaN");
498 ATF_TC_BODY(expm1_nan
, tc
)
501 const double x
= 0.0L / 0.0L;
503 if (isnan(expm1(x
)) == 0)
504 atf_tc_fail_nonfatal("expm1(NaN) != NaN");
508 ATF_TC(expm1_inf_neg
);
509 ATF_TC_HEAD(expm1_inf_neg
, tc
)
511 atf_tc_set_md_var(tc
, "descr", "Test expm1(-Inf) == -1");
514 ATF_TC_BODY(expm1_inf_neg
, tc
)
517 const double x
= -1.0L / 0.0L;
519 if (expm1(x
) != -1.0)
520 atf_tc_fail_nonfatal("expm1(-Inf) != -1.0");
524 ATF_TC(expm1_inf_pos
);
525 ATF_TC_HEAD(expm1_inf_pos
, tc
)
527 atf_tc_set_md_var(tc
, "descr", "Test expm1(+Inf) == +Inf");
530 ATF_TC_BODY(expm1_inf_pos
, tc
)
533 const double x
= 1.0L / 0.0L;
536 if (isinf(y
) == 0 || signbit(y
) != 0)
537 atf_tc_fail_nonfatal("expm1(+Inf) != +Inf");
541 ATF_TC(expm1_zero_neg
);
542 ATF_TC_HEAD(expm1_zero_neg
, tc
)
544 atf_tc_set_md_var(tc
, "descr", "Test expm1(-0.0) == -0.0");
547 ATF_TC_BODY(expm1_zero_neg
, tc
)
550 const double x
= -0.0L;
553 if (fabs(y
) > 0.0 || signbit(y
) == 0)
554 atf_tc_fail_nonfatal("expm1(-0.0) != -0.0");
558 ATF_TC(expm1_zero_pos
);
559 ATF_TC_HEAD(expm1_zero_pos
, tc
)
561 atf_tc_set_md_var(tc
, "descr", "Test expm1(+0.0) == 1.0");
564 ATF_TC_BODY(expm1_zero_pos
, tc
)
567 const double x
= 0.0L;
570 if (fabs(y
) > 0.0 || signbit(y
) != 0)
571 atf_tc_fail_nonfatal("expm1(+0.0) != +0.0");
579 ATF_TC_HEAD(expm1f_nan
, tc
)
581 atf_tc_set_md_var(tc
, "descr", "Test expm1f(NaN) == NaN");
584 ATF_TC_BODY(expm1f_nan
, tc
)
587 const float x
= 0.0L / 0.0L;
589 if (isnan(expm1f(x
)) == 0)
590 atf_tc_fail_nonfatal("expm1f(NaN) != NaN");
594 ATF_TC(expm1f_inf_neg
);
595 ATF_TC_HEAD(expm1f_inf_neg
, tc
)
597 atf_tc_set_md_var(tc
, "descr", "Test expm1f(-Inf) == -1");
600 ATF_TC_BODY(expm1f_inf_neg
, tc
)
603 const float x
= -1.0L / 0.0L;
605 if (expm1f(x
) != -1.0)
606 atf_tc_fail_nonfatal("expm1f(-Inf) != -1.0");
610 ATF_TC(expm1f_inf_pos
);
611 ATF_TC_HEAD(expm1f_inf_pos
, tc
)
613 atf_tc_set_md_var(tc
, "descr", "Test expm1f(+Inf) == +Inf");
616 ATF_TC_BODY(expm1f_inf_pos
, tc
)
619 const float x
= 1.0L / 0.0L;
622 if (isinf(y
) == 0 || signbit(y
) != 0)
623 atf_tc_fail_nonfatal("expm1f(+Inf) != +Inf");
627 ATF_TC(expm1f_zero_neg
);
628 ATF_TC_HEAD(expm1f_zero_neg
, tc
)
630 atf_tc_set_md_var(tc
, "descr", "Test expm1f(-0.0) == -0.0");
633 ATF_TC_BODY(expm1f_zero_neg
, tc
)
636 const float x
= -0.0L;
639 if (fabsf(y
) > 0.0 || signbit(y
) == 0)
640 atf_tc_fail_nonfatal("expm1f(-0.0) != -0.0");
644 ATF_TC(expm1f_zero_pos
);
645 ATF_TC_HEAD(expm1f_zero_pos
, tc
)
647 atf_tc_set_md_var(tc
, "descr", "Test expm1f(+0.0) == 1.0");
650 ATF_TC_BODY(expm1f_zero_pos
, tc
)
653 const float x
= 0.0L;
656 if (fabsf(y
) > 0.0 || signbit(y
) != 0)
657 atf_tc_fail_nonfatal("expm1f(+0.0) != +0.0");
664 ATF_TP_ADD_TC(tp
, exp2_nan
);
665 ATF_TP_ADD_TC(tp
, exp2_inf_neg
);
666 ATF_TP_ADD_TC(tp
, exp2_inf_pos
);
667 ATF_TP_ADD_TC(tp
, exp2_product
);
668 ATF_TP_ADD_TC(tp
, exp2_zero_neg
);
669 ATF_TP_ADD_TC(tp
, exp2_zero_pos
);
671 ATF_TP_ADD_TC(tp
, exp2f_nan
);
672 ATF_TP_ADD_TC(tp
, exp2f_inf_neg
);
673 ATF_TP_ADD_TC(tp
, exp2f_inf_pos
);
674 ATF_TP_ADD_TC(tp
, exp2f_product
);
675 ATF_TP_ADD_TC(tp
, exp2f_zero_neg
);
676 ATF_TP_ADD_TC(tp
, exp2f_zero_pos
);
678 ATF_TP_ADD_TC(tp
, exp_nan
);
679 ATF_TP_ADD_TC(tp
, exp_inf_neg
);
680 ATF_TP_ADD_TC(tp
, exp_inf_pos
);
681 ATF_TP_ADD_TC(tp
, exp_product
);
682 ATF_TP_ADD_TC(tp
, exp_zero_neg
);
683 ATF_TP_ADD_TC(tp
, exp_zero_pos
);
685 ATF_TP_ADD_TC(tp
, expf_nan
);
686 ATF_TP_ADD_TC(tp
, expf_inf_neg
);
687 ATF_TP_ADD_TC(tp
, expf_inf_pos
);
688 ATF_TP_ADD_TC(tp
, expf_product
);
689 ATF_TP_ADD_TC(tp
, expf_zero_neg
);
690 ATF_TP_ADD_TC(tp
, expf_zero_pos
);
692 ATF_TP_ADD_TC(tp
, expm1_nan
);
693 ATF_TP_ADD_TC(tp
, expm1_inf_neg
);
694 ATF_TP_ADD_TC(tp
, expm1_inf_pos
);
695 ATF_TP_ADD_TC(tp
, expm1_zero_neg
);
696 ATF_TP_ADD_TC(tp
, expm1_zero_pos
);
698 ATF_TP_ADD_TC(tp
, expm1f_nan
);
699 ATF_TP_ADD_TC(tp
, expm1f_inf_neg
);
700 ATF_TP_ADD_TC(tp
, expm1f_inf_pos
);
701 ATF_TP_ADD_TC(tp
, expm1f_zero_neg
);
702 ATF_TP_ADD_TC(tp
, expm1f_zero_pos
);
704 return atf_no_error();