1/* Test file for mpfr_coth.
2
3Copyright 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_coth
29#include "tgeneric.c"
30
31static void
32check_specials (void)
33{
34  mpfr_t  x, y;
35
36  mpfr_init2 (x, 123L);
37  mpfr_init2 (y, 123L);
38
39  mpfr_set_nan (x);
40  mpfr_coth (y, x, MPFR_RNDN);
41  if (! mpfr_nan_p (y))
42    {
43      printf ("Error: coth(NaN) != NaN\n");
44      exit (1);
45    }
46
47  mpfr_set_inf (x, 1);
48  mpfr_coth (y, x, MPFR_RNDN);
49  if (mpfr_cmp_ui (y, 1))
50    {
51      printf ("Error: coth(Inf) != 1\n");
52      exit (1);
53    }
54
55  mpfr_set_inf (x, -1);
56  mpfr_coth (y, x, MPFR_RNDN);
57  if (mpfr_cmp_si (y, -1))
58    {
59      printf ("Error: coth(-Inf) != -1\n");
60      exit (1);
61    }
62
63  /* coth(+/-0) = +/-Inf */
64  mpfr_set_ui (x, 0, MPFR_RNDN);
65  mpfr_coth (y, x, MPFR_RNDN);
66  if (! (mpfr_inf_p (y) && MPFR_SIGN (y) > 0))
67    {
68      printf ("Error: coth(+0) != +Inf\n");
69      exit (1);
70    }
71  mpfr_neg (x, x, MPFR_RNDN);
72  mpfr_coth (y, x, MPFR_RNDN);
73  if (! (mpfr_inf_p (y) && MPFR_SIGN (y) < 0))
74    {
75      printf ("Error: coth(-0) != -Inf\n");
76      exit (1);
77    }
78
79  mpfr_clear (x);
80  mpfr_clear (y);
81}
82
83static void
84check_bugs (void)
85{
86  mpfr_t x, y;
87
88  mpfr_init (x);
89  mpfr_init (y);
90
91  /* bug found by Rob (Sisyphus) on 16 Sep 2005 */
92  mpfr_set_ui (x, 2, MPFR_RNDN);
93  mpfr_set_prec (y, 2);
94  mpfr_coth (y, x, MPFR_RNDN);
95  if (mpfr_cmp_ui (y, 1))
96    {
97      printf ("Error for coth(2), expected 1, got ");
98      mpfr_dump (y);
99      exit (1);
100    }
101
102  mpfr_set_prec (x, 53);
103  mpfr_set_prec (y, 53);
104
105  mpfr_set_str (x, "18.368400284838550", 10, MPFR_RNDN);
106  mpfr_set_str (y, "1.0000000000000002", 10, MPFR_RNDN);
107  mpfr_coth (x, x, MPFR_RNDN);
108  if (mpfr_cmp (x, y) != 0)
109    {
110      printf ("Error for coth(18.368400284838550)\n");
111      exit (1);
112    }
113
114  mpfr_set_str (x, "18.714973875118520", 10, MPFR_RNDN);
115  mpfr_coth (x, x, MPFR_RNDN);
116  if (mpfr_cmp (x, y) != 0)
117    {
118      printf ("Error for coth(18.714973875118520)\n");
119      exit (1);
120    }
121
122  mpfr_set_str (x, "18.714973875118524", 10, MPFR_RNDN);
123  mpfr_coth (x, x, MPFR_RNDN);
124  if (mpfr_cmp_ui (x, 1) != 0)
125    {
126      printf ("Error for coth(18.714973875118524)\n");
127      exit (1);
128    }
129
130  mpfr_clear (x);
131  mpfr_clear (y);
132}
133
134static void
135underflowed_cothinf (void)
136{
137  mpfr_t x, y;
138  int i, inex, rnd, err = 0;
139  mpfr_exp_t old_emin;
140
141  old_emin = mpfr_get_emin ();
142
143  mpfr_init2 (x, 8);
144  mpfr_init2 (y, 8);
145
146  for (i = -1; i <= 1; i += 2)
147    RND_LOOP (rnd)
148      {
149        mpfr_set_inf (x, i);
150        mpfr_clear_flags ();
151        set_emin (2);  /* 1 is not representable. */
152        inex = mpfr_coth (x, x, (mpfr_rnd_t) rnd);
153        set_emin (old_emin);
154        if (! mpfr_underflow_p ())
155          {
156            printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n"
157                    "  The underflow flag is not set.\n",
158                    i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
159            err = 1;
160          }
161        mpfr_set_si (y, (i < 0 && (rnd == MPFR_RNDD || rnd == MPFR_RNDA)) ||
162                        (i > 0 && (rnd == MPFR_RNDU || rnd == MPFR_RNDA))
163                     ? 2 : 0, MPFR_RNDN);
164        if (i < 0)
165          mpfr_neg (y, y, MPFR_RNDN);
166        if (! (mpfr_equal_p (x, y) &&
167               MPFR_MULT_SIGN (MPFR_SIGN (x), MPFR_SIGN (y)) > 0))
168          {
169            printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n"
170                    "  Got ", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
171            mpfr_print_binary (x);
172            printf (" instead of ");
173            mpfr_print_binary (y);
174            printf (".\n");
175            err = 1;
176          }
177        if ((rnd == MPFR_RNDD ||
178             (i > 0 && (rnd == MPFR_RNDN || rnd == MPFR_RNDZ))) && inex >= 0)
179          {
180            printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n"
181                    "  The inexact value must be negative.\n",
182                    i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
183            err = 1;
184          }
185        if ((rnd == MPFR_RNDU ||
186             (i < 0 && (rnd == MPFR_RNDN || rnd == MPFR_RNDZ))) && inex <= 0)
187          {
188            printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n"
189                    "  The inexact value must be positive.\n",
190                    i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
191            err = 1;
192          }
193      }
194
195  if (err)
196    exit (1);
197  mpfr_clear (x);
198  mpfr_clear (y);
199}
200
201int
202main (int argc, char *argv[])
203{
204  tests_start_mpfr ();
205
206  check_specials ();
207  check_bugs ();
208  test_generic (2, 200, 10);
209  underflowed_cothinf ();
210
211  tests_end_mpfr ();
212  return 0;
213}
214