1 /* Test file for mpfr_cosh.
2
3 Copyright 2001-2002, 2004-2020 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include "mpfr-test.h"
24
25 #define TEST_FUNCTION mpfr_cosh
26 #define TEST_RANDOM_EMIN -36
27 #define TEST_RANDOM_EMAX 36
28 #include "tgeneric.c"
29
30 static void
special(void)31 special (void)
32 {
33 mpfr_t x, y;
34 int i;
35
36 mpfr_init (x);
37 mpfr_init (y);
38
39 mpfr_set_nan (x);
40 mpfr_cosh (y, x, MPFR_RNDN);
41 if (!mpfr_nan_p (y))
42 {
43 printf ("Error: cosh(NaN) != NaN\n");
44 exit (1);
45 }
46
47 mpfr_set_inf (x, 1);
48 mpfr_cosh (y, x, MPFR_RNDN);
49 if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
50 {
51 printf ("Error: cosh(+Inf) != +Inf\n");
52 exit (1);
53 }
54
55 mpfr_set_inf (x, -1);
56 mpfr_cosh (y, x, MPFR_RNDN);
57 if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
58 {
59 printf ("Error: cosh(-Inf) != +Inf\n");
60 exit (1);
61 }
62
63 /* cosh(+/-0) = 1 */
64 mpfr_set_ui (x, 0, MPFR_RNDN);
65 mpfr_cosh (y, x, MPFR_RNDN);
66 if (mpfr_cmp_ui (y, 1))
67 {
68 printf ("Error: cosh(+0) != 1\n");
69 exit (1);
70 }
71 mpfr_neg (x, x, MPFR_RNDN);
72 mpfr_cosh (y, x, MPFR_RNDN);
73 if (mpfr_cmp_ui (y, 1))
74 {
75 printf ("Error: cosh(-0) != 1\n");
76 exit (1);
77 }
78
79 mpfr_set_prec (x, 32);
80 mpfr_set_prec (y, 32);
81
82 mpfr_set_str_binary (x, "0.1101110111111111001011101000101");
83 mpfr_set_str_binary (y, "1.0110011001110000101100011001001");
84 mpfr_cosh (x, x, MPFR_RNDN);
85 if (mpfr_cmp (x, y))
86 {
87 printf ("Error: mpfr_cosh for prec=32 (1)\n");
88 exit (1);
89 }
90
91 mpfr_set_str_binary (x, "-0.1110111000011101010111100000101E-1");
92 mpfr_set_str_binary (y, "1.0001110000101111111111100110101");
93 mpfr_cosh (x, x, MPFR_RNDN);
94 if (mpfr_cmp (x, y))
95 {
96 printf ("Error: mpfr_cosh for prec=32 (2)\n");
97 exit (1);
98 }
99
100 mpfr_set_prec (x, 2);
101 mpfr_clear_flags ();
102 mpfr_set_str_binary (x, "1E1000000000");
103 i = mpfr_cosh (x, x, MPFR_RNDN);
104 MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
105 MPFR_ASSERTN (mpfr_overflow_p ());
106 MPFR_ASSERTN (i == 1);
107
108 mpfr_clear_flags ();
109 mpfr_set_str_binary (x, "-1E1000000000");
110 i = mpfr_cosh (x, x, MPFR_RNDN);
111 MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
112 MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ());
113 MPFR_ASSERTN (i == 1);
114
115 mpfr_clear_flags ();
116 mpfr_set_str_binary (x, "-1E1000000000");
117 i = mpfr_cosh (x, x, MPFR_RNDD);
118 MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_IS_POS (x));
119 MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ());
120 MPFR_ASSERTN (i == -1);
121
122 mpfr_clear_flags ();
123 mpfr_set_str_binary (x, "-1E1000000000");
124 i = mpfr_cosh (x, x, MPFR_RNDU);
125 MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
126 MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ());
127 MPFR_ASSERTN (i == 1);
128
129 mpfr_clear (x);
130 mpfr_clear (y);
131 }
132
133 static void
special_overflow(void)134 special_overflow (void)
135 {
136 /* Check for overflow in 3 cases:
137 1. cosh(x) is representable, but not exp(x)
138 2. cosh(x) is not representable in the selected range of exp.
139 3. cosh(x) exp overflow even with the largest range of exp */
140 mpfr_t x, y;
141 mpfr_exp_t emin, emax;
142
143 emin = mpfr_get_emin ();
144 emax = mpfr_get_emax ();
145
146 set_emin (-125);
147 set_emax (128);
148
149 mpfr_init2 (x, 24);
150 mpfr_init2 (y, 24);
151
152 mpfr_set_str_binary (x, "0.101100100000000000110100E7");
153 mpfr_cosh (y, x, MPFR_RNDN);
154 if (mpfr_cmp_str (y, "0.101010001111001010001110E128", 2, MPFR_RNDN))
155 {
156 printf("Special overflow error 1.\n");
157 mpfr_dump (y);
158 exit (1);
159 }
160
161 mpfr_set_str_binary (x, "0.101100100000000000110100E8");
162 mpfr_cosh (y, x, MPFR_RNDN);
163 if (!mpfr_inf_p(y))
164 {
165 printf("Special overflow error 2.\n");
166 mpfr_dump (y);
167 exit (1);
168 }
169
170 set_emin (emin);
171 set_emax (emax);
172
173 mpfr_set_str_binary (x, "0.101100100000000000110100E1000000");
174 mpfr_cosh (y, x, MPFR_RNDN);
175 if (!mpfr_inf_p(y))
176 {
177 printf("Special overflow error 3.\n");
178 mpfr_dump (y);
179 exit (1);
180 }
181
182 mpfr_clear (y);
183 mpfr_clear (x);
184 }
185
186 int
main(int argc,char * argv[])187 main (int argc, char *argv[])
188 {
189 tests_start_mpfr ();
190
191 special_overflow ();
192 special ();
193
194 test_generic (MPFR_PREC_MIN, 100, 100);
195
196 data_check ("data/cosh", mpfr_cosh, "mpfr_cosh");
197 bad_cases (mpfr_cosh, mpfr_acosh, "mpfr_cosh", 0, 1, 255, 4, 128, 800, 100);
198
199 tests_end_mpfr ();
200 return 0;
201 }
202