xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tsech.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Test file for mpfr_sech.
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_sech
26 #define TEST_RANDOM_EMIN -64
27 #define TEST_RANDOM_EMAX 36
28 #include "tgeneric.c"
29 
30 static void
check_specials(void)31 check_specials (void)
32 {
33   mpfr_t  x, y;
34 
35   mpfr_init2 (x, 123L);
36   mpfr_init2 (y, 123L);
37 
38   mpfr_set_nan (x);
39   mpfr_sech (y, x, MPFR_RNDN);
40   if (! mpfr_nan_p (y))
41     {
42       printf ("Error: sech(NaN) != NaN\n");
43       exit (1);
44     }
45 
46   mpfr_set_inf (x, 1);
47   mpfr_sech (y, x, MPFR_RNDN);
48   if (! (MPFR_IS_ZERO (y) && MPFR_IS_POS (y)))
49     {
50       printf ("Error: sech(+Inf) != +0\n");
51       exit (1);
52     }
53 
54   mpfr_set_inf (x, -1);
55   mpfr_sech (y, x, MPFR_RNDN);
56   if (! (MPFR_IS_ZERO (y) && MPFR_IS_POS (y)))
57     {
58       printf ("Error: sech(-Inf) != +0\n");
59       exit (1);
60     }
61 
62   /* sec(+/-0) = 1 */
63   mpfr_set_ui (x, 0, MPFR_RNDN);
64   mpfr_sech (y, x, MPFR_RNDN);
65   if (mpfr_cmp_ui (y, 1))
66     {
67       printf ("Error: sech(+0) != 1\n");
68       exit (1);
69     }
70   mpfr_neg (x, x, MPFR_RNDN);
71   mpfr_sech (y, x, MPFR_RNDN);
72   if (mpfr_cmp_ui (y, 1))
73     {
74       printf ("Error: sech(-0) != 1\n");
75       exit (1);
76     }
77 
78   /* check huge x */
79   mpfr_set_str (x, "8e8", 10, MPFR_RNDN);
80   mpfr_sech (y, x, MPFR_RNDN);
81   if (! (mpfr_zero_p (y) && MPFR_IS_POS (y)))
82     {
83       printf ("Error: sech(8e8) != +0\n");
84       exit (1);
85     }
86   mpfr_set_str (x, "-8e8", 10, MPFR_RNDN);
87   mpfr_sech (y, x, MPFR_RNDN);
88   if (! (mpfr_zero_p (y) && MPFR_IS_POS (y)))
89     {
90       printf ("Error: sech(-8e8) != +0\n");
91       exit (1);
92     }
93 
94   mpfr_clear (x);
95   mpfr_clear (y);
96 }
97 
98 static void
overflowed_sech0(void)99 overflowed_sech0 (void)
100 {
101   mpfr_t x, y;
102   int emax, i, inex, rnd, err = 0;
103   mpfr_exp_t old_emax;
104 
105   old_emax = mpfr_get_emax ();
106 
107   mpfr_init2 (x, 8);
108   mpfr_init2 (y, 8);
109 
110   for (emax = -1; emax <= 0; emax++)
111     {
112       mpfr_set_ui_2exp (y, 1, emax, MPFR_RNDN);
113       mpfr_nextbelow (y);
114       set_emax (emax);  /* 1 is not representable. */
115       /* and if emax < 0, 1 - eps is not representable either. */
116       for (i = -1; i <= 1; i++)
117         RND_LOOP_NO_RNDF (rnd)
118           {
119             mpfr_set_si_2exp (x, i, -512 * ABS (i), MPFR_RNDN);
120             mpfr_clear_flags ();
121             inex = mpfr_sech (x, x, (mpfr_rnd_t) rnd);
122             if ((i == 0 || emax < 0 || rnd == MPFR_RNDN || rnd == MPFR_RNDU) &&
123                 ! mpfr_overflow_p ())
124               {
125                 printf ("Error in overflowed_sech0 (i = %d, rnd = %s):\n"
126                         "  The overflow flag is not set.\n",
127                         i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
128                 err = 1;
129               }
130             if (rnd == MPFR_RNDZ || rnd == MPFR_RNDD)
131               {
132                 if (inex >= 0)
133                   {
134                     printf ("Error in overflowed_sech0 (i = %d, rnd = %s):\n"
135                             "  The inexact value must be negative.\n",
136                             i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
137                     err = 1;
138                   }
139                 if (! mpfr_equal_p (x, y))
140                   {
141                     printf ("Error in overflowed_sech0 (i = %d, rnd = %s):\n"
142                             "  Got        ", i,
143                             mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
144                     mpfr_dump (x);
145                     printf ("  instead of 0.11111111E%d.\n", emax);
146                     err = 1;
147                   }
148               }
149             else
150               {
151                 if (inex <= 0)
152                   {
153                     printf ("Error in overflowed_sech0 (i = %d, rnd = %s):\n"
154                             "  The inexact value must be positive.\n",
155                             i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
156                     err = 1;
157                   }
158                 if (! (mpfr_inf_p (x) && MPFR_IS_POS (x)))
159                   {
160                     printf ("Error in overflowed_sech0 (i = %d, rnd = %s):\n"
161                             "  Got        ", i,
162                             mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
163                     mpfr_dump (x);
164                     printf ("  instead of +Inf.\n");
165                     err = 1;
166                   }
167               }
168           }
169       set_emax (old_emax);
170     }
171 
172   if (err)
173     exit (1);
174   mpfr_clear (x);
175   mpfr_clear (y);
176 }
177 
178 int
main(int argc,char * argv[])179 main (int argc, char *argv[])
180 {
181   tests_start_mpfr ();
182 
183   check_specials ();
184   test_generic (MPFR_PREC_MIN, 200, 10);
185   overflowed_sech0 ();
186 
187   tests_end_mpfr ();
188   return 0;
189 }
190