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