1/* Test file for mpfr_cot.
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_cot
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_cot (y, x, MPFR_RNDN);
42  if (! mpfr_nan_p (y))
43    {
44      printf ("Error: cot(NaN) != NaN\n");
45      exit (1);
46    }
47
48  mpfr_set_inf (x, 1);
49  mpfr_cot (y, x, MPFR_RNDN);
50  if (! mpfr_nan_p (y))
51    {
52      printf ("Error: cot(Inf) != NaN\n");
53      exit (1);
54    }
55
56  mpfr_set_inf (x, -1);
57  mpfr_cot (y, x, MPFR_RNDN);
58  if (! mpfr_nan_p (y))
59    {
60      printf ("Error: cot(-Inf) != NaN\n");
61      exit (1);
62    }
63
64  /* cot(+/-0) = +/-Inf */
65  mpfr_set_ui (x, 0, MPFR_RNDN);
66  mpfr_cot (y, x, MPFR_RNDN);
67  if (! (mpfr_inf_p (y) && mpfr_sgn (y) > 0))
68    {
69      printf ("Error: cot(+0) != +Inf\n");
70      exit (1);
71    }
72  mpfr_neg (x, x, MPFR_RNDN);
73  mpfr_cot (y, x, MPFR_RNDN);
74  if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
75    {
76      printf ("Error: cot(-0) != -Inf\n");
77      exit (1);
78    }
79
80  mpfr_clear (x);
81  mpfr_clear (y);
82}
83
84static void
85two2emin (mpfr_exp_t e)
86{
87  mpfr_exp_t old_emin, old_emax;
88  mpfr_t x, y;
89  int i, rnd;
90
91  old_emin = mpfr_get_emin ();
92  old_emax = mpfr_get_emax ();
93
94  if (mpfr_set_emin (-e) || mpfr_set_emax (e))
95    {
96      printf ("Can't change exponent range\n");
97      exit (1);
98    }
99
100  mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
101  for (i = -4; i <= 4; i++)
102    RND_LOOP (rnd)
103      {
104        mpfr_set_si (y, i, MPFR_RNDN);
105        mpfr_ui_div (y, 1, y, (mpfr_rnd_t) rnd);  /* no overflow/underflow */
106        mpfr_set_si_2exp (x, i, -e, MPFR_RNDN);
107        if (ABS (i) != 3)  /* not a power of 2 (not 0 either) */
108          mpfr_sub (y, y, x, (mpfr_rnd_t) rnd);  /* no overflow/underflow */
109        mpfr_set_ui_2exp (x, 1, -e, MPFR_RNDN);
110        mpfr_div (y, y, x, (mpfr_rnd_t) rnd);  /* 1/x - SIGN(x).epsilon */
111        mpfr_set_si_2exp (x, i, -e, MPFR_RNDN);
112        mpfr_cot (x, x, (mpfr_rnd_t) rnd);
113        if (! mpfr_equal_p (x, y))
114          {
115            printf ("Error in two2emin for i = %d and rnd = %s\n",
116                    i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
117            printf ("Got        ");
118            mpfr_dump (x);
119            printf ("instead of ");
120            mpfr_dump (y);
121            exit (1);
122          }
123      }
124  mpfr_clears (x, y, (mpfr_ptr) 0);
125
126  mpfr_set_emin (old_emin);
127  mpfr_set_emax (old_emax);
128}
129
130int
131main (int argc, char *argv[])
132{
133  tests_start_mpfr ();
134
135  check_specials ();
136  two2emin (256);
137  two2emin (MPFR_EMAX_DEFAULT);
138  if (MPFR_EMAX_MAX != MPFR_EMAX_DEFAULT)
139    two2emin (MPFR_EMAX_MAX);
140  test_generic (2, 200, 5);
141
142  tests_end_mpfr ();
143  return 0;
144}
145