Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tatan.c
1 /* Test file for mpfr_atan.
2
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 http://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 <stdio.h>
24 #include <stdlib.h>
25
26 #include "mpfr-test.h"
27
28 static void
29 special (void)
30 {
31   mpfr_t x, y, z;
32   int r;
33   int i;
34
35   mpfr_init2 (x, 53);
36   mpfr_init2 (y, 53);
37   mpfr_init2 (z, 53);
38
39   mpfr_set_str_binary (x, "1.0000100110000001100111100011001110101110100111011101");
40   mpfr_set_str_binary (y, "1.1001101101110100101100110011011101101000011010111110e-1");
41   mpfr_atan (z, x, MPFR_RNDN);
42   if (mpfr_cmp (y, z))
43     {
44       printf ("Error in mpfr_atan for prec=53, rnd=MPFR_RNDN\n");
45       printf ("x=");
46       mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
47       printf ("\nexpected ");
48       mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
49       printf ("\ngot      ");
50       mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN);
51       printf ("\n");
52       exit (1);
53     }
54
55   /* atan(+Inf) = Pi/2 */
56   for (r = 0; r < MPFR_RND_MAX ; r++)
57     {
58       mpfr_set_inf (x, 1);
59       mpfr_atan (y, x, (mpfr_rnd_t) r);
60       mpfr_const_pi (x, (mpfr_rnd_t) r);
61       mpfr_div_2exp (x, x, 1, (mpfr_rnd_t) r);
62       if (mpfr_cmp (x, y))
63         {
64           printf ("Error: mpfr_atan(+Inf), rnd=%s\n",
65                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
66           exit (1);
67         }
68     }
69
70   /* atan(-Inf) = - Pi/2 */
71   for (r = 0; r < MPFR_RND_MAX ; r++)
72     {
73       mpfr_set_inf (x, -1);
74       mpfr_atan (y, x, (mpfr_rnd_t) r);
75       mpfr_const_pi (x, MPFR_INVERT_RND((mpfr_rnd_t) r));
76       mpfr_neg (x, x, (mpfr_rnd_t) r);
77       mpfr_div_2exp (x, x, 1, (mpfr_rnd_t) r);
78       if (mpfr_cmp (x, y))
79         {
80           printf ("Error: mpfr_atan(-Inf), rnd=%s\n",
81                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
82           exit (1);
83         }
84     }
85
86   /* atan(NaN) = NaN */
87   mpfr_set_nan (x);
88   mpfr_atan (y, x, MPFR_RNDN);
89   if (!mpfr_nan_p (y))
90     {
91       printf ("Error: mpfr_atan(NaN) <> NaN\n");
92       exit (1);
93     }
94
95   /* atan(+/-0) = +/-0 */
96   mpfr_set_ui (x, 0, MPFR_RNDN);
97   MPFR_SET_NEG (y);
98   mpfr_atan (y, x, MPFR_RNDN);
99   if (mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y))
100     {
101       printf ("Error: mpfr_atan (+0) <> +0\n");
102       exit (1);
103     }
104   mpfr_atan (x, x, MPFR_RNDN);
105   if (mpfr_cmp_ui (x, 0) || MPFR_IS_NEG (x))
106     {
107       printf ("Error: mpfr_atan (+0) <> +0 (in place)\n");
108       exit (1);
109     }
110   mpfr_neg (x, x, MPFR_RNDN);
111   MPFR_SET_POS (y);
112   mpfr_atan (y, x, MPFR_RNDN);
113   if (mpfr_cmp_ui (y, 0) || MPFR_IS_POS (y))
114     {
115       printf ("Error: mpfr_atan (-0) <> -0\n");
116       exit (1);
117     }
118   mpfr_atan (x, x, MPFR_RNDN);
119   if (mpfr_cmp_ui (x, 0) || MPFR_IS_POS (x))
120     {
121       printf ("Error: mpfr_atan (-0) <> -0 (in place)\n");
122       exit (1);
123     }
124
125   mpfr_set_prec (x, 32);
126   mpfr_set_prec (y, 32);
127
128   /* test one random positive argument */
129   mpfr_set_str_binary (x, "0.10000100001100101001001001011001");
130   mpfr_atan (x, x, MPFR_RNDN);
131   mpfr_set_str_binary (y, "0.1111010000001111001111000000011E-1");
132   if (mpfr_cmp (x, y))
133     {
134       printf ("Error in mpfr_atan (1)\n");
135       exit (1);
136     }
137
138   /* test one random negative argument */
139   mpfr_set_str_binary (x, "-0.1100001110110000010101011001011");
140   mpfr_atan (x, x, MPFR_RNDN);
141   mpfr_set_str_binary (y, "-0.101001110001010010110001110001");
142   if (mpfr_cmp (x, y))
143     {
144       printf ("Error in mpfr_atan (2)\n");
145       mpfr_print_binary (x); printf ("\n");
146       mpfr_print_binary (y); printf ("\n");
147       exit (1);
148     }
149
150   mpfr_set_prec (x, 3);
151   mpfr_set_prec (y, 192);
152   mpfr_set_prec (z, 192);
153   mpfr_set_str_binary (x, "-0.100e1");
154   mpfr_atan (z, x, MPFR_RNDD);
155   mpfr_set_str_binary (y, "-0.110010010000111111011010101000100010000101101000110000100011010011000100110001100110001010001011100000001101110000011100110100010010100100000010010011100000100010001010011001111100110001110101");
156   if (mpfr_cmp (z, y))
157     {
158       printf ("Error in mpfr_atan (3)\n");
159       printf ("Expected "); mpfr_print_binary (y); printf ("\n");
160       printf ("Got      "); mpfr_print_binary (z); printf ("\n");
161       exit (1);
162     }
163
164   /* Test regression */
165   mpfr_set_prec (x, 51);
166   mpfr_set_prec (y, 51);
167   mpfr_set_str_binary (x,
168            "0.101100100000101111111010001111111000001000000000000E-11");
169   i = mpfr_atan (y, x, MPFR_RNDN);
170   if (mpfr_cmp_str (y,
171    "1.01100100000101111111001110011001010110100100000000e-12", 2, MPFR_RNDN)
172       || i >= 0)
173     {
174       printf ("Wrong Regression test (%d)\n", i);
175       mpfr_dump (y);
176       exit (1);
177     }
178
179   mpfr_set_si (x, -1, MPFR_RNDN);
180   mpfr_atan (x, x, MPFR_RNDN);
181   MPFR_ASSERTN (MPFR_IS_NEG (x));
182
183   /* Test regression */
184   mpfr_set_prec (x, 48);
185   mpfr_set_prec (y, 48);
186   mpfr_set_str_binary (x, "1.11001110010000011111100000010000000000000000000e-19");
187   mpfr_atan (y, x, MPFR_RNDD);
188   if (mpfr_cmp_str (y, "0.111001110010000011111100000001111111110000010011E-18", 2, MPFR_RNDN))
189     {
190       printf ("Error in mpfr_atan (4)\n");
191       printf ("Input    1.11001110010000011111100000010000000000000000000e-19 [prec=48]\n");
192       printf ("Expected 0.111001110010000011111100000001111111110000010011E-18\n");
193       printf ("Got      "); mpfr_dump (y);
194       exit (1);
195     }
196
197   mpfr_clear (x);
198   mpfr_clear (y);
199   mpfr_clear (z);
200 }
201
202 #define TEST_FUNCTION mpfr_atan
203 #define test_generic test_generic_atan
204 #define RAND_FUNCTION(x) (mpfr_urandomb (x, RANDS), mpfr_mul_2si (x, x, (randlimb () %1000-500), MPFR_RNDN))
205 #include "tgeneric.c"
206
207 #define TEST_FUNCTION mpfr_atan2
208 #define TWO_ARGS
209 #define test_generic test_generic_atan2
210 #include "tgeneric.c"
211
212 #define TEST_FUNCTION mpfr_atan2
213 #define TWO_ARGS
214 #define RAND_FUNCTION(x) (mpfr_urandomb (x, RANDS), MPFR_SET_NEG (x))
215 #define test_generic test_generic_atan2_neg
216 #include "tgeneric.c"
217
218 static void
219 special_overflow (void)
220 {
221   mpfr_t x, y;
222   mpfr_exp_t emin, emax;
223
224   emin = mpfr_get_emin ();
225   emax = mpfr_get_emax ();
226
227   set_emin (-125);
228   set_emax (128);
229   mpfr_init2 (x, 24);
230   mpfr_init2 (y, 48);
231   mpfr_set_str_binary (x, "0.101101010001001101111010E0");
232   mpfr_atan (y, x, MPFR_RNDN);
233   if (mpfr_cmp_str (y, "0.100111011001100111000010111101000111010101011110E0",
234                     2, MPFR_RNDN))
235     {
236       printf("Special Overflow error.\n");
237       mpfr_dump (y);
238       exit (1);
239     }
240
241   /* intermediate Pi overflows while atan(+Inf) = Pi/2 is representable */
242   set_emax (1);
243   mpfr_set_inf (x, +1);
244   mpfr_clear_flags ();
245   mpfr_atan (y, x, MPFR_RNDN);
246   if (mpfr_cmp_str (y, "C90FDAA22169p-47", 16, MPFR_RNDN)
247       || mpfr_overflow_p ())
248     {
249       printf("atan(+Inf) = Pi/2 should not overflow when emax = %ld\n",
250              (long int) mpfr_get_emax ());
251       mpfr_dump (y);
252       exit (1);
253     }
254
255   /* atan(+Inf) = Pi/2 underflows */
256   set_emax (128);
257   set_emin (3);
258   mpfr_clear_flags ();
259   mpfr_atan (y, x, MPFR_RNDN);
260   if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ())
261     {
262       printf("atan(+Inf) = Pi/2 should underflow when emin = %ld\n",
263              (long int) mpfr_get_emin ());
264       mpfr_dump (y);
265       exit (1);
266     }
267
268   /* intermediate Pi overflows while atan(+1) = Pi/4 is representable */
269   set_emax (1);
270   set_emin (-128);
271   mpfr_set_ui (x, 1, MPFR_RNDN);
272   mpfr_clear_flags ();
273   mpfr_atan (y, x, MPFR_RNDN);
274   if (mpfr_cmp_str (y, "C90FDAA22169p-48", 16, MPFR_RNDN)
275       || mpfr_overflow_p ())
276     {
277       printf("atan(+1) = Pi/4 should not overflow when emax = %ld\n",
278              (long int) mpfr_get_emax ());
279       mpfr_dump (y);
280       exit (1);
281     }
282
283   /* atan(+1) = Pi/4 underflows and is rounded up to 1 */
284   set_emax (128);
285   set_emin (1);
286   mpfr_set_prec (y, 2);
287   mpfr_clear_flags ();
288   mpfr_atan (y, x, MPFR_RNDN);
289   if (mpfr_cmp_ui (y, 1) || !mpfr_underflow_p ())
290     {
291       printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n",
292              (long int) mpfr_get_emin ());
293       mpfr_dump (y);
294       exit (1);
295     }
296
297   /* atan(+1) = Pi/4 underflows and is rounded down to 0 */
298   mpfr_clear_flags ();
299   mpfr_atan (y, x, MPFR_RNDD);
300   if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ())
301     {
302       printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n",
303              (long int) mpfr_get_emin ());
304       mpfr_dump (y);
305       exit (1);
306     }
307
308   mpfr_clear (y);
309   mpfr_clear (x);
310   set_emin (emin);
311   set_emax (emax);
312 }
313
314 static void
315 special_atan2 (void)
316 {
317   mpfr_t x, y, z;
318
319   mpfr_inits2 (4, x, y, z, (mpfr_ptr) 0);
320
321   /* Anything with NAN should be set to NAN */
322   mpfr_set_ui (y, 0, MPFR_RNDN);
323   mpfr_set_nan (x);
324   mpfr_atan2 (z, y, x, MPFR_RNDN);
325   MPFR_ASSERTN (MPFR_IS_NAN (z));
326   mpfr_swap (x, y);
327   mpfr_atan2 (z, y, x, MPFR_RNDN);
328   MPFR_ASSERTN (MPFR_IS_NAN (z));
329
330   /* 0+ 0+ --> 0+ */
331   mpfr_set_ui (y, 0, MPFR_RNDN);
332   mpfr_atan2 (z, y, x, MPFR_RNDN);
333   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z));
334   /* 0- 0+ --> 0- */
335   MPFR_CHANGE_SIGN (y);
336   mpfr_atan2 (z, y, x, MPFR_RNDN);
337   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z));
338   /* 0- 0- --> -PI */
339   MPFR_CHANGE_SIGN (x);
340   mpfr_atan2 (z, y, x, MPFR_RNDN);
341   MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0);
342   /* 0+ 0- --> +PI */
343   MPFR_CHANGE_SIGN (y);
344   mpfr_atan2 (z, y, x, MPFR_RNDN);
345   MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0);
346   /* 0+ -1 --> PI */
347   mpfr_set_si (x, -1, MPFR_RNDN);
348   mpfr_atan2 (z, y, x, MPFR_RNDN);
349   MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0);
350   /* 0- -1 --> -PI */
351   MPFR_CHANGE_SIGN (y);
352   mpfr_atan2 (z, y, x, MPFR_RNDN);
353   MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0);
354   /* 0- +1 --> 0- */
355   mpfr_set_ui (x, 1, MPFR_RNDN);
356   mpfr_atan2 (z, y, x, MPFR_RNDN);
357   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z));
358   /* 0+ +1 --> 0+ */
359   MPFR_CHANGE_SIGN (y);
360   mpfr_atan2 (z, y, x, MPFR_RNDN);
361   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z));
362   /* +1 0+ --> PI/2 */
363   mpfr_swap (x, y);
364   mpfr_atan2 (z, y, x, MPFR_RNDN);
365   MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0);
366   /* +1 0- --> PI/2 */
367   MPFR_CHANGE_SIGN (x);
368   mpfr_atan2 (z, y, x, MPFR_RNDN);
369   MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0);
370   /* -1 0- --> -PI/2 */
371   MPFR_CHANGE_SIGN (y);
372   mpfr_atan2 (z, y, x, MPFR_RNDN);
373   MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0);
374   /* -1 0+ --> -PI/2 */
375   MPFR_CHANGE_SIGN (x);
376   mpfr_atan2 (z, y, x, MPFR_RNDN);
377   MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0);
378
379   /* -1 +INF --> -0 */
380   MPFR_SET_INF (x);
381   mpfr_atan2 (z, y, x, MPFR_RNDN);
382   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z));
383   /* +1 +INF --> +0 */
384   MPFR_CHANGE_SIGN (y);
385   mpfr_atan2 (z, y, x, MPFR_RNDN);
386   MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z));
387   /* +1 -INF --> +PI */
388   MPFR_CHANGE_SIGN (x);
389   mpfr_atan2 (z, y, x, MPFR_RNDN);
390   MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0);
391   /* -1 -INF --> -PI */
392   MPFR_CHANGE_SIGN (y);
393   mpfr_atan2 (z, y, x, MPFR_RNDN);
394   MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0);
395   /* -INF -1 --> -PI/2 */
396   mpfr_swap (x, y);
397   mpfr_atan2 (z, y, x, MPFR_RNDN);
398   MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0);
399   /* +INF -1  --> PI/2 */
400   MPFR_CHANGE_SIGN (y);
401   mpfr_atan2 (z, y, x, MPFR_RNDN);
402   MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0);
403   /* +INF -INF --> 3*PI/4 */
404   MPFR_SET_INF (x);
405   mpfr_atan2 (z, y, x, MPFR_RNDN);
406   MPFR_ASSERTN (mpfr_cmp_str (z, "2.356194490192344928", 10, MPFR_RNDN) == 0);
407   /* +INF +INF --> PI/4 */
408   MPFR_CHANGE_SIGN (x);
409   mpfr_atan2 (z, y, x, MPFR_RNDN);
410   MPFR_ASSERTN (mpfr_cmp_str (z, "0.785375", 10, MPFR_RNDN) == 0);
411   /* -INF +INF --> -PI/4 */
412   MPFR_CHANGE_SIGN (y);
413   mpfr_atan2 (z, y, x, MPFR_RNDN);
414   MPFR_ASSERTN (mpfr_cmp_str (z, "-0.785375", 10, MPFR_RNDN) == 0);
415   /* -INF -INF --> -3*PI/4 */
416   MPFR_CHANGE_SIGN (x);
417   mpfr_atan2 (z, y, x, MPFR_RNDN);
418   MPFR_ASSERTN (mpfr_cmp_str (z, "-2.356194490192344928", 10, MPFR_RNDN) == 0);
419   mpfr_set_prec (z, 905); /* exercises Ziv's loop */
420   mpfr_atan2 (z, y, x, MPFR_RNDZ);
421   MPFR_ASSERTN (mpfr_cmp_str (z, "-2.35619449019234492884698253745962716314787704953132936573120844423086230471465674897102611900658780098661106488496172998532038345716293667379401955609636083808771307702645389082916973346721171619778647332160823174945008459635673617534008737395340143185923642519259526145784", 10, MPFR_RNDN) == 0);
422
423   mpfr_clears (x, y, z, (mpfr_ptr) 0);
424 }
425
426 /* from Christopher Creutzig, 18 Jul 2007 */
427 static void
428 smallvals_atan2 (void)
429 {
430   mpfr_t a, x, y;
431   mpfr_exp_t old_emin;
432
433   mpfr_inits (a, x, y, (mpfr_ptr) 0);
434   mpfr_set_ui (y, 0, MPFR_RNDN);
435   mpfr_nextbelow (y);
436   mpfr_set_ui (x, 1, MPFR_RNDN);
437   /* y=-2^(-emin-1), x=1 */
438
439   mpfr_atan2 (a, y, x, MPFR_RNDD);
440   MPFR_ASSERTN (mpfr_equal_p (a, y));
441
442   mpfr_atan2 (a, y, x, MPFR_RNDU);
443   MPFR_ASSERTN (mpfr_zero_p (a) && MPFR_IS_NEG(a));
444
445   mpfr_set_prec (x, 8);
446   mpfr_set_prec (y, 8);
447   mpfr_set_prec (a, 8);
448   old_emin = mpfr_get_emin ();
449   mpfr_set_emin (MPFR_EMIN_MIN);
450
451   mpfr_set_si (y, 3, MPFR_RNDN);
452   mpfr_set_exp (y, mpfr_get_emin ());
453   mpfr_set_str_binary (x, "1.1");
454   mpfr_atan2 (a, y, x, MPFR_RNDU);
455   mpfr_set_si (y, 1, MPFR_RNDN);
456   mpfr_set_exp (y, mpfr_get_emin ());
457   MPFR_ASSERTN (mpfr_equal_p (a, y));
458
459   /* From a bug reported by Christopher Creutzig on 2007-08-28.
460      Added test in each rounding mode.
461      Segmentation fault or assertion failure due to an infinite Ziv loop. */
462   mpfr_set_si (y, 1, MPFR_RNDN);
463   mpfr_set_exp (y, mpfr_get_emin ());
464   mpfr_set_str_binary (x, "1.01");
465   mpfr_atan2 (a, y, x, MPFR_RNDZ);
466   MPFR_ASSERTN (mpfr_zero_p (a));
467   mpfr_atan2 (a, y, x, MPFR_RNDD);
468   MPFR_ASSERTN (mpfr_zero_p (a));
469   mpfr_atan2 (a, y, x, MPFR_RNDU);
470   MPFR_ASSERTN (mpfr_equal_p (a, y));
471   mpfr_atan2 (a, y, x, MPFR_RNDN);
472   MPFR_ASSERTN (mpfr_equal_p (a, y));
473
474   /* trigger underflow with rounding to nearest */
475   mpfr_set_ui (x, 4, MPFR_RNDN);
476   mpfr_atan2 (a, y, x, MPFR_RNDN);
477   MPFR_ASSERTN (mpfr_zero_p (a));
478
479   mpfr_set_emin (old_emin);
480
481   mpfr_clears (a, x, y, (mpfr_ptr) 0);
482 }
483
484 /* Bug found by Robert Bajema (regression in MPFR 2.3.0).
485    The cause is the underflow flag set before the mpfr_atan2 call. */
486 static void
487 atan2_bug_20071003 (void)
488 {
489   mpfr_t a, x, y, z;
490
491   mpfr_inits (a, x, y, z, (mpfr_ptr) 0);
492
493   mpfr_set_underflow ();
494   mpfr_set_str_binary (y,
495     "-0.10100110110100110111010110111111100110100010001110110E2");
496   mpfr_set_str_binary (x,
497     "0.10100101010110010100010010111000110110011110001011110E3");
498   mpfr_set_str_binary (z,
499     "-0.11101111001101101100111011001101000010010111101110110E-1");
500   mpfr_atan2 (a, y, x, MPFR_RNDN);
501   if (! mpfr_equal_p (a, z))
502     {
503       printf ("mpfr_atan2 fails on:\n");
504       printf ("  y = ");
505       mpfr_dump (y);
506       printf ("  x = ");
507       mpfr_dump (x);
508       printf ("Expected ");
509       mpfr_dump (z);
510       printf ("Got      ");
511       mpfr_dump (a);
512       exit (1);
513     }
514
515   mpfr_clears (a, x, y, z, (mpfr_ptr) 0);
516 }
517
518 /* Bug found on 2009-04-29 by Christopher Creutzig.
519  * With r6179: atan.c:62: MPFR assertion failed: r > n
520  */
521 static void
522 atan2_different_prec (void)
523 {
524   mpfr_t a, x, y;
525
526   mpfr_init2 (a, 59);
527   mpfr_init2 (x, 59);
528   mpfr_init2 (y, 86);
529
530   mpfr_set_ui (x, 1, MPFR_RNDN);
531   mpfr_set_ui (y, 1, MPFR_RNDN);
532   mpfr_nextbelow (y);
533   mpfr_atan2 (a, y, x, MPFR_RNDN);
534
535   mpfr_clears (a, x, y, (mpfr_ptr) 0);
536 }
537
538 static void
539 atan2_pow_of_2 (void)
540 {
541   mpfr_t x, y, r, g;
542   int i;
543   int d[] = { 0, -1, 1 };
544   int ntests = sizeof (d) / sizeof (int);
545
546   mpfr_init2 (x, 53);
547   mpfr_init2 (y, 53);
548   mpfr_init2 (r, 53);
549   mpfr_init2 (g, 53);
550
551   /* atan(42) */
552   mpfr_set_str_binary (g, "1100011000000011110011111001100110101000011010010011E-51");
553
554   for (i = 0; i < ntests; ++i)
555     {
556       mpfr_set_ui (y, 42, MPFR_RNDN);
557       mpfr_mul_2si (y, y, d[i], MPFR_RNDN);
558       mpfr_set_ui_2exp (x, 1, d[i], MPFR_RNDN);
559       mpfr_atan2 (r, y, x, MPFR_RNDN);
560       if (mpfr_equal_p (r, g) == 0)
561         {
562           printf ("Error in mpfr_atan2 (5)\n");
563           printf ("Expected "); mpfr_print_binary (g); printf ("\n");
564           printf ("Got      "); mpfr_print_binary (r); printf ("\n");
565           exit (1);
566         }
567     }
568   mpfr_clear (x);
569   mpfr_clear (y);
570   mpfr_clear (r);
571   mpfr_clear (g);
572 }
573
574 /* https://sympa.inria.fr/sympa/arc/mpfr/2011-05/msg00008.html
575  * Incorrect flags (in debug mode on a 32-bit machine, assertion failure).
576  */
577 static void
578 reduced_expo_range (void)
579 {
580   mpfr_exp_t emin, emax;
581   mpfr_t x, y, ex_y;
582   int inex, ex_inex;
583   unsigned int flags, ex_flags;
584
585   emin = mpfr_get_emin ();
586   emax = mpfr_get_emax ();
587
588   mpfr_inits2 (12, x, y, ex_y, (mpfr_ptr) 0);
589   mpfr_set_str (x, "0.1e-5", 2, MPFR_RNDN);
590
591   mpfr_set_emin (-5);
592   mpfr_set_emax (-5);
593   mpfr_clear_flags ();
594   inex = mpfr_atan (y, x, MPFR_RNDN);
595   flags = __gmpfr_flags;
596   mpfr_set_emin (emin);
597   mpfr_set_emax (emax);
598
599   mpfr_set_str (ex_y, "0.1e-5", 2, MPFR_RNDN);
600   ex_inex = 1;
601   ex_flags = MPFR_FLAGS_INEXACT;
602
603   if (SIGN (inex) != ex_inex || flags != ex_flags ||
604       ! mpfr_equal_p (y, ex_y))
605     {
606       printf ("Error in reduced_expo_range\non x = ");
607       mpfr_dump (x);
608       printf ("Expected y = ");
609       mpfr_out_str (stdout, 2, 0, ex_y, MPFR_RNDN);
610       printf ("\n         inex = %d, flags = %u\n", ex_inex, ex_flags);
611       printf ("Got      y = ");
612       mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
613       printf ("\n         inex = %d, flags = %u\n", SIGN (inex), flags);
614       exit (1);
615     }
616
617   mpfr_clears (x, y, ex_y, (mpfr_ptr) 0);
618 }
619
620 int
621 main (int argc, char *argv[])
622 {
623   tests_start_mpfr ();
624
625   special_overflow ();
626   special ();
627   special_atan2 ();
628   smallvals_atan2 ();
629   atan2_bug_20071003 ();
630   atan2_different_prec ();
631   reduced_expo_range ();
632
633   test_generic_atan  (2, 200, 17);
634   test_generic_atan2 (2, 200, 17);
635   test_generic_atan2_neg (2, 200, 17);
636
637   data_check ("data/atan", mpfr_atan, "mpfr_atan");
638   bad_cases (mpfr_atan, mpfr_tan, "mpfr_atan", 256, -40, 1, 4, 128, 800, 40);
639   atan2_pow_of_2 ();
640
641   tests_end_mpfr ();
642   return 0;
643 }