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