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