Import Upstream version 0.8.2
[platform/upstream/mpc.git] / tests / ttan.c
1 /* test file for mpc_tan.
2
3 Copyright (C) 2008 Philippe Th\'eveny, Paul Zimmermann, Andreas Enge
4
5 This file is part of the MPC Library.
6
7 The MPC Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11
12 The MPC Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPC Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include <stdlib.h>
23 #include "mpc-tests.h"
24
25 static void
26 test_failed (mpc_t op, mpc_t get, mpc_t expected)
27 {
28   printf ("mpc_tan(op) failed\n with ");
29   OUT (op);
30   printf ("     ");
31   OUT (get);
32   OUT (expected);
33   exit (1);
34 }
35
36 static void
37 pure_real_argument (void)
38 {
39   /* tan(x -i*0) = tan(x) -i*0 */
40   /* tan(x +i*0) = tan(x) +i*0 */
41   mpfr_t x;
42   mpfr_t tan_x;
43   mpc_t z;
44   mpc_t tan_z;
45
46   mpfr_init2 (x, 79);
47   mpfr_init2 (tan_x, 113);
48   mpc_init2 (z, 79);
49   mpc_init2 (tan_z, 113);
50
51   /* tan(1 +i*0) = tan(1) +i*0 */
52   mpc_set_ui_ui (z, 1, 0, MPC_RNDNN);
53   mpfr_set_ui (x, 1, GMP_RNDN);
54   mpfr_tan (tan_x, x, GMP_RNDN);
55   mpc_tan (tan_z, z, MPC_RNDNN);
56   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
57       || !mpfr_zero_p (MPC_IM (tan_z)) || mpfr_signbit (MPC_IM (tan_z)))
58     {
59       printf ("mpc_tan(1 + i * 0) failed\n");
60       exit (1);
61     }
62
63   /* tan(1 -i*0) = tan(1) -i*0 */
64   mpc_conj (z, z, MPC_RNDNN);
65   mpc_tan (tan_z, z, MPC_RNDNN);
66   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
67       || !mpfr_zero_p (MPC_IM (tan_z)) || !mpfr_signbit (MPC_IM (tan_z)))
68     {
69       printf ("mpc_tan(1 - i * 0) failed\n");
70       exit (1);
71     }
72
73   /* tan(Pi/2 +i*0) = +Inf +i*0 */
74   mpfr_const_pi (x, GMP_RNDN);
75   mpfr_div_2ui (x, x, 1, GMP_RNDN);
76   mpfr_set (MPC_RE (z), x, GMP_RNDN);
77   mpfr_set_ui (MPC_IM (z), 0, GMP_RNDN);
78   mpfr_tan (tan_x, x, GMP_RNDN);
79   mpc_tan (tan_z, z, MPC_RNDNN);
80   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
81       || !mpfr_zero_p (MPC_IM (tan_z)) || mpfr_signbit (MPC_IM (tan_z)))
82     {
83       printf ("mpc_tan(Pi/2 + i * 0) failed\n");
84       exit (1);
85     }
86
87   /* tan(Pi/2 -i*0) = +Inf -i*0 */
88   mpc_conj (z, z, MPC_RNDNN);
89   mpc_tan (tan_z, z, MPC_RNDNN);
90   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
91       || !mpfr_zero_p (MPC_IM (tan_z)) || !mpfr_signbit (MPC_IM (tan_z)))
92     {
93       printf ("mpc_tan(Pi/2 - i * 0) failed\n");
94       exit (1);
95     }
96
97   /* tan(-Pi/2 +i*0) = -Inf +i*0 */
98   mpfr_neg (x, x, GMP_RNDN);
99   mpc_neg (z, z, MPC_RNDNN);
100   mpfr_tan (tan_x, x, GMP_RNDN);
101   mpc_tan (tan_z, z, MPC_RNDNN);
102   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
103       || !mpfr_zero_p (MPC_IM (tan_z)) || mpfr_signbit (MPC_IM (tan_z)))
104     {
105       printf ("mpc_tan(-Pi/2 + i * 0) failed\n");
106       exit (1);
107     }
108
109   /* tan(-Pi/2 -i*0) = -Inf -i*0 */
110   mpc_conj (z, z, MPC_RNDNN);
111   mpc_tan (tan_z, z, MPC_RNDNN);
112   if (mpfr_cmp (MPC_RE (tan_z), tan_x) != 0
113       || !mpfr_zero_p (MPC_IM (tan_z)) || !mpfr_signbit (MPC_IM (tan_z)))
114     {
115       printf ("mpc_tan(-Pi/2 - i * 0) failed\n");
116       exit (1);
117     }
118
119   mpc_clear (tan_z);
120   mpc_clear (z);
121   mpfr_clear (tan_x);
122   mpfr_clear (x);
123 }
124
125 static void
126 pure_imaginary_argument (void)
127 {
128   /* tan(-0 +i*y) = -0 +i*tanh(y) */
129   /* tan(+0 +i*y) = +0 +i*tanh(y) */
130   mpfr_t y;
131   mpfr_t tanh_y;
132   mpc_t z;
133   mpc_t tan_z;
134   mp_prec_t prec = (mp_prec_t) 111;
135
136   mpfr_init2 (y, 2);
137   mpfr_init2 (tanh_y, prec);
138   mpc_init2 (z, 2);
139   mpc_init2 (tan_z, prec);
140
141   /* tan(0 +i) = +0 +i*tanh(1) */
142   mpc_set_ui_ui (z, 0, 1, MPC_RNDNN);
143   mpfr_set_ui (y, 1, GMP_RNDN);
144   mpfr_tanh (tanh_y, y, GMP_RNDN);
145   mpc_tan (tan_z, z, MPC_RNDNN);
146   if (mpfr_cmp (MPC_IM (tan_z), tanh_y) != 0
147       || !mpfr_zero_p (MPC_RE (tan_z)) || mpfr_signbit (MPC_RE (tan_z)))
148     {
149       mpc_t c99;
150
151       mpc_init2 (c99, prec);
152       mpfr_set_ui (MPC_RE (c99), 0, GMP_RNDN);
153       mpfr_set (MPC_IM (c99), tanh_y, GMP_RNDN);
154
155       test_failed (z, tan_z, c99);
156     }
157
158   /* tan(0 -i) = +0 +i*tanh(-1) */
159   mpc_conj (z, z, MPC_RNDNN);
160   mpfr_neg (tanh_y, tanh_y, GMP_RNDN);
161   mpc_tan (tan_z, z, MPC_RNDNN);
162   if (mpfr_cmp (MPC_IM (tan_z), tanh_y) != 0
163       || !mpfr_zero_p (MPC_RE (tan_z)) || mpfr_signbit (MPC_RE (tan_z)))
164     {
165       mpc_t c99;
166
167       mpc_init2 (c99, prec);
168       mpfr_set_ui (MPC_RE (c99), 0, GMP_RNDN);
169       mpfr_set (MPC_IM (c99), tanh_y, GMP_RNDN);
170
171       test_failed (z, tan_z, c99);
172     }
173
174   /* tan(-0 +i) = -0 +i*tanh(1) */
175   mpc_neg (z, z, MPC_RNDNN);
176   mpfr_neg (tanh_y, tanh_y, GMP_RNDN);
177   mpc_tan (tan_z, z, MPC_RNDNN);
178   if (mpfr_cmp (MPC_IM (tan_z), tanh_y) != 0
179       || !mpfr_zero_p (MPC_RE (tan_z)) || !mpfr_signbit (MPC_RE (tan_z)))
180     {
181       mpc_t c99;
182
183       mpc_init2 (c99, prec);
184       mpfr_set_ui (MPC_RE (c99), 0, GMP_RNDN);
185       mpfr_set (MPC_IM (c99), tanh_y, GMP_RNDN);
186
187       test_failed (z, tan_z, c99);
188     }
189
190   /* tan(-0 -i) = -0 +i*tanh(-1) */
191   mpc_conj (z, z, MPC_RNDNN);
192   mpfr_neg (tanh_y, tanh_y, GMP_RNDN);
193   mpc_tan (tan_z, z, MPC_RNDNN);
194   if (mpfr_cmp (MPC_IM (tan_z), tanh_y) != 0
195       || !mpfr_zero_p (MPC_RE (tan_z)) || !mpfr_signbit (MPC_RE (tan_z)))
196     {
197       mpc_t c99;
198
199       mpc_init2 (c99, prec);
200       mpfr_set_ui (MPC_RE (c99), 0, GMP_RNDN);
201       mpfr_set (MPC_IM (c99), tanh_y, GMP_RNDN);
202
203       test_failed (z, tan_z, c99);
204     }
205
206   mpc_clear (tan_z);
207   mpc_clear (z);
208   mpfr_clear (tanh_y);
209   mpfr_clear (y);
210 }
211
212 static void
213 check_53 (void)
214 {
215   mpc_t z;
216   mpc_t tan_z;
217   mpc_t t;
218
219   mpc_init2 (z, 53);
220   mpc_init2 (tan_z, 53);
221   mpc_init2 (t, 53);
222
223   /* Let's play around the poles */
224   /* x = Re(z) = Pi/2 rounded to nearest to 53 bits precision */
225   /* y = Im(z) = Pi/2 - Re(z) rounded to nearest to 53 bits precision */
226   mpfr_set_str (MPC_RE (z), "3243F6A8885A30p-53", 16, GMP_RNDN);
227   mpfr_set_str (MPC_IM (z), "11A62633145C07p-106", 16, GMP_RNDN);
228   mpfr_set_str (MPC_RE (t), "1D02967C31CDB5", 16, GMP_RNDN);
229   mpfr_set_str (MPC_IM (t), "1D02967C31CDB5", 16, GMP_RNDN);
230   mpc_tan (tan_z, z, MPC_RNDNN);
231   if (mpc_cmp (tan_z, t) != 0)
232     test_failed (z, tan_z, t);
233
234   mpfr_set_str (MPC_RE (t), "1D02967C31CDB4", 16, GMP_RNDN);
235   mpc_tan (tan_z, z, MPC_RNDDU);
236   if (mpc_cmp (tan_z, t) != 0)
237     test_failed (z, tan_z, t);
238
239   mpfr_set_str (MPC_IM (t), "1D02967C31CDB4", 16, GMP_RNDN);
240   mpc_tan (tan_z, z, MPC_RNDZD);
241   if (mpc_cmp (tan_z, t) != 0)
242     test_failed (z, tan_z, t);
243
244   /* Re(z) = x + 2^(-52) */
245   /* Im(z) = y - 2^(-52) */
246   mpfr_set_str (MPC_RE (z), "1921FB54442D19p-52", 16, GMP_RNDN);
247   mpfr_set_str (MPC_IM (z), "-172CECE675D1FDp-105", 16, GMP_RNDN);
248   mpfr_set_str (MPC_RE (t), "-B0BD0AA4A3B3D", 16, GMP_RNDN);
249   mpfr_set_str (MPC_IM (t), "-B0BD0AA4A3B3D", 16, GMP_RNDN);
250   mpc_tan (tan_z, z, MPC_RNDNN);
251   if (mpc_cmp (tan_z, t) != 0)
252     test_failed (z, tan_z, t);
253
254   mpc_clear (t);
255   mpc_clear (tan_z);
256   mpc_clear (z);
257 }
258
259 int
260 main (void)
261 {
262   DECL_FUNC (CC, f, mpc_tan);
263
264   test_start ();
265
266   data_check (f, "tan.dat");
267   tgeneric (f, 2, 512, 7, 4);
268
269   pure_real_argument ();
270   pure_imaginary_argument ();
271   check_53 ();
272
273   test_end ();
274
275   return 0;
276 }