Update.
[glibc.git] / math / libm-test.c
blob05acca2c5371aebbe73244a805a8786eeca8be5c
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@arthur.pfalz.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
22 Part of testsuite for libm.
24 This file has to be included by a master file that defines:
26 Makros:
27 FUNC(function): converts general function name (like cos) to
28 name with correct suffix (e.g. cosl or cosf)
29 MATHCONST(x): like FUNC but for constants (e.g convert 0.0 to 0.0L)
30 MATHTYPE: floating point type to test
31 TEST_MSG: informal message to be displayed
32 CHOOSE(Clongdouble,Cdouble,Cfloat):
33 chooses one of the parameters as epsilon for testing
34 equality
35 PRINTF_EXPR Floating point conversion specification to print a variable
36 of type MATHTYPE with printf. PRINTF_EXPR just contains
37 the specifier, not the percent and width arguments,
38 e.g. "f"
41 /* This program isn't finished yet.
42 It has tests for acos, acosh, asin, asinh, atan, atan2, atanh,
43 cbrt, ceil, copysign, cos, cosh, exp, exp2, expm1,
44 fabs, fdim, floor, fmin, fmax, fpclassify,
45 frexp, hypot, ilogb, ldexp,
46 log, log10, log1p, log2, logb, modf, nextafter,
47 pow, scalb, scalbn, sin, sinh, sqrt, tan, tanh, trunc.
48 Tests for the other libm-functions will come later.
50 The routines using random variables are still under construction. I don't
51 like it the way it's working now and will change it.
53 Exception handling has not been implemented so far so don't get fooled
54 that these tests pass.
56 Parameter handling is primitive in the moment:
57 --verbose=[0..3] for different levels of output:
58 0: only error count
59 1: basic report on failed tests
60 2: full report on failed tests
61 3: full report on failed and passed tests (default)
62 -v for full output (equals --verbose=3)
63 -s,--silent outputs only the error count (equals --verbose=0)
66 /* Define if the following ISO C 9X functions are implemented: exp2,
67 log2. */
68 #undef ISO_9X_IMPLEMENTED
70 #ifndef _GNU_SOURCE
71 # define _GNU_SOURCE
72 #endif
74 #include <complex.h>
75 #include <math.h>
76 #include <float.h>
78 #include <errno.h>
79 #include <stdlib.h>
80 #include <stdio.h>
81 #include <time.h>
82 #include <getopt.h>
84 /* TEST_EXCEPTION: tests if an exception as occured */
85 /* for the moment: does nothing */
86 /* Possible exceptions */
87 #define NO_EXCEPTION 0x0
88 #define INVALID_EXCEPTION 0x1
89 #define DIVIDE_BY_ZERO_EXCEPTION 0x2
91 #define PRINT 1
92 #define NO_PRINT 0
94 #define TEST_EXCEPTION(test) do {} while (0);
95 /* As long as no exception code is available prevent warnings. */
96 #define UNUSED __attribute__ ((unused))
98 static int noErrors;
100 static int verbose = 3;
101 static MATHTYPE minus_zero, plus_zero;
102 static MATHTYPE plus_infty, minus_infty, nan_value;
104 typedef MATHTYPE (*mathfunc) (MATHTYPE);
107 #define ISINF(x) \
108 (sizeof (x) == sizeof (float) ? \
109 isinff (x) \
110 : sizeof (x) == sizeof (double) ? \
111 isinf (x) : isinfl (x))
115 Test if Floating-Point stack hasn't changed
117 static void
118 fpstack_test (const char *test_name)
120 #ifdef i386
121 static int old_stack;
122 int sw;
123 asm ("fnstsw":"=a" (sw));
124 sw >>= 11;
125 sw &= 7;
126 if (sw != old_stack)
128 printf ("FP-Stack wrong after test %s\n", test_name);
129 if (verbose > 2)
130 printf ("=======> stack = %d\n", sw);
131 ++noErrors;
132 old_stack = sw;
134 #endif
139 Call to an external function so that floating point registers
140 get moved to memory
142 static void
143 this_does_nothing (void)
145 clock_t dummy;
147 dummy = clock ();
151 Get a random value x with min_value < x < max_value
152 and min_value, max_value finite,
153 max_value and min_value shouldn't be too close together
155 static MATHTYPE
156 random_value (MATHTYPE min_value, MATHTYPE max_value)
158 int r;
159 MATHTYPE x;
161 r = rand ();
163 x = (max_value - min_value) / RAND_MAX * (MATHTYPE) r + min_value;
165 if ((x <= min_value) || (x >= max_value) || !isfinite (x))
166 x = (max_value - min_value) / 2 + min_value;
168 return x;
171 /* Get a random value x with x > min_value. */
172 static MATHTYPE
173 random_greater (MATHTYPE min_value)
175 return random_value (min_value, 1e6); /* CHOOSE (LDBL_MAX, DBL_MAX, FLT_MAX) */
178 /* Get a random value x with x < max_value. */
179 static MATHTYPE
180 random_less (MATHTYPE max_value)
182 return random_value (-1e6, max_value);
186 /* Test if two floating point numbers are equal. */
187 static int
188 check_equal (MATHTYPE computed, MATHTYPE supplied, MATHTYPE eps, MATHTYPE * diff)
190 /* Both plus Infinity or both minus infinity. */
191 if (ISINF (computed) && (ISINF (computed) == ISINF (supplied)))
192 return 1;
194 if (isnan (computed) && isnan (supplied)) /* isnan works for all types */
195 return 1;
197 *diff = FUNC(fabs) (computed - supplied);
199 if (*diff <= eps && (signbit (computed) == signbit (supplied) || eps != 0.0))
200 return 1;
202 return 0;
206 static void
207 output_result_bool (const char *test_name, int result)
209 if (result)
211 if (verbose > 2)
212 printf ("Pass: %s\n", test_name);
214 else
216 if (verbose)
217 printf ("Fail: %s\n", test_name);
218 ++noErrors;
221 fpstack_test (test_name);
225 static void
226 output_isvalue (const char *test_name, int result,
227 MATHTYPE value)
229 if (result)
231 if (verbose > 2)
232 printf ("Pass: %s\n", test_name);
234 else
236 if (verbose)
237 printf ("Fail: %s\n", test_name);
238 if (verbose > 1)
239 printf (" Value: %.20" PRINTF_EXPR "\n", value);
240 noErrors++;
243 fpstack_test (test_name);
247 static void
248 output_isvalue_ext (const char *test_name, int result,
249 MATHTYPE value, MATHTYPE parameter)
251 if (result)
253 if (verbose > 2)
254 printf ("Pass: %s\n", test_name);
256 else
258 if (verbose)
259 printf ("Fail: %s\n", test_name);
260 if (verbose > 1)
262 printf (" Value: %.20" PRINTF_EXPR "\n", value);
263 printf (" Parameter: %.20" PRINTF_EXPR "\n", parameter);
265 noErrors++;
268 fpstack_test (test_name);
272 static void
273 output_result (const char *test_name, int result,
274 MATHTYPE computed, MATHTYPE expected,
275 MATHTYPE difference,
276 int print_values, int print_diff)
278 if (result)
280 if (verbose > 2)
281 printf ("Pass: %s\n", test_name);
283 else
285 if (verbose)
286 printf ("Fail: %s\n", test_name);
287 if (verbose > 1 && print_values)
289 printf ("Result:\n");
290 printf (" is: %.20" PRINTF_EXPR "\n", computed);
291 printf (" should be: %.20" PRINTF_EXPR "\n", expected);
292 if (print_diff)
293 printf (" difference: %.20" PRINTF_EXPR "\n", difference);
295 noErrors++;
298 fpstack_test (test_name);
302 static void
303 output_result_ext (const char *test_name, int result,
304 MATHTYPE computed, MATHTYPE expected,
305 MATHTYPE difference,
306 MATHTYPE parameter,
307 int print_values, int print_diff)
309 if (result)
311 if (verbose > 2)
312 printf ("Pass: %s\n", test_name);
314 else
316 if (verbose)
317 printf ("Fail: %s\n", test_name);
318 if (verbose > 1 && print_values)
320 printf ("Result:\n");
321 printf (" is: %.20" PRINTF_EXPR "\n", computed);
322 printf (" should be: %.20" PRINTF_EXPR "\n", expected);
323 if (print_diff)
324 printf (" difference: %.20" PRINTF_EXPR "\n", difference);
325 printf ("Parameter: %.20" PRINTF_EXPR "\n", parameter);
327 noErrors++;
330 fpstack_test (test_name);
334 static void
335 check (const char *test_name, MATHTYPE computed, MATHTYPE expected)
337 MATHTYPE diff;
338 int result;
340 result = check_equal (computed, expected, 0, &diff);
341 output_result (test_name, result,
342 computed, expected, diff, PRINT, PRINT);
346 static void
347 check_ext (const char *test_name, MATHTYPE computed, MATHTYPE expected,
348 MATHTYPE parameter)
350 MATHTYPE diff;
351 int result;
353 result = check_equal (computed, expected, 0, &diff);
354 output_result_ext (test_name, result,
355 computed, expected, diff, parameter, PRINT, PRINT);
359 static void
360 check_eps (const char *test_name, MATHTYPE computed, MATHTYPE expected,
361 MATHTYPE epsilon)
363 MATHTYPE diff;
364 int result;
366 result = check_equal (computed, expected, epsilon, &diff);
367 output_result (test_name, result,
368 computed, expected, diff, PRINT, PRINT);
372 static void
373 check_bool (const char *test_name, int computed)
375 output_result_bool (test_name, computed);
379 static void
380 check_isnan (const char *test_name, MATHTYPE computed)
382 output_isvalue (test_name, isnan (computed), computed);
386 static void
387 check_isnan_exc (const char *test_name, MATHTYPE computed,
388 short exception UNUSED)
390 output_isvalue (test_name, isnan (computed), computed);
394 static void
395 check_isnan_ext (const char *test_name, MATHTYPE computed,
396 MATHTYPE parameter)
398 output_isvalue_ext (test_name, isnan (computed), computed, parameter);
402 /* Tests if computed is +Inf */
403 static void
404 check_isinfp (const char *test_name, MATHTYPE computed)
406 output_isvalue (test_name, (ISINF (computed) == +1), computed);
410 static void
411 check_isinfp_ext (const char *test_name, MATHTYPE computed,
412 MATHTYPE parameter)
414 output_isvalue_ext (test_name, (ISINF (computed) == +1), computed, parameter);
418 /* Tests if computed is +Inf */
419 static void
420 check_isinfp_exc (const char *test_name, MATHTYPE computed,
421 int exception UNUSED)
423 output_isvalue (test_name, (ISINF (computed) == +1), computed);
426 /* Tests if computed is -Inf */
427 static void
428 check_isinfn (const char *test_name, MATHTYPE computed)
430 output_isvalue (test_name, (ISINF (computed) == -1), computed);
434 static void
435 check_isinfn_ext (const char *test_name, MATHTYPE computed,
436 MATHTYPE parameter)
438 output_isvalue_ext (test_name, (ISINF (computed) == -1), computed, parameter);
442 /* Tests if computed is -Inf */
443 static void
444 check_isinfn_exc (const char *test_name, MATHTYPE computed,
445 int exception UNUSED)
447 output_isvalue (test_name, (ISINF (computed) == -1), computed);
451 /****************************************************************************
452 Test for single functions of libm
453 ****************************************************************************/
455 static void
456 acos_test (void)
458 MATHTYPE x;
460 check ("acos (1) == 0", FUNC(acos) (1), 0);
462 x = random_greater (1);
463 check_isnan_exc ("acos (x) == NaN + invalid exception for |x| > 1",
464 FUNC(acos) (x),
465 INVALID_EXCEPTION);
468 static void
469 acosh_test (void)
471 MATHTYPE x;
473 check ("acosh(1) == 0", FUNC(acosh) (1), 0);
474 check_isinfp ("acosh(+inf) == +inf", FUNC(acosh) (plus_infty));
476 x = random_less (1);
477 check_isnan_exc ("acosh(x) == NaN plus invalid exception if x < 1",
478 FUNC(acosh) (x), INVALID_EXCEPTION);
482 static void
483 asin_test (void)
485 MATHTYPE x;
486 check ("asin (0) == 0", FUNC(asin) (0), 0);
488 x = random_greater (1);
489 check_isnan_exc ("asin x == NaN + invalid exception for |x| > 1",
490 FUNC(asin) (x),
491 INVALID_EXCEPTION);
494 static void
495 asinh_test (void)
498 check ("asinh(+0) == +0", FUNC(asinh) (0), 0);
499 check ("asinh(-0) == -0", FUNC(asinh) (minus_zero), minus_zero);
503 static void
504 atan_test (void)
506 check ("atan (0) == 0", FUNC(atan) (0), 0);
507 check ("atan (-0) == -0", FUNC(atan) (minus_zero), minus_zero);
509 check ("atan (+inf) == pi/2", FUNC(atan) (plus_infty), M_PI_2);
510 check ("atan (-inf) == -pi/2", FUNC(atan) (minus_infty), -M_PI_2);
514 static void
515 atan2_test (void)
517 MATHTYPE x;
519 x = random_greater (0);
520 check ("atan2 (0,x) == 0 for x > 0",
521 FUNC(atan2) (0, x), 0);
522 x = random_greater (0);
523 check ("atan2 (-0,x) == -0 for x > 0",
524 FUNC(atan2) (minus_zero, x), minus_zero);
526 check ("atan2 (+0,+0) == +0", FUNC(atan2) (0, 0), 0);
527 check ("atan2 (-0,+0) == -0", FUNC(atan2) (minus_zero, 0), minus_zero);
529 x = -random_greater (0);
530 check ("atan2 (+0,x) == +pi for x < 0", FUNC(atan2) (0, x), M_PI);
532 x = -random_greater (0);
533 check ("atan2 (-0,x) == -pi for x < 0", FUNC(atan2) (minus_zero, x), -M_PI);
535 check ("atan2 (+0,-0) == +pi", FUNC(atan2) (0, minus_zero), M_PI);
536 check ("atan2 (-0,-0) == -pi", FUNC(atan2) (minus_zero, minus_zero), -M_PI);
538 x = random_greater (0);
539 check ("atan2 (y,+0) == pi/2 for y > 0", FUNC(atan2) (x, 0), M_PI_2);
541 x = random_greater (0);
542 check ("atan2 (y,-0) == pi/2 for y > 0", FUNC(atan2) (x, minus_zero), M_PI_2);
544 x = random_greater (0);
545 check ("atan2 (y,-inf) == +pi for finite y > 0",
546 FUNC(atan2) (x, minus_infty), M_PI);
548 x = -random_greater (0);
549 check ("atan2 (y,-inf) == -pi for finite y < 0",
550 FUNC(atan2) (x, minus_infty), -M_PI);
552 check ("atan2 (+inf,+inf) == +pi/4",
553 FUNC(atan2) (plus_infty, plus_infty), M_PI_4);
555 check ("atan2 (-inf,+inf) == -pi/4",
556 FUNC(atan2) (minus_infty, plus_infty), -M_PI_4);
558 check ("atan2 (+inf,-inf) == +3*pi/4",
559 FUNC(atan2) (plus_infty, minus_infty), 3 * M_PI_4);
561 check ("atan2 (-inf,-inf) == -3*pi/4",
562 FUNC(atan2) (minus_infty, minus_infty), -3 * M_PI_4);
566 static void
567 atanh_test (void)
570 check ("atanh(+0) == +0", FUNC(atanh) (0), 0);
571 check ("atanh(-0) == -0", FUNC(atanh) (minus_zero), minus_zero);
572 check_isinfp_exc ("atanh(+1) == +inf plus divide-by-zero exception",
573 FUNC(atanh) (1), DIVIDE_BY_ZERO_EXCEPTION);
574 check_isinfn_exc ("atanh(-1) == -inf plus divide-by-zero exception",
575 FUNC(atanh) (-1), DIVIDE_BY_ZERO_EXCEPTION);
579 static void
580 cbrt_test (void)
582 check ("cbrt (+0) == +0", FUNC(cbrt) (0.0), 0.0);
583 check ("cbrt (-0) == -0", FUNC(cbrt) (minus_zero), minus_zero);
585 check ("cbrt (8) == 2", FUNC(cbrt) (8), 2);
586 check ("cbrt (-27) == -3", FUNC(cbrt) (-27.0), -3.0);
590 static void
591 ceil_test (void)
593 check ("ceil (+0) == +0", FUNC(ceil) (0.0), 0.0);
594 check ("ceil (-0) == -0", FUNC(ceil) (minus_zero), minus_zero);
595 check_isinfp ("ceil (+inf) == +inf", FUNC(ceil) (plus_infty));
596 check_isinfn ("ceil (-inf) == -inf", FUNC(ceil) (minus_infty));
598 check ("ceil (pi) == 4", FUNC(ceil) (M_PI), 4.0);
599 check ("ceil (-pi) == -3", FUNC(ceil) (-M_PI), -3.0);
603 static void
604 cos_test (void)
607 check ("cos (+0) == 1", FUNC(cos) (0), 1);
608 check ("cos (-0) == 1", FUNC(cos) (minus_zero), 1);
609 check_isnan_exc ("cos (+inf) == NaN plus invalid exception",
610 FUNC(cos) (plus_infty),
611 INVALID_EXCEPTION);
612 check_isnan_exc ("cos (-inf) == NaN plus invalid exception",
613 FUNC(cos) (minus_infty),
614 INVALID_EXCEPTION);
616 check_eps ("cos (pi/3) == 0.5", FUNC(cos) (M_PI / 3.0),
617 0.5, CHOOSE (4e-18L, 1e-15L, 1e-7L));
618 check_eps ("cos (pi/2) == 0", FUNC(cos) (M_PI_2),
619 0, CHOOSE (1e-19L, 1e-16L, 1e-7L));
623 static void
624 cosh_test (void)
626 check ("cosh (+0) == 1", FUNC(cosh) (0), 1);
627 check ("cosh (-0) == 1", FUNC(cosh) (minus_zero), 1);
629 check_isinfp ("cosh (+inf) == +inf", FUNC(cosh) (plus_infty));
630 check_isinfp ("cosh (-inf) == +inf", FUNC(cosh) (minus_infty));
634 static void
635 exp_test (void)
637 check ("exp (+0) == 1", FUNC(exp) (0), 1);
638 check ("exp (-0) == 1", FUNC(exp) (minus_zero), 1);
640 check_isinfp ("exp (+inf) == +inf", FUNC(exp) (plus_infty));
641 check ("exp (-inf) == 0", FUNC(exp) (minus_infty), 0);
643 check_eps ("exp (1) == e", FUNC(exp) (1), M_E, CHOOSE (4e-18L, 0, 0));
647 #ifdef ISO_9X_IMPLEMENTED
648 static void
649 exp2_test (void)
651 check ("exp2 (+0) == 1", FUNC(exp2) (0), 1);
652 check ("exp2 (-0) == 1", FUNC(exp2) (minus_zero), 1);
654 check_isinfp ("exp2 (+inf) == +inf", FUNC(exp2) (plus_infty));
655 check ("exp2 (-inf) == 0", FUNC(exp2) (minus_infty), 0);
656 check ("exp2 (10) == 1024", FUNC(exp2) (10), 1024);
658 #endif
661 static void
662 expm1_test (void)
664 check ("expm1 (+0) == 0", FUNC(expm1) (0), 0);
665 check ("expm1 (-0) == -0", FUNC(expm1) (minus_zero), minus_zero);
667 check_isinfp ("expm1 (+inf) == +inf", FUNC(expm1) (plus_infty));
668 check ("expm1 (-inf) == -1", FUNC(expm1) (minus_infty), -1);
670 check_eps ("expm1 (1) == e-1", FUNC(expm1) (1), M_E - 1.0,
671 CHOOSE (4e-18L, 0, 0));
677 static void
678 check_frexp (const char *test_name, MATHTYPE computed, MATHTYPE expected,
679 int comp_int, int exp_int)
681 MATHTYPE diff;
682 int result;
684 result = (check_equal (computed, expected, 0, &diff)
685 && (comp_int == exp_int));
687 if (result)
689 if (verbose > 2)
690 printf ("Pass: %s\n", test_name);
692 else
694 if (verbose)
695 printf ("Fail: %s\n", test_name);
696 if (verbose > 1)
698 printf ("Result:\n");
699 printf (" is: %.20" PRINTF_EXPR " *2^%d\n", computed, comp_int);
700 printf (" should be: %.20" PRINTF_EXPR " *2^%d\n", expected, exp_int);
701 printf (" difference: %.20" PRINTF_EXPR "\n", diff);
703 noErrors++;
705 fpstack_test (test_name);
706 output_result (test_name, result,
707 computed, expected, diff, PRINT, PRINT);
711 static void
712 frexp_test (void)
714 int x_int;
715 MATHTYPE result;
717 result = FUNC(frexp) (plus_infty, &x_int);
718 check_isinfp ("frexp (+inf, expr) == +inf", result);
720 result = FUNC(frexp) (minus_infty, &x_int);
721 check_isinfn ("frexp (-inf, expr) == -inf", result);
723 result = FUNC(frexp) (nan_value, &x_int);
724 check_isnan ("frexp (Nan, expr) == NaN", result);
726 result = FUNC(frexp) (0, &x_int);
727 check_frexp ("frexp: +0 == 0 * 2^0", result, 0, x_int, 0);
729 result = FUNC(frexp) (minus_zero, &x_int);
730 check_frexp ("frexp: -0 == -0 * 2^0", result, minus_zero, x_int, 0);
732 result = FUNC(frexp) (12.8L, &x_int);
733 check_frexp ("frexp: 12.8 == 0.8 * 2^4", result, 0.8L, x_int, 4);
735 result = FUNC(frexp) (-27.34L, &x_int);
736 check_frexp ("frexp: -27.34 == -0.854375 * 2^5", result, -0.854375L, x_int, 5);
741 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)
742 /* All floating-point numbers can be put in one of these categories. */
743 enum
745 FP_NAN,
746 #define FP_NAN FP_NAN
747 FP_INFINITE,
748 #define FP_INFINITE FP_INFINITE
749 FP_ZERO,
750 #define FP_ZERO FP_ZERO
751 FP_SUBNORMAL,
752 #define FP_SUBNORMAL FP_SUBNORMAL
753 FP_NORMAL
754 #define FP_NORMAL FP_NORMAL
756 #endif
759 static void
760 fpclassify_test (void)
762 MATHTYPE x;
764 /* fpclassify is a macro, don't give it constants as parameter */
765 check_bool ("fpclassify (NaN) == FP_NAN", fpclassify (nan_value) == FP_NAN);
766 check_bool ("fpclassify (+inf) == FP_INFINITE",
767 fpclassify (plus_infty) == FP_INFINITE);
768 check_bool ("fpclassify (-inf) == FP_INFINITE",
769 fpclassify (minus_infty) == FP_INFINITE);
770 check_bool ("fpclassify (+0) == FP_ZERO",
771 fpclassify (plus_zero) == FP_ZERO);
772 check_bool ("fpclassify (-0) == FP_ZERO",
773 fpclassify (minus_zero) == FP_ZERO);
775 x = 1000.0;
776 check_bool ("fpclassify (1000) == FP_NORMAL",
777 fpclassify (x) == FP_NORMAL);
781 static void
782 ilogb_test (void)
785 /* XXX Are these tests correct? I couldn't find any specification */
786 #if 0
787 /* the source suggests that the following calls should fail -
788 but shall we test these special cases or just ignore them? */
789 check_isinfp ("ilogb (+inf) == +inf", FUNC(ilogb) (plus_infty));
790 check_isinfp ("ilogb (-inf) == +inf", FUNC(ilogb) (minus_infty));
792 check_isinfn_exc ("ilogb (+0) == -inf plus divide-by-zero exception",
793 FUNC(ilogb) (0), DIVIDE_BY_ZERO_EXCEPTION);
795 check_isinfn_exc ("ilogb (-0) == -inf plus divide-by-zero exception",
796 FUNC(ilogb) (minus_zero), DIVIDE_BY_ZERO_EXCEPTION);
797 #endif
798 check ("ilogb (1) == 0", FUNC(ilogb) (1), 0);
799 check ("ilogb (e) == 1", FUNC(ilogb) (M_E), 1);
800 check ("ilogb (1024) == 10", FUNC(ilogb) (1024), 10);
801 check ("ilogb (-2000) == 10", FUNC(ilogb) (-2000), 10);
806 static void
807 ldexp_test (void)
809 MATHTYPE x;
811 check ("ldexp (0, 0) == 0", FUNC(ldexp) (0, 0), 0);
813 check_isinfp ("ldexp (+inf, 1) == +inf", FUNC(ldexp) (plus_infty, 1));
814 check_isinfn ("ldexp (-inf, 1) == -inf", FUNC(ldexp) (minus_infty, 1));
815 check_isnan ("ldexp (NaN, 1) == NaN", FUNC(ldexp) (nan_value, 1));
817 check ("ldexp (0.8, 4) == 12.8", FUNC(ldexp) (0.8L, 4), 12.8L);
818 check ("ldexp (-0.854375, 5) == -27.34", FUNC(ldexp) (-0.854375L, 5), -27.34L);
820 x = random_greater (0.0);
821 check_ext ("ldexp (x, 0) == x", FUNC(ldexp) (x, 0L), x, x);
826 static void
827 log_test (void)
829 check_isinfn_exc ("log (+0) == -inf", FUNC(log) (0),
830 DIVIDE_BY_ZERO_EXCEPTION);
831 check_isinfn_exc ("log (-0) == -inf", FUNC(log) (minus_zero),
832 DIVIDE_BY_ZERO_EXCEPTION);
834 check ("log (1) == 0", FUNC(log) (1), 0);
836 check_isnan_exc ("log (x) == NaN plus divide-by-zero exception if x < 0",
837 FUNC(log) (-1), INVALID_EXCEPTION);
838 check_isinfp ("log (+inf) == +inf", FUNC(log) (plus_infty));
840 check_eps ("log (e) == 1", FUNC(log) (M_E), 1, CHOOSE (1e-18L, 0, 9e-8L));
841 check_eps ("log (1/e) == -1", FUNC(log) (1.0 / M_E), -1,
842 CHOOSE (2e-18L, 0, 0));
843 check ("log (2) == M_LN2", FUNC(log) (2), M_LN2);
844 check_eps ("log (10) == M_LN10", FUNC(log) (10), M_LN10,
845 CHOOSE (1e-18L, 0, 0));
849 static void
850 log10_test (void)
852 check_isinfn_exc ("log10 (+0) == -inf", FUNC(log10) (0),
853 DIVIDE_BY_ZERO_EXCEPTION);
854 check_isinfn_exc ("log10 (-0) == -inf", FUNC(log10) (minus_zero),
855 DIVIDE_BY_ZERO_EXCEPTION);
857 check ("log10 (1) == +0", FUNC(log10) (1), 0);
859 check_isnan_exc ("log10 (x) == NaN plus divide-by-zero exception if x < 0",
860 FUNC(log10) (-1), INVALID_EXCEPTION);
862 check_isinfp ("log10 (+inf) == +inf", FUNC(log10) (plus_infty));
864 check_eps ("log10 (0.1) == -1", FUNC(log10) (0.1L), -1,
865 CHOOSE (1e-18L, 0, 0));
866 check_eps ("log10 (10) == 1", FUNC(log10) (10.0), 1,
867 CHOOSE (1e-18L, 0, 0));
868 check_eps ("log10 (100) == 2", FUNC(log10) (100.0), 2,
869 CHOOSE (1e-18L, 0, 0));
870 check ("log10 (10000) == 4", FUNC(log10) (10000.0), 4);
871 check_eps ("log10 (e) == M_LOG10E", FUNC(log10) (M_E), M_LOG10E,
872 CHOOSE (1e-18, 0, 9e-8));
876 static void
877 log1p_test (void)
879 check ("log1p (+0) == +0", FUNC(log1p) (0), 0);
880 check ("log1p (-0) == -0", FUNC(log1p) (minus_zero), minus_zero);
882 check_isinfn_exc ("log1p (-1) == -inf", FUNC(log1p) (-1),
883 DIVIDE_BY_ZERO_EXCEPTION);
884 check_isnan_exc ("log1p (x) == NaN plus divide-by-zero exception if x < -1",
885 FUNC(log1p) (-2), INVALID_EXCEPTION);
887 check_isinfp ("log1p (+inf) == +inf", FUNC(log1p) (plus_infty));
889 check_eps ("log1p (e-1) == 1", FUNC(log1p) (M_E - 1.0), 1,
890 CHOOSE (1e-18L, 0, 0));
895 #ifdef ISO_9X_IMPLEMENTED
896 static void
897 log2_test (void)
899 check_isinfn_exc ("log2 (+0) == -inf", FUNC(log2) (0),
900 DIVIDE_BY_ZERO_EXCEPTION);
901 check_isinfn_exc ("log2 (-0) == -inf", FUNC(log2) (minus_zero),
902 DIVIDE_BY_ZERO_EXCEPTION);
904 check ("log2 (1) == +0", FUNC(log2) (1), 0);
906 check_isnan_exc ("log2 (x) == NaN plus divide-by-zero exception if x < 0",
907 FUNC(log2) (-1), INVALID_EXCEPTION);
909 check_isinfp ("log2 (+inf) == +inf", FUNC(log2) (plus_infty));
911 check ("log2 (e) == M_LOG2E", FUNC(log2) (M_E), M_LOG2E);
912 check ("log2 (2) == 1", FUNC(log2) (2.0), 1);
913 check ("log2 (16) == 4", FUNC(log2) (16.0), 4);
914 check ("log2 (256) == 8", FUNC(log2) (256.0), 8);
917 #endif
920 static void
921 logb_test (void)
923 check_isinfp ("logb (+inf) == +inf", FUNC(logb) (plus_infty));
924 check_isinfp ("logb (-inf) == +inf", FUNC(logb) (minus_infty));
926 check_isinfn_exc ("logb (+0) == -inf plus divide-by-zero exception",
927 FUNC(logb) (0), DIVIDE_BY_ZERO_EXCEPTION);
929 check_isinfn_exc ("logb (-0) == -inf plus divide-by-zero exception",
930 FUNC(logb) (minus_zero), DIVIDE_BY_ZERO_EXCEPTION);
932 check ("logb (1) == 0", FUNC(logb) (1), 0);
933 check ("logb (e) == 1", FUNC(logb) (M_E), 1);
934 check ("logb (1024) == 10", FUNC(logb) (1024), 10);
935 check ("logb (-2000) == 10", FUNC(logb) (-2000), 10);
940 static void
941 modf_test (void)
943 MATHTYPE result, intpart;
945 result = FUNC(modf) (plus_infty, &intpart);
946 check ("modf (+inf, &x) returns +0", result, 0);
947 check_isinfp ("modf (+inf, &x) set x to +inf", intpart);
949 result = FUNC(modf) (minus_infty, &intpart);
950 check ("modf (-inf, &x) returns -0", result, minus_zero);
951 check_isinfn ("modf (-inf, &x) sets x to -inf", intpart);
953 result = FUNC(modf) (nan_value, &intpart);
954 check_isnan ("modf (NaN, &x) returns NaN", result);
955 check_isnan ("modf (-inf, &x) sets x to NaN", intpart);
957 result = FUNC(modf) (0, &intpart);
958 check ("modf (0, &x) returns 0", result, 0);
959 check ("modf (0, &x) sets x to 0", intpart, 0);
961 result = FUNC(modf) (minus_zero, &intpart);
962 check ("modf (-0, &x) returns -0", result, minus_zero);
963 check ("modf (-0, &x) sets x to -0", intpart, minus_zero);
965 result = FUNC(modf) (2.5, &intpart);
966 check ("modf (2.5, &x) returns 0.5", result, 0.5);
967 check ("modf (2.5, &x) sets x to 2", intpart, 2);
969 result = FUNC(modf) (-2.5, &intpart);
970 check ("modf (-2.5, &x) returns -0.5", result, -0.5);
971 check ("modf (-2.5, &x) sets x to -2", intpart, -2);
976 static void
977 scalb_test (void)
979 MATHTYPE x;
981 check ("scalb (0, 0) == 0", FUNC(scalb) (0, 0), 0);
983 check_isinfp ("scalb (+inf, 1) == +inf", FUNC(scalb) (plus_infty, 1));
984 check_isinfn ("scalb (-inf, 1) == -inf", FUNC(scalb) (minus_infty, 1));
985 check_isnan ("scalb (NaN, 1) == NaN", FUNC(scalb) (nan_value, 1));
987 check ("scalb (0.8, 4) == 12.8", FUNC(scalb) (0.8L, 4), 12.8L);
988 check ("scalb (-0.854375, 5) == -27.34", FUNC(scalb) (-0.854375L, 5), -27.34L);
990 x = random_greater (0.0);
991 check_ext ("scalb (x, 0) == x", FUNC(scalb) (x, 0L), x, x);
995 static void
996 scalbn_test (void)
998 MATHTYPE x;
1000 check ("scalbn (0, 0) == 0", FUNC(scalbn) (0, 0), 0);
1002 check_isinfp ("scalbn (+inf, 1) == +inf", FUNC(scalbn) (plus_infty, 1));
1003 check_isinfn ("scalbn (-inf, 1) == -inf", FUNC(scalbn) (minus_infty, 1));
1004 check_isnan ("scalbn (NaN, 1) == NaN", FUNC(scalbn) (nan_value, 1));
1006 check ("scalbn (0.8, 4) == 12.8", FUNC(scalbn) (0.8L, 4), 12.8L);
1007 check ("scalbn (-0.854375, 5) == -27.34", FUNC(scalbn) (-0.854375L, 5), -27.34L);
1009 x = random_greater (0.0);
1010 check_ext ("scalbn (x, 0) == x", FUNC(scalbn) (x, 0L), x, x);
1014 static void
1015 sin_test (void)
1017 check ("sin (+0) == +0", FUNC(sin) (0), 0);
1018 check ("sin (-0) == -0", FUNC(sin) (minus_zero), minus_zero);
1019 check_isnan_exc ("sin (+inf) == NaN plus invalid exception",
1020 FUNC(sin) (plus_infty),
1021 INVALID_EXCEPTION);
1022 check_isnan_exc ("sin (-inf) == NaN plus invalid exception",
1023 FUNC(sin) (minus_infty),
1024 INVALID_EXCEPTION);
1026 check_eps ("sin (pi/6) == 0.5", FUNC(sin) (M_PI / 6.0), 0.5,
1027 CHOOSE (4e-18L, 0, 0));
1028 check ("sin (pi/2) == 1", FUNC(sin) (M_PI_2), 1);
1032 static void
1033 sinh_test (void)
1035 check ("sinh (+0) == +0", FUNC(sinh) (0), 0);
1036 check ("sinh (-0) == -0", FUNC(sinh) (minus_zero), minus_zero);
1038 check_isinfp ("sinh (+inf) == +inf", FUNC(sinh) (plus_infty));
1039 check_isinfn ("sinh (-inf) == -inf", FUNC(sinh) (minus_infty));
1043 static void
1044 tan_test (void)
1046 check ("tan (+0) == +0", FUNC(tan) (0), 0);
1047 check ("tan (-0) == -0", FUNC(tan) (minus_zero), minus_zero);
1048 check_isnan_exc ("tan (+inf) == NaN plus invalid exception",
1049 FUNC(tan) (plus_infty), INVALID_EXCEPTION);
1050 check_isnan_exc ("tan (-inf) == NaN plus invalid exception",
1051 FUNC(tan) (minus_infty), INVALID_EXCEPTION);
1053 check_eps ("tan (pi/4) == 1", FUNC(tan) (M_PI_4), 1,
1054 CHOOSE (2e-18L, 1e-15L, 0));
1058 static void
1059 tanh_test (void)
1061 check ("tanh (+0) == +0", FUNC(tanh) (0), 0);
1062 check ("tanh (-0) == -0", FUNC(tanh) (minus_zero), minus_zero);
1064 check ("tanh (+inf) == +1", FUNC(tanh) (plus_infty), 1);
1065 check ("tanh (-inf) == -1", FUNC(tanh) (minus_infty), -1);
1069 static void
1070 fabs_test (void)
1072 check ("fabs (+0) == +0", FUNC(fabs) (0), 0);
1073 check ("fabs (-0) == +0", FUNC(fabs) (minus_zero), 0);
1075 check_isinfp ("fabs (+inf) == +inf", FUNC(fabs) (plus_infty));
1076 check_isinfp ("fabs (-inf) == +inf", FUNC(fabs) (minus_infty));
1078 check ("fabs (+38) == 38", FUNC(fabs) (38.0), 38.0);
1079 check ("fabs (-e) == e", FUNC(fabs) (-M_E), M_E);
1083 static void
1084 floor_test (void)
1086 check ("floor (+0) == +0", FUNC(floor) (0.0), 0.0);
1087 check ("floor (-0) == -0", FUNC(floor) (minus_zero), minus_zero);
1088 check_isinfp ("floor (+inf) == +inf", FUNC(floor) (plus_infty));
1089 check_isinfn ("floor (-inf) == -inf", FUNC(floor) (minus_infty));
1091 check ("floor (pi) == 3", FUNC(floor) (M_PI), 3.0);
1092 check ("floor (-pi) == -4", FUNC(floor) (-M_PI), -4.0);
1096 static void
1097 hypot_test (void)
1099 MATHTYPE a;
1101 a = random_greater (0);
1102 check_isinfp_ext ("hypot (+inf, x) == +inf", FUNC(hypot) (plus_infty, a), a);
1103 check_isinfp_ext ("hypot (-inf, x) == +inf", FUNC(hypot) (minus_infty, a), a);
1105 check_isnan ("hypot (NaN, NaN) == NaN", FUNC(hypot) (nan_value, nan_value));
1107 a = FUNC(hypot) (12.4L, 0.7L);
1108 check ("hypot (x,y) == hypot (y,x)", FUNC(hypot) (0.7L, 12.4L), a);
1109 check ("hypot (x,y) == hypot (-x,y)", FUNC(hypot) (-12.4L, 0.7L), a);
1110 check ("hypot (x,y) == hypot (-y,x)", FUNC(hypot) (-0.7L, 12.4L), a);
1111 check ("hypot (x,y) == hypot (-x,-y)", FUNC(hypot) (-12.4L, -0.7L), a);
1112 check ("hypot (x,y) == hypot (-y,-x)", FUNC(hypot) (-0.7L, -12.4L), a);
1113 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (-0.7L, 0), 0.7L);
1114 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (0.7L, 0), 0.7L);
1115 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (-1.0L, 0), 1.0L);
1116 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (1.0L, 0), 1.0L);
1117 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (-5.7e7L, 0), 5.7e7L);
1118 check ("hypot (x,0) == fabs (x)", FUNC(hypot) (5.7e7L, 0), 5.7e7L);
1122 static void
1123 pow_test (void)
1125 MATHTYPE x;
1127 check ("pow (+0, +0) == 1", FUNC(pow) (0, 0), 1);
1128 check ("pow (+0, -0) == 1", FUNC(pow) (0, minus_zero), 1);
1129 check ("pow (-0, +0) == 1", FUNC(pow) (minus_zero, 0), 1);
1130 check ("pow (-0, -0) == 1", FUNC(pow) (minus_zero, minus_zero), 1);
1132 check ("pow (+10, +0) == 1", FUNC(pow) (10, 0), 1);
1133 check ("pow (+10, -0) == 1", FUNC(pow) (10, minus_zero), 1);
1134 check ("pow (-10, +0) == 1", FUNC(pow) (-10, 0), 1);
1135 check ("pow (-10, -0) == 1", FUNC(pow) (-10, minus_zero), 1);
1137 check ("pow (NaN, +0) == 1", FUNC(pow) (nan_value, 0), 1);
1138 check ("pow (NaN, -0) == 1", FUNC(pow) (nan_value, minus_zero), 1);
1140 check_isinfp ("pow (+1.1, +inf) == +inf", FUNC(pow) (1.1, plus_infty));
1141 check_isinfp ("pow (+inf, +inf) == +inf", FUNC(pow) (plus_infty, plus_infty));
1142 check_isinfp ("pow (-1.1, +inf) == +inf", FUNC(pow) (-1.1, plus_infty));
1143 check_isinfp ("pow (-inf, +inf) == +inf", FUNC(pow) (minus_infty, plus_infty));
1145 check ("pow (0.9, +inf) == +0", FUNC(pow) (0.9L, plus_infty), 0);
1146 check ("pow (1e-7, +inf) == +0", FUNC(pow) (1e-7L, plus_infty), 0);
1147 check ("pow (-0.9, +inf) == +0", FUNC(pow) (-0.9L, plus_infty), 0);
1148 check ("pow (-1e-7, +inf) == +0", FUNC(pow) (-1e-7L, plus_infty), 0);
1150 check ("pow (+1.1, -inf) == 0", FUNC(pow) (1.1, minus_infty), 0);
1151 check ("pow (+inf, -inf) == 0", FUNC(pow) (plus_infty, minus_infty), 0);
1152 check ("pow (-1.1, -inf) == 0", FUNC(pow) (-1.1, minus_infty), 0);
1153 check ("pow (-inf, -inf) == 0", FUNC(pow) (minus_infty, minus_infty), 0);
1155 check_isinfp ("pow (0.9, -inf) == +inf", FUNC(pow) (0.9L, minus_infty));
1156 check_isinfp ("pow (1e-7, -inf) == +inf", FUNC(pow) (1e-7L, minus_infty));
1157 check_isinfp ("pow (-0.9, -inf) == +inf", FUNC(pow) (-0.9L, minus_infty));
1158 check_isinfp ("pow (-1e-7, -inf) == +inf", FUNC(pow) (-1e-7L, minus_infty));
1160 check_isinfp ("pow (+inf, 1e-7) == +inf", FUNC(pow) (plus_infty, 1e-7L));
1161 check_isinfp ("pow (+inf, 1) == +inf", FUNC(pow) (plus_infty, 1));
1162 check_isinfp ("pow (+inf, 1e7) == +inf", FUNC(pow) (plus_infty, 1e7L));
1164 check ("pow (+inf, -1e-7) == 0", FUNC(pow) (plus_infty, -1e-7L), 0);
1165 check ("pow (+inf, -1) == 0", FUNC(pow) (plus_infty, -1), 0);
1166 check ("pow (+inf, -1e7) == 0", FUNC(pow) (plus_infty, -1e7L), 0);
1168 check_isinfn ("pow (-inf, 1) == -inf", FUNC(pow) (minus_infty, 1));
1169 check_isinfn ("pow (-inf, 11) == -inf", FUNC(pow) (minus_infty, 11));
1170 check_isinfn ("pow (-inf, 1001) == -inf", FUNC(pow) (minus_infty, 1001));
1172 check_isinfp ("pow (-inf, 2) == +inf", FUNC(pow) (minus_infty, 2));
1173 check_isinfp ("pow (-inf, 12) == +inf", FUNC(pow) (minus_infty, 12));
1174 check_isinfp ("pow (-inf, 1002) == +inf", FUNC(pow) (minus_infty, 1002));
1175 check_isinfp ("pow (-inf, 0.1) == +inf", FUNC(pow) (minus_infty, 0.1));
1176 check_isinfp ("pow (-inf, 1.1) == +inf", FUNC(pow) (minus_infty, 1.1));
1177 check_isinfp ("pow (-inf, 11.1) == +inf", FUNC(pow) (minus_infty, 11.1));
1178 check_isinfp ("pow (-inf, 1001.1) == +inf", FUNC(pow) (minus_infty, 1001.1));
1180 check ("pow (-inf, -1) == -0", FUNC(pow) (minus_infty, -1), minus_zero);
1181 check ("pow (-inf, -11) == -0", FUNC(pow) (minus_infty, -11), minus_zero);
1182 check ("pow (-inf, -1001) == -0", FUNC(pow) (minus_infty, -1001), minus_zero);
1184 check ("pow (-inf, -2) == +0", FUNC(pow) (minus_infty, -2), 0);
1185 check ("pow (-inf, -12) == +0", FUNC(pow) (minus_infty, -12), 0);
1186 check ("pow (-inf, -1002) == +0", FUNC(pow) (minus_infty, -1002), 0);
1187 check ("pow (-inf, -0.1) == +0", FUNC(pow) (minus_infty, -0.1), 0);
1188 check ("pow (-inf, -1.1) == +0", FUNC(pow) (minus_infty, -1.1), 0);
1189 check ("pow (-inf, -11.1) == +0", FUNC(pow) (minus_infty, -11.1), 0);
1190 check ("pow (-inf, -1001.1) == +0", FUNC(pow) (minus_infty, -1001.1), 0);
1192 check_isnan ("pow (NaN, NaN) == NaN", FUNC(pow) (nan_value, nan_value));
1193 check_isnan ("pow (0, NaN) == NaN", FUNC(pow) (0, nan_value));
1194 check_isnan ("pow (1, NaN) == NaN", FUNC(pow) (1, nan_value));
1195 check_isnan ("pow (-1, NaN) == NaN", FUNC(pow) (-1, nan_value));
1196 check_isnan ("pow (NaN, 1) == NaN", FUNC(pow) (nan_value, 1));
1197 check_isnan ("pow (NaN, -1) == NaN", FUNC(pow) (nan_value, -1));
1199 x = random_greater (0.0);
1200 check_isnan_ext ("pow (x, NaN) == NaN", FUNC(pow) (x, nan_value), x);
1202 check_isnan_exc ("pow (+1, +inf) == NaN", FUNC(pow) (1, plus_infty),
1203 INVALID_EXCEPTION);
1204 check_isnan_exc ("pow (-1, +inf) == NaN", FUNC(pow) (-1, plus_infty),
1205 INVALID_EXCEPTION);
1206 check_isnan_exc ("pow (+1, -inf) == NaN", FUNC(pow) (1, minus_infty),
1207 INVALID_EXCEPTION);
1208 check_isnan_exc ("pow (-1, -inf) == NaN", FUNC(pow) (-1, minus_infty),
1209 INVALID_EXCEPTION);
1211 check_isnan_exc ("pow (-0.1, 1.1) == NaN", FUNC(pow) (-0.1, 1.1),
1212 INVALID_EXCEPTION);
1213 check_isnan_exc ("pow (-0.1, -1.1) == NaN", FUNC(pow) (-0.1, -1.1),
1214 INVALID_EXCEPTION);
1215 check_isnan_exc ("pow (-10.1, 1.1) == NaN", FUNC(pow) (-10.1, 1.1),
1216 INVALID_EXCEPTION);
1217 check_isnan_exc ("pow (-10.1, -1.1) == NaN", FUNC(pow) (-10.1, -1.1),
1218 INVALID_EXCEPTION);
1220 check_isinfp_exc ("pow (+0, -1) == +inf", FUNC(pow) (0, -1),
1221 DIVIDE_BY_ZERO_EXCEPTION);
1222 check_isinfp_exc ("pow (+0, -11) == +inf", FUNC(pow) (0, -11),
1223 DIVIDE_BY_ZERO_EXCEPTION);
1224 check_isinfn_exc ("pow (-0, -1) == -inf", FUNC(pow) (minus_zero, -1),
1225 DIVIDE_BY_ZERO_EXCEPTION);
1226 check_isinfn_exc ("pow (-0, -11) == -inf", FUNC(pow) (minus_zero, -11),
1227 DIVIDE_BY_ZERO_EXCEPTION);
1229 check_isinfp_exc ("pow (+0, -2) == +inf", FUNC(pow) (0, -2),
1230 DIVIDE_BY_ZERO_EXCEPTION);
1231 check_isinfp_exc ("pow (+0, -11.1) == +inf", FUNC(pow) (0, -11.1),
1232 DIVIDE_BY_ZERO_EXCEPTION);
1233 check_isinfp_exc ("pow (-0, -2) == +inf", FUNC(pow) (minus_zero, -2),
1234 DIVIDE_BY_ZERO_EXCEPTION);
1235 check_isinfp_exc ("pow (-0, -11.1) == +inf", FUNC(pow) (minus_zero, -11.1),
1236 DIVIDE_BY_ZERO_EXCEPTION);
1238 check ("pow (+0, 1) == +0", FUNC(pow) (0, 1), 0);
1239 check ("pow (+0, 11) == +0", FUNC(pow) (0, 11), 0);
1240 check ("pow (-0, 1) == -0", FUNC(pow) (minus_zero, 1), minus_zero);
1241 check ("pow (-0, 11) == -0", FUNC(pow) (minus_zero, 11), minus_zero);
1243 check ("pow (+0, 2) == +0", FUNC(pow) (0, 2), 0);
1244 check ("pow (+0, 11.1) == +0", FUNC(pow) (0, 11.1), 0);
1245 check ("pow (-0, 2) == +0", FUNC(pow) (minus_zero, 2), 0);
1246 check ("pow (-0, 11.1) == +0", FUNC(pow) (minus_zero, 11.1), 0);
1248 x = random_greater (1.0);
1249 check_isinfp_ext ("pow (x, +inf) == +inf for |x| > 1",
1250 FUNC(pow) (x, plus_infty), x);
1252 x = random_value (-1.0, 1.0);
1253 check_ext ("pow (x, +inf) == +0 for |x| < 1",
1254 FUNC(pow) (x, plus_infty), 0.0, x);
1256 x = random_greater (1.0);
1257 check_ext ("pow (x, -inf) == +0 for |x| > 1",
1258 FUNC(pow) (x, minus_infty), 0.0, x);
1260 x = random_value (-1.0, 1.0);
1261 check_isinfp_ext ("pow (x, -inf) == +inf for |x| < 1",
1262 FUNC(pow) (x, minus_infty), x);
1264 x = random_greater (0.0);
1265 check_isinfp_ext ("pow (+inf, y) == +inf for y > 0",
1266 FUNC(pow) (plus_infty, x), x);
1268 x = random_less (0.0);
1269 check_ext ("pow (+inf, y) == +0 for y < 0",
1270 FUNC(pow) (plus_infty, x), 0.0, x);
1272 x = (rand () % 1000000) * 2.0 + 1; /* Get random odd integer > 0 */
1273 check_isinfn_ext ("pow (-inf, y) == -inf for y an odd integer > 0",
1274 FUNC(pow) (minus_infty, x), x);
1276 x = ((rand () % 1000000) + 1) * 2.0; /* Get random even integer > 1 */
1277 check_isinfp_ext ("pow (-inf, y) == +inf for y > 0 and not an odd integer",
1278 FUNC(pow) (minus_infty, x), x);
1280 x = -((rand () % 1000000) * 2.0 + 1); /* Get random odd integer < 0 */
1281 check_ext ("pow (-inf, y) == -0 for y an odd integer < 0",
1282 FUNC(pow) (minus_infty, x), minus_zero, x);
1284 x = ((rand () % 1000000) + 1) * -2.0; /* Get random even integer < 0 */
1285 check_ext ("pow (-inf, y) == +0 for y < 0 and not an odd integer",
1286 FUNC(pow) (minus_infty, x), 0.0, x);
1288 x = (rand () % 1000000) * 2.0 + 1; /* Get random odd integer > 0 */
1289 check_ext ("pow (+0, y) == +0 for y an odd integer > 0",
1290 FUNC(pow) (0.0, x), 0.0, x);
1291 x = (rand () % 1000000) * 2.0 + 1; /* Get random odd integer > 0 */
1292 check_ext ("pow (-0, y) == -0 for y an odd integer > 0",
1293 FUNC(pow) (minus_zero, x), minus_zero, x);
1295 x = ((rand () % 1000000) + 1) * 2.0; /* Get random even integer > 1 */
1296 check_ext ("pow (+0, y) == +0 for y > 0 and not an odd integer",
1297 FUNC(pow) (0.0, x), 0.0, x);
1299 x = ((rand () % 1000000) + 1) * 2.0; /* Get random even integer > 1 */
1300 check_ext ("pow (-0, y) == +0 for y > 0 and not an odd integer",
1301 FUNC(pow) (minus_zero, x), 0.0, x);
1305 static void
1306 fdim_test (void)
1308 check ("fdim (+0, +0) = +0", FUNC(fdim) (0, 0), 0);
1309 check ("fdim (9, 0) = 9", FUNC(fdim) (9, 0), 9);
1310 check ("fdim (0, 9) = 0", FUNC(fdim) (0, 9), 0);
1311 check ("fdim (-9, 0) = 9", FUNC(fdim) (-9, 0), 0);
1312 check ("fdim (0, -9) = 9", FUNC(fdim) (0, -9), 9);
1314 check_isinfp ("fdim (+inf, 9) = +inf", FUNC(fdim) (plus_infty, 9));
1315 check_isinfp ("fdim (+inf, -9) = +inf", FUNC(fdim) (plus_infty, -9));
1316 check ("fdim (-inf, 9) = 0", FUNC(fdim) (minus_infty, 9), 0);
1317 check ("fdim (-inf, -9) = 0", FUNC(fdim) (minus_infty, -9), 0);
1318 check_isinfp ("fdim (+9, -inf) = +inf", FUNC(fdim) (9, minus_infty));
1319 check_isinfp ("fdim (-9, -inf) = +inf", FUNC(fdim) (-9, minus_infty));
1320 check ("fdim (9, inf) = 0", FUNC(fdim) (9, plus_infty), 0);
1321 check ("fdim (-9, inf) = 0", FUNC(fdim) (-9, plus_infty), 0);
1323 check_isnan ("fdim (0, NaN) = NaN", FUNC(fdim) (0, nan_value));
1324 check_isnan ("fdim (9, NaN) = NaN", FUNC(fdim) (9, nan_value));
1325 check_isnan ("fdim (-9, NaN) = NaN", FUNC(fdim) (-9, nan_value));
1326 check_isnan ("fdim (NaN, 9) = NaN", FUNC(fdim) (nan_value, 9));
1327 check_isnan ("fdim (NaN, -9) = NaN", FUNC(fdim) (nan_value, -9));
1328 check_isnan ("fdim (+inf, NaN) = NaN", FUNC(fdim) (plus_infty, nan_value));
1329 check_isnan ("fdim (-inf, NaN) = NaN", FUNC(fdim) (minus_infty, nan_value));
1330 check_isnan ("fdim (NaN, +inf) = NaN", FUNC(fdim) (nan_value, plus_infty));
1331 check_isnan ("fdim (NaN, -inf) = NaN", FUNC(fdim) (nan_value, minus_infty));
1332 check_isnan ("fdim (NaN, NaN) = NaN", FUNC(fdim) (nan_value, nan_value));
1336 static void
1337 fmin_test (void)
1339 check ("fmin (+0, +0) = +0", FUNC(fmin) (0, 0), 0);
1340 check ("fmin (9, 0) = 0", FUNC(fmin) (9, 0), 0);
1341 check ("fmin (0, 9) = 0", FUNC(fmin) (0, 9), 0);
1342 check ("fmin (-9, 0) = -9", FUNC(fmin) (-9, 0), -9);
1343 check ("fmin (0, -9) = -9", FUNC(fmin) (0, -9), -9);
1345 check ("fmin (+inf, 9) = 9", FUNC(fmin) (plus_infty, 9), 9);
1346 check ("fmin (9, +inf) = 9", FUNC(fmin) (9, plus_infty), 9);
1347 check ("fmin (+inf, -9) = -9", FUNC(fmin) (plus_infty, -9), -9);
1348 check ("fmin (-9, +inf) = -9", FUNC(fmin) (-9, plus_infty), -9);
1349 check_isinfn ("fmin (-inf, 9) = -inf", FUNC(fmin) (minus_infty, 9));
1350 check_isinfn ("fmin (-inf, -9) = -inf", FUNC(fmin) (minus_infty, -9));
1351 check_isinfn ("fmin (+9, -inf) = -inf", FUNC(fmin) (9, minus_infty));
1352 check_isinfn ("fmin (-9, -inf) = -inf", FUNC(fmin) (-9, minus_infty));
1354 check ("fmin (0, NaN) = 0", FUNC(fmin) (0, nan_value), 0);
1355 check ("fmin (9, NaN) = 9", FUNC(fmin) (9, nan_value), 9);
1356 check ("fmin (-9, NaN) = 9", FUNC(fmin) (-9, nan_value), -9);
1357 check ("fmin (NaN, 0) = 0", FUNC(fmin) (nan_value, 0), 0);
1358 check ("fmin (NaN, 9) = NaN", FUNC(fmin) (nan_value, 9), 9);
1359 check ("fmin (NaN, -9) = NaN", FUNC(fmin) (nan_value, -9), -9);
1360 check_isinfp ("fmin (+inf, NaN) = +inf", FUNC(fmin) (plus_infty, nan_value));
1361 check_isinfn ("fmin (-inf, NaN) = -inf", FUNC(fmin) (minus_infty, nan_value));
1362 check_isinfp ("fmin (NaN, +inf) = +inf", FUNC(fmin) (nan_value, plus_infty));
1363 check_isinfn ("fmin (NaN, -inf) = -inf", FUNC(fmin) (nan_value, minus_infty));
1364 check_isnan ("fmin (NaN, NaN) = NaN", FUNC(fmin) (nan_value, nan_value));
1368 static void
1369 fmax_test (void)
1371 check ("fmax (+0, +0) = +0", FUNC(fmax) (0, 0), 0);
1372 check ("fmax (9, 0) = 9", FUNC(fmax) (9, 0), 9);
1373 check ("fmax (0, 9) = 9", FUNC(fmax) (0, 9), 9);
1374 check ("fmax (-9, 0) = 0", FUNC(fmax) (-9, 0), 0);
1375 check ("fmax (0, -9) = 0", FUNC(fmax) (0, -9), 0);
1377 check_isinfp ("fmax (+inf, 9) = +inf", FUNC(fmax) (plus_infty, 9));
1378 check_isinfp ("fmax (9, +inf) = +inf", FUNC(fmax) (0, plus_infty));
1379 check_isinfp ("fmax (-9, +inf) = +inf", FUNC(fmax) (-9, plus_infty));
1380 check_isinfp ("fmax (+inf, -9) = +inf", FUNC(fmax) (plus_infty, -9));
1381 check ("fmax (-inf, 9) = 9", FUNC(fmax) (minus_infty, 9), 9);
1382 check ("fmax (-inf, -9) = -9", FUNC(fmax) (minus_infty, -9), -9);
1383 check ("fmax (+9, -inf) = 9", FUNC(fmax) (9, minus_infty), 9);
1384 check ("fmax (-9, -inf) = -9", FUNC(fmax) (-9, minus_infty), -9);
1386 check ("fmax (0, NaN) = 0", FUNC(fmax) (0, nan_value), 0);
1387 check ("fmax (9, NaN) = 9", FUNC(fmax) (9, nan_value), 9);
1388 check ("fmax (-9, NaN) = 9", FUNC(fmax) (-9, nan_value), -9);
1389 check ("fmax (NaN, 0) = 0", FUNC(fmax) (nan_value, 0), 0);
1390 check ("fmax (NaN, 9) = NaN", FUNC(fmax) (nan_value, 9), 9);
1391 check ("fmax (NaN, -9) = NaN", FUNC(fmax) (nan_value, -9), -9);
1392 check_isinfp ("fmax (+inf, NaN) = +inf", FUNC(fmax) (plus_infty, nan_value));
1393 check_isinfn ("fmax (-inf, NaN) = -inf", FUNC(fmax) (minus_infty, nan_value));
1394 check_isinfp ("fmax (NaN, +inf) = +inf", FUNC(fmax) (nan_value, plus_infty));
1395 check_isinfn ("fmax (NaN, -inf) = -inf", FUNC(fmax) (nan_value, minus_infty));
1396 check_isnan ("fmax (NaN, NaN) = NaN", FUNC(fmax) (nan_value, nan_value));
1400 static void
1401 nextafter_test (void)
1403 MATHTYPE x;
1405 check ("nextafter (+0, +0) = +0", FUNC(nextafter) (0, 0), 0);
1406 check ("nextafter (-0, +0) = +0", FUNC(nextafter) (minus_zero, 0), 0);
1407 check ("nextafter (+0, -0) = -0", FUNC(nextafter) (0, minus_zero),
1408 minus_zero);
1409 check ("nextafter (-0, -0) = -0", FUNC(nextafter) (minus_zero, minus_zero),
1410 minus_zero);
1412 check ("nextafter (9, 9) = 9", FUNC(nextafter) (9, 9), 9);
1413 check ("nextafter (-9, -9) = -9", FUNC(nextafter) (-9, -9), -9);
1414 check_isinfp ("nextafter (+inf, +inf) = +inf",
1415 FUNC(nextafter) (plus_infty, plus_infty));
1416 check_isinfn ("nextafter (-inf, -inf) = -inf",
1417 FUNC(nextafter) (minus_infty, minus_infty));
1419 x = rand () * 1.1;
1420 check_isnan ("nextafter (NaN, x) = NaN", FUNC(nextafter) (nan_value, x));
1421 check_isnan ("nextafter (x, NaN) = NaN", FUNC(nextafter) (x, nan_value));
1422 check_isnan ("nextafter (NaN, NaN) = NaN", FUNC(nextafter) (nan_value,
1423 nan_value));
1425 /* XXX We need the hexadecimal FP number representation here for further
1426 tests. */
1430 static void
1431 copysign_test (void)
1433 check ("copysign (0, 4) = 0", FUNC(copysign) (0, 4), 0);
1434 check ("copysign (0, -4) = -0", FUNC(copysign) (0, -4), minus_zero);
1435 check ("copysign (-0, 4) = 0", FUNC(copysign) (minus_zero, 4), 0);
1436 check ("copysign (-0, -4) = -0", FUNC(copysign) (minus_zero, -4),
1437 minus_zero);
1439 check_isinfp ("copysign (+inf, 0) = +inf", FUNC(copysign) (plus_infty, 0));
1440 check_isinfn ("copysign (+inf, -0) = -inf", FUNC(copysign) (plus_infty,
1441 minus_zero));
1442 check_isinfp ("copysign (-inf, 0) = +inf", FUNC(copysign) (minus_infty, 0));
1443 check_isinfn ("copysign (-inf, -0) = -inf", FUNC(copysign) (minus_infty,
1444 minus_zero));
1446 check ("copysign (0, +inf) = 0", FUNC(copysign) (0, plus_infty), 0);
1447 check ("copysign (0, -inf) = -0", FUNC(copysign) (0, minus_zero),
1448 minus_zero);
1449 check ("copysign (-0, +inf) = 0", FUNC(copysign) (minus_zero, plus_infty),
1451 check ("copysign (-0, -inf) = -0", FUNC(copysign) (minus_zero, minus_zero),
1452 minus_zero);
1454 /* XXX More correctly we would have to check the sign of the NaN. */
1455 check_isnan ("copysign (+NaN, 0) = +NaN", FUNC(copysign) (nan_value, 0));
1456 check_isnan ("copysign (+NaN, -0) = -NaN", FUNC(copysign) (nan_value,
1457 minus_zero));
1458 check_isnan ("copysign (-NaN, 0) = +NaN", FUNC(copysign) (-nan_value, 0));
1459 check_isnan ("copysign (-NaN, -0) = -NaN", FUNC(copysign) (-nan_value,
1460 minus_zero));
1464 static void
1465 trunc_test (void)
1467 check ("trunc(0) = 0", FUNC(trunc) (0), 0);
1468 check ("trunc(-0) = -0", FUNC(trunc) (minus_zero), minus_zero);
1469 check ("trunc(0.625) = 0", FUNC(trunc) (0.625), 0);
1470 check ("trunc(-0.625) = -0", FUNC(trunc) (-0.625), minus_zero);
1471 check ("trunc(1) = 1", FUNC(trunc) (1), 1);
1472 check ("trunc(-1) = -1", FUNC(trunc) (-1), -1);
1473 check ("trunc(1.625) = 1", FUNC(trunc) (1.625), 1);
1474 check ("trunc(-1.625) = -1", FUNC(trunc) (-1.625), -1);
1476 check ("trunc(1048580.625) = 1048580", FUNC(trunc) (1048580.625L),
1477 1048580L);
1478 check ("trunc(-1048580.625) = -1048580", FUNC(trunc) (-1048580.625L),
1479 -1048580L);
1481 check ("trunc(8388610.125) = 8388610", FUNC(trunc) (8388610.125L),
1482 8388610.0L);
1483 check ("trunc(-8388610.125) = -8388610", FUNC(trunc) (-8388610.125L),
1484 -8388610.0L);
1486 check ("trunc(4294967296.625) = 4294967296", FUNC(trunc) (4294967296.625L),
1487 4294967296.0L);
1488 check ("trunc(-4294967296.625) = -4294967296",
1489 FUNC(trunc) (-4294967296.625L), -4294967296.0L);
1491 check_isinfp ("trunc(+inf) = +inf", FUNC(trunc) (plus_infty));
1492 check_isinfn ("trunc(-inf) = -inf", FUNC(trunc) (minus_infty));
1493 check_isnan ("trunc(NaN) = NaN", FUNC(trunc) (nan_value));
1497 static void
1498 sqrt_test (void)
1500 MATHTYPE x;
1503 /* XXX Tests fuer negative x are missing */
1504 check ("sqrt (0) == 0", FUNC(sqrt) (0), 0);
1505 check_isnan ("sqrt (NaN) == NaN", FUNC(sqrt) (nan_value));
1506 check_isinfp ("sqrt (+inf) == +inf", FUNC(sqrt) (plus_infty));
1508 x = random_value (0, 10000);
1509 check_ext ("sqrt (x*x) == x", sqrt (x*x), x, x);
1510 check ("sqrt (4) == 2", FUNC(sqrt) (4), 2);
1514 static void
1515 remquo_test (void)
1517 int quo;
1518 MATHTYPE result;
1520 result = FUNC(remquo) (1.625, 1.0, &quo);
1521 check ("remquo(1.625, 1.0, &x) == -0.375", result, -0.375);
1522 check ("remquo(1.625, 1.0, &x) puts 1 in x", quo, 1);
1524 result = FUNC(remquo) (-1.625, 1.0, &quo);
1525 check ("remquo(-1.625, 1.0, &x) == 0.375", result, 0.375);
1526 check ("remquo(-1.625, 1.0, &x) puts -1 in x", quo, -1);
1528 result = FUNC(remquo) (1.625, -1.0, &quo);
1529 check ("remquo(1.125, -1.0, &x) == 0.125", result, 0.125);
1530 check ("remquo(1.125, -1.0, &x) puts -1 in x", quo, -1);
1532 result = FUNC(remquo) (-1.625, -1.0, &quo);
1533 check ("remquo(-1.125, -1.0, &x) == 0.125", result, 0.125);
1534 check ("remquo(-1.125, -1.0, &x) puts 1 in x", quo, 1);
1538 static void
1539 cexp_test (void)
1541 __complex__ MATHTYPE result;
1543 result = FUNC(cexp) (plus_zero + 1.0i * plus_zero);
1544 check ("real(cexp(0 + 0i)) = 1", __real__ result, 1);
1545 check ("imag(cexp(0 + 0i)) = 0", __imag__ result, 0);
1546 result = FUNC(cexp) (minus_zero + 1.0i * plus_zero);
1547 check ("real(cexp(-0 + 0i)) = 1", __real__ result, 1);
1548 check ("imag(cexp(-0 + 0i)) = 0", __imag__ result, 0);
1549 result = FUNC(cexp) (plus_zero + 1.0i * minus_zero);
1550 check ("real(cexp(0 - 0i)) = 1", __real__ result, 1);
1551 check ("imag(cexp(0 - 0i)) = 0", __imag__ result, 0);
1552 result = FUNC(cexp) (minus_zero + 1.0i * minus_zero);
1553 check ("real(cexp(-0 - 0i)) = 1", __real__ result, 1);
1554 check ("imag(cexp(-0 - 0i)) = 0", __imag__ result, 0);
1556 result = FUNC(cexp) (plus_infty + 1.0i * plus_zero);
1557 check_isinfp ("real(cexp(+inf + 0i)) = +inf", __real__ result);
1558 check ("imag(cexp(+inf + 0i)) = 0", __imag__ result, 0);
1559 result = FUNC(cexp) (plus_infty + 1.0i * minus_zero);
1560 check_isinfp ("real(cexp(+inf - 0i)) = +inf", __real__ result);
1561 check ("imag(cexp(+inf - 0i)) = 0", __imag__ result, 0);
1563 result = FUNC(cexp) (minus_infty + 1.0i * plus_zero);
1564 check ("real(cexp(-inf + 0i)) = 0", __real__ result, 0);
1565 check ("imag(cexp(-inf + 0i)) = 0", __imag__ result, 0);
1566 result = FUNC(cexp) (minus_infty + 1.0i * minus_zero);
1567 check ("real(cexp(-inf - 0i)) = 0", __real__ result, 0);
1568 check ("imag(cexp(-inf - 0i)) = 0", __imag__ result, 0);
1572 static void
1573 inverse_func_pair_test (const char *test_name,
1574 mathfunc f1, mathfunc inverse,
1575 MATHTYPE x, MATHTYPE epsilon)
1577 MATHTYPE a, b, difference;
1578 int result;
1580 a = f1 (x);
1581 this_does_nothing ();
1582 b = inverse (a);
1583 this_does_nothing ();
1585 result = check_equal (b, x, epsilon, &difference);
1586 output_result (test_name, result,
1587 b, x, difference, PRINT, PRINT);
1591 static void
1592 inverse_functions (void)
1594 inverse_func_pair_test ("asin(sin(x)) == x",
1595 FUNC(sin), FUNC(asin), 1.0, CHOOSE (2e-18L, 0, 1e-7L));
1596 inverse_func_pair_test ("sin(asin(x)) == x",
1597 FUNC(asin), FUNC(sin), 1.0, 0.0);
1599 inverse_func_pair_test ("acos(cos(x)) == x",
1600 FUNC(cos), FUNC(acos), 1.0, CHOOSE (4e-18L, 1e-15L, 0));
1601 inverse_func_pair_test ("cos(acos(x)) == x",
1602 FUNC(acos), FUNC(cos), 1.0, 0.0);
1603 inverse_func_pair_test ("atan(tan(x)) == x",
1604 FUNC(tan), FUNC(atan), 1.0, CHOOSE (2e-18L, 0, 0));
1605 inverse_func_pair_test ("tan(atan(x)) == x",
1606 FUNC(atan), FUNC(tan), 1.0, CHOOSE (2e-18L, 1e-15L, 0));
1608 inverse_func_pair_test ("asinh(sinh(x)) == x",
1609 FUNC(sinh), FUNC(asinh), 1.0, CHOOSE (1e-18L, 0, 1e-7));
1610 inverse_func_pair_test ("sinh(asinh(x)) == x",
1611 FUNC(asinh), FUNC(sinh), 1.0, CHOOSE (2e-18L, 0, 0));
1613 inverse_func_pair_test ("acosh(cosh(x)) == x",
1614 FUNC(cosh), FUNC(acosh), 1.0, CHOOSE (1e-18L, 1e-15L, 0));
1615 inverse_func_pair_test ("cosh(acosh(x)) == x",
1616 FUNC(acosh), FUNC(cosh), 1.0, 0.0);
1618 inverse_func_pair_test ("atanh(tanh(x)) == x",
1619 FUNC(tanh), FUNC(atanh), 1.0, CHOOSE (1e-18L, 1e-15L, 0));
1620 inverse_func_pair_test ("tanh(atanh(x)) == x",
1621 FUNC(atanh), FUNC(tanh), 1.0, 0.0);
1625 /* Test sin and cos with the identity: sin(x)^2 + cos(x)^2 = 1. */
1626 static void
1627 identities1_test (MATHTYPE x, MATHTYPE epsilon)
1629 MATHTYPE res1, res2, res3, diff;
1630 int result;
1632 res1 = FUNC(sin) (x);
1633 this_does_nothing ();
1634 res2 = FUNC(cos) (x);
1635 this_does_nothing ();
1636 res3 = res1 * res1 + res2 * res2;
1637 this_does_nothing ();
1639 result = check_equal (res3, 1.0, epsilon, &diff);
1640 output_result_ext ("sin^2 + cos^2 == 1", result,
1641 res3, 1.0, diff, x, PRINT, PRINT);
1645 /* Test sin, cos, tan with the following relation: tan = sin/cos. */
1646 static void
1647 identities2_test (MATHTYPE x, MATHTYPE epsilon)
1649 MATHTYPE res1, res2, res3, res4, diff;
1650 int result;
1652 res1 = FUNC(sin) (x);
1653 this_does_nothing ();
1654 res2 = FUNC(cos) (x);
1655 this_does_nothing ();
1656 res3 = FUNC(tan) (x);
1657 this_does_nothing ();
1658 res4 = res1 / res2;
1659 this_does_nothing ();
1661 result = check_equal (res4, res3, epsilon, &diff);
1662 output_result_ext ("sin/cos == tan", result,
1663 res4, res3, diff, x, PRINT, PRINT);
1667 /* Test cosh and sinh with the identity cosh^2 - sinh^2 = 1. */
1668 static void
1669 identities3_test (MATHTYPE x, MATHTYPE epsilon)
1671 MATHTYPE res1, res2, res3, diff;
1672 int result;
1674 res1 = FUNC(sinh) (x);
1675 this_does_nothing ();
1676 res2 = FUNC(cosh) (x);
1677 this_does_nothing ();
1678 res3 = res2 * res2 - res1 * res1;
1679 this_does_nothing ();
1681 result = check_equal (res3, 1.0, epsilon, &diff);
1682 output_result_ext ("cosh^2 - sinh^2 == 1", result,
1683 res3, 1.0, diff, x, PRINT, PRINT);
1687 static void
1688 identities (void)
1690 identities1_test (0.2L, CHOOSE (1e-18L, 0, 2e-7));
1691 identities1_test (0.9L, CHOOSE (1e-18L, 0, 0));
1692 identities1_test (0, 0);
1693 identities1_test (-1, CHOOSE (1e-18L, 0, 1e-7));
1695 identities2_test (0.2L, CHOOSE (0, 1e-16, 0));
1696 identities2_test (0.9L, CHOOSE (0, 1e-15, 0));
1697 identities2_test (0, 0);
1698 identities2_test (-1, CHOOSE (1e-18L, 1e-15, 0));
1700 identities3_test (0.2L, CHOOSE (1e-18L, 0, 1e-7));
1701 identities3_test (0.9L, CHOOSE (1e-18L, 1e-15, 1e-6));
1702 identities3_test (0, CHOOSE (0, 0, 1e-6));
1703 identities3_test (-1, CHOOSE (1e-18L, 0, 1e-6));
1708 Let's test that basic arithmetic is working
1709 tests: Infinity and NaN
1711 static void
1712 basic_tests (void)
1714 /* variables are declared volatile to forbid some compiler
1715 optimizations */
1716 volatile MATHTYPE Inf_var, NaN_var, zero_var, one_var;
1717 MATHTYPE x1, x2;
1719 zero_var = 0.0;
1720 one_var = 1.0;
1721 NaN_var = nan_value;
1722 Inf_var = one_var / zero_var;
1724 this_does_nothing ();
1726 check_isinfp ("isinf (inf) == +1", Inf_var);
1727 check_isinfn ("isinf (-inf) == -1", -Inf_var);
1728 check_bool ("!isinf (1)", !(FUNC(isinf) (one_var)));
1729 check_bool ("!isinf (NaN)", !(FUNC(isinf) (NaN_var)));
1731 check_isnan ("isnan (NaN)", NaN_var);
1732 check_isnan ("isnan (-NaN)", -NaN_var);
1733 check_bool ("!isnan (1)", !(FUNC(isnan) (one_var)));
1734 check_bool ("!isnan (inf)", !(FUNC(isnan) (Inf_var)));
1736 check_bool ("inf == inf", Inf_var == Inf_var);
1737 check_bool ("-inf == -inf", -Inf_var == -Inf_var);
1738 check_bool ("inf != -inf", Inf_var != -Inf_var);
1739 check_bool ("NaN != NaN", NaN_var != NaN_var);
1742 the same tests but this time with NAN from <nan.h>
1743 NAN is a double const
1745 check_bool ("isnan (NAN)", isnan (NAN));
1746 check_bool ("isnan (-NAN)", isnan (-NAN));
1747 check_bool ("!isinf (NAN)", !(isinf (NAN)));
1748 check_bool ("!isinf (-NAN)", !(isinf (-NAN)));
1749 check_bool ("NAN != NAN", NAN != NAN);
1752 And again with the value returned by the `nan' function.
1754 check_bool ("isnan (NAN)", isnan (FUNC(nan) ("")));
1755 check_bool ("isnan (-NAN)", isnan (-FUNC(nan) ("")));
1756 check_bool ("!isinf (NAN)", !(isinf (FUNC(nan) (""))));
1757 check_bool ("!isinf (-NAN)", !(isinf (-FUNC(nan) (""))));
1758 check_bool ("NAN != NAN", FUNC(nan) ("") != FUNC(nan) (""));
1760 /* test if EPSILON is ok */
1761 x1 = MATHCONST (1.0);
1762 x2 = x1 + CHOOSE (LDBL_EPSILON, DBL_EPSILON, FLT_EPSILON);
1763 check_bool ("1 != 1+EPSILON", x1 != x2);
1765 x1 = MATHCONST (1.0);
1766 x2 = x1 - CHOOSE (LDBL_EPSILON, DBL_EPSILON, FLT_EPSILON);
1767 check_bool ("1 != 1-EPSILON", x1 != x2);
1769 /* test if HUGE_VALx is ok */
1770 x1 = CHOOSE (HUGE_VALL, HUGE_VAL, HUGE_VALF);
1771 check_bool ("isinf (HUGE_VALx) == +1", ISINF (x1) == +1);
1772 x1 = -CHOOSE (HUGE_VALL, HUGE_VAL, HUGE_VALF);
1773 check_bool ("isinf (-HUGE_VALx) == -1", ISINF (x1) == -1);
1778 static void
1779 initialize (void)
1781 fpstack_test ("*init*");
1782 plus_zero = 0.0;
1783 nan_value = plus_zero / plus_zero; /* Suppress GCC warning */
1785 minus_zero = FUNC (copysign) (0.0, -1.0);
1786 plus_infty = CHOOSE (HUGE_VALL, HUGE_VAL, HUGE_VALF);
1787 minus_infty = -CHOOSE (HUGE_VALL, HUGE_VAL, HUGE_VALF);
1789 /* Test to make sure we start correctly. */
1790 fpstack_test ("*init*");
1794 static struct option long_options[] =
1796 {"verbose", optional_argument, NULL, 'v'},
1797 {"silent", no_argument, NULL, 's'},
1798 {0, 0, 0, 0}
1802 static void
1803 parse_options (int argc, char *argv[])
1805 int c;
1806 int option_index;
1808 verbose = 1;
1810 while (1)
1812 c = getopt_long (argc, argv, "v::s",
1813 long_options, &option_index);
1815 /* Detect the end of the options. */
1816 if (c == -1)
1817 break;
1819 switch (c)
1821 case 'v':
1822 if (optarg)
1823 verbose = (unsigned int) strtoul (optarg, NULL, 0);
1824 else
1825 verbose = 3;
1826 break;
1827 case 's':
1828 verbose = 0;
1829 default:
1830 break;
1837 main (int argc, char *argv[])
1839 parse_options (argc, argv);
1841 initialize ();
1842 printf (TEST_MSG);
1843 basic_tests ();
1845 acos_test ();
1846 acosh_test ();
1847 asin_test ();
1848 asinh_test ();
1849 atan_test ();
1850 atanh_test ();
1851 atan2_test ();
1852 cbrt_test ();
1853 ceil_test ();
1854 cos_test ();
1855 cosh_test ();
1856 exp_test ();
1857 #ifdef ISO_9X_IMPLEMENTED
1858 exp2_test ();
1859 #endif
1860 expm1_test ();
1861 frexp_test ();
1862 ilogb_test ();
1863 ldexp_test ();
1864 log_test ();
1865 log10_test ();
1866 log1p_test ();
1867 #ifdef ISO_9X_IMPLEMENTED
1868 log2_test ();
1869 #endif
1870 logb_test ();
1871 modf_test ();
1872 scalb_test ();
1873 scalbn_test ();
1874 sin_test ();
1875 sinh_test ();
1876 tan_test ();
1877 tanh_test ();
1878 fabs_test ();
1879 floor_test ();
1880 fpclassify_test ();
1881 hypot_test ();
1882 pow_test ();
1883 fdim_test ();
1884 fmin_test ();
1885 fmax_test ();
1886 nextafter_test ();
1887 copysign_test ();
1888 sqrt_test ();
1889 trunc_test ();
1890 #if 0
1891 /* XXX I'm not sure what is the correct result. */
1892 remquo_test ();
1893 #endif
1894 cexp_test ();
1896 identities ();
1897 inverse_functions ();
1899 if (noErrors)
1901 printf ("\n%d errors occured.\n", noErrors);
1902 exit (1);
1904 printf ("\n All tests passed sucessfully.\n");
1905 exit (0);