1/* Test file for mpfr_tan.
2
3Copyright 2001, 2002, 2003, 2004, 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_tan
29#define REDUCE_EMAX 262143 /* otherwise arg. reduction is too expensive */
30#include "tgeneric.c"
31
32static void
33check_nans (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_tan (y, x, MPFR_RNDN);
42  if (! mpfr_nan_p (y))
43    {
44      printf ("Error: tan(NaN) != NaN\n");
45      exit (1);
46    }
47
48  mpfr_set_inf (x, 1);
49  mpfr_tan (y, x, MPFR_RNDN);
50  if (! mpfr_nan_p (y))
51    {
52      printf ("Error: tan(Inf) != NaN\n");
53      exit (1);
54    }
55
56  mpfr_set_inf (x, -1);
57  mpfr_tan (y, x, MPFR_RNDN);
58  if (! mpfr_nan_p (y))
59    {
60      printf ("Error: tan(-Inf) != NaN\n");
61      exit (1);
62    }
63
64  /* exercise recomputation */
65  mpfr_set_prec (x, 14);
66  mpfr_set_str_binary (x, "0.10100000101010E0");
67  mpfr_set_prec (y, 24);
68  mpfr_tan (y, x, MPFR_RNDU);
69  mpfr_set_prec (x, 24);
70  mpfr_set_str_binary (x, "101110011011001100100001E-24");
71  MPFR_ASSERTN(mpfr_cmp (x, y) == 0);
72
73  /* Compute ~Pi/2 to check overflow */
74  mpfr_set_prec (x, 20000);
75  mpfr_const_pi (x, MPFR_RNDD);
76  mpfr_div_2ui (x, x, 1, MPFR_RNDN);
77  mpfr_set_prec (y, 24);
78  mpfr_tan (y, x, MPFR_RNDN);
79  if (mpfr_cmp_str (y, "0.100011101101011000100011E20001", 2, MPFR_RNDN))
80    {
81      printf("Error computing tan(~Pi/2)\n");
82      mpfr_dump (y);
83      exit (1);
84    }
85
86  /* bug found by Kaveh Ghazi on 13 Jul 2007 */
87  mpfr_set_prec (x, 53);
88  mpfr_set_prec (y, 53);
89  mpfr_set_str_binary (x, "0.10011100110111000001000010110100101000000000000000000E34");
90  mpfr_tan (y, x, MPFR_RNDN);
91  mpfr_set_str_binary (x, "0.1000010011001010001000010100000110100111000011010101E41");
92  MPFR_ASSERTN(mpfr_cmp (x, y) == 0);
93
94  mpfr_clear (x);
95  mpfr_clear (y);
96}
97
98int
99main (int argc, char *argv[])
100{
101  mpfr_t x;
102  unsigned int i;
103  unsigned int prec[10] = {14, 15, 19, 22, 23, 24, 25, 40, 41, 52};
104  unsigned int prec2[10] = {4, 5, 6, 19, 70, 95, 100, 106, 107, 108};
105
106  tests_start_mpfr ();
107
108  check_nans ();
109
110  mpfr_init (x);
111
112  mpfr_set_prec (x, 2);
113  mpfr_set_str (x, "0.5", 10, MPFR_RNDN);
114  mpfr_tan (x, x, MPFR_RNDD);
115  if (mpfr_cmp_ui_2exp(x, 1, -1))
116    {
117      printf ("mpfr_tan(0.5, MPFR_RNDD) failed\n"
118              "expected 0.5, got");
119      mpfr_print_binary(x);
120      putchar('\n');
121      exit (1);
122    }
123
124  /* check that tan(3*Pi/4) ~ -1 */
125  for (i=0; i<10; i++)
126    {
127      mpfr_set_prec (x, prec[i]);
128      mpfr_const_pi (x, MPFR_RNDN);
129      mpfr_mul_ui (x, x, 3, MPFR_RNDN);
130      mpfr_div_ui (x, x, 4, MPFR_RNDN);
131      mpfr_tan (x, x, MPFR_RNDN);
132      if (mpfr_cmp_si (x, -1))
133        {
134          printf ("tan(3*Pi/4) fails for prec=%u\n", prec[i]);
135          exit (1);
136        }
137    }
138
139  /* check that tan(7*Pi/4) ~ -1 */
140  for (i=0; i<10; i++)
141    {
142      mpfr_set_prec (x, prec2[i]);
143      mpfr_const_pi (x, MPFR_RNDN);
144      mpfr_mul_ui (x, x, 7, MPFR_RNDN);
145      mpfr_div_ui (x, x, 4, MPFR_RNDN);
146      mpfr_tan (x, x, MPFR_RNDN);
147      if (mpfr_cmp_si (x, -1))
148        {
149          printf ("tan(3*Pi/4) fails for prec=%u\n", prec2[i]);
150          exit (1);
151        }
152    }
153
154  mpfr_clear (x);
155
156  test_generic (2, 100, 10);
157
158  data_check ("data/tan", mpfr_tan, "mpfr_tan");
159  bad_cases (mpfr_tan, mpfr_atan, "mpfr_tan", 256, -256, 255, 4, 128, 800, 40);
160
161  tests_end_mpfr ();
162  return 0;
163}
164