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