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