1 /* Test file for mpfr_atan.
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.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
26 #include "mpfr-test.h"
39 mpfr_set_str_binary (x, "1.0000100110000001100111100011001110101110100111011101");
40 mpfr_set_str_binary (y, "1.1001101101110100101100110011011101101000011010111110e-1");
41 mpfr_atan (z, x, MPFR_RNDN);
44 printf ("Error in mpfr_atan for prec=53, rnd=MPFR_RNDN\n");
46 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
47 printf ("\nexpected ");
48 mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
50 mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN);
55 /* atan(+Inf) = Pi/2 */
56 for (r = 0; r < MPFR_RND_MAX ; r++)
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);
64 printf ("Error: mpfr_atan(+Inf), rnd=%s\n",
65 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
70 /* atan(-Inf) = - Pi/2 */
71 for (r = 0; r < MPFR_RND_MAX ; r++)
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);
80 printf ("Error: mpfr_atan(-Inf), rnd=%s\n",
81 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
88 mpfr_atan (y, x, MPFR_RNDN);
91 printf ("Error: mpfr_atan(NaN) <> NaN\n");
95 /* atan(+/-0) = +/-0 */
96 mpfr_set_ui (x, 0, MPFR_RNDN);
98 mpfr_atan (y, x, MPFR_RNDN);
99 if (mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y))
101 printf ("Error: mpfr_atan (+0) <> +0\n");
104 mpfr_atan (x, x, MPFR_RNDN);
105 if (mpfr_cmp_ui (x, 0) || MPFR_IS_NEG (x))
107 printf ("Error: mpfr_atan (+0) <> +0 (in place)\n");
110 mpfr_neg (x, x, MPFR_RNDN);
112 mpfr_atan (y, x, MPFR_RNDN);
113 if (mpfr_cmp_ui (y, 0) || MPFR_IS_POS (y))
115 printf ("Error: mpfr_atan (-0) <> -0\n");
118 mpfr_atan (x, x, MPFR_RNDN);
119 if (mpfr_cmp_ui (x, 0) || MPFR_IS_POS (x))
121 printf ("Error: mpfr_atan (-0) <> -0 (in place)\n");
125 mpfr_set_prec (x, 32);
126 mpfr_set_prec (y, 32);
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");
134 printf ("Error in mpfr_atan (1)\n");
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");
144 printf ("Error in mpfr_atan (2)\n");
145 mpfr_print_binary (x); printf ("\n");
146 mpfr_print_binary (y); printf ("\n");
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");
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");
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);
171 "1.01100100000101111111001110011001010110100100000000e-12", 2, MPFR_RNDN)
174 printf ("Wrong Regression test (%d)\n", i);
179 mpfr_set_si (x, -1, MPFR_RNDN);
180 mpfr_atan (x, x, MPFR_RNDN);
181 MPFR_ASSERTN (MPFR_IS_NEG (x));
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))
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);
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"
207 #define TEST_FUNCTION mpfr_atan2
209 #define test_generic test_generic_atan2
210 #include "tgeneric.c"
212 #define TEST_FUNCTION mpfr_atan2
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"
219 special_overflow (void)
222 mpfr_exp_t emin, emax;
224 emin = mpfr_get_emin ();
225 emax = mpfr_get_emax ();
231 mpfr_set_str_binary (x, "0.101101010001001101111010E0");
232 mpfr_atan (y, x, MPFR_RNDN);
233 if (mpfr_cmp_str (y, "0.100111011001100111000010111101000111010101011110E0",
236 printf("Special Overflow error.\n");
241 /* intermediate Pi overflows while atan(+Inf) = Pi/2 is representable */
243 mpfr_set_inf (x, +1);
245 mpfr_atan (y, x, MPFR_RNDN);
246 if (mpfr_cmp_str (y, "C90FDAA22169p-47", 16, MPFR_RNDN)
247 || mpfr_overflow_p ())
249 printf("atan(+Inf) = Pi/2 should not overflow when emax = %ld\n",
250 (long int) mpfr_get_emax ());
255 /* atan(+Inf) = Pi/2 underflows */
259 mpfr_atan (y, x, MPFR_RNDN);
260 if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ())
262 printf("atan(+Inf) = Pi/2 should underflow when emin = %ld\n",
263 (long int) mpfr_get_emin ());
268 /* intermediate Pi overflows while atan(+1) = Pi/4 is representable */
271 mpfr_set_ui (x, 1, MPFR_RNDN);
273 mpfr_atan (y, x, MPFR_RNDN);
274 if (mpfr_cmp_str (y, "C90FDAA22169p-48", 16, MPFR_RNDN)
275 || mpfr_overflow_p ())
277 printf("atan(+1) = Pi/4 should not overflow when emax = %ld\n",
278 (long int) mpfr_get_emax ());
283 /* atan(+1) = Pi/4 underflows and is rounded up to 1 */
286 mpfr_set_prec (y, 2);
288 mpfr_atan (y, x, MPFR_RNDN);
289 if (mpfr_cmp_ui (y, 1) || !mpfr_underflow_p ())
291 printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n",
292 (long int) mpfr_get_emin ());
297 /* atan(+1) = Pi/4 underflows and is rounded down to 0 */
299 mpfr_atan (y, x, MPFR_RNDD);
300 if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ())
302 printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n",
303 (long int) mpfr_get_emin ());
319 mpfr_inits2 (4, x, y, z, (mpfr_ptr) 0);
321 /* Anything with NAN should be set to NAN */
322 mpfr_set_ui (y, 0, MPFR_RNDN);
324 mpfr_atan2 (z, y, x, MPFR_RNDN);
325 MPFR_ASSERTN (MPFR_IS_NAN (z));
327 mpfr_atan2 (z, y, x, MPFR_RNDN);
328 MPFR_ASSERTN (MPFR_IS_NAN (z));
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));
335 MPFR_CHANGE_SIGN (y);
336 mpfr_atan2 (z, y, x, MPFR_RNDN);
337 MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z));
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);
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);
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);
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);
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));
359 MPFR_CHANGE_SIGN (y);
360 mpfr_atan2 (z, y, x, MPFR_RNDN);
361 MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z));
364 mpfr_atan2 (z, y, x, MPFR_RNDN);
365 MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0);
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);
381 mpfr_atan2 (z, y, x, MPFR_RNDN);
382 MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z));
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 */
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 */
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);
423 mpfr_clears (x, y, z, (mpfr_ptr) 0);
426 /* from Christopher Creutzig, 18 Jul 2007 */
428 smallvals_atan2 (void)
433 mpfr_inits (a, x, y, (mpfr_ptr) 0);
434 mpfr_set_ui (y, 0, MPFR_RNDN);
436 mpfr_set_ui (x, 1, MPFR_RNDN);
437 /* y=-2^(-emin-1), x=1 */
439 mpfr_atan2 (a, y, x, MPFR_RNDD);
440 MPFR_ASSERTN (mpfr_equal_p (a, y));
442 mpfr_atan2 (a, y, x, MPFR_RNDU);
443 MPFR_ASSERTN (mpfr_zero_p (a) && MPFR_IS_NEG(a));
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);
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));
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));
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));
479 mpfr_set_emin (old_emin);
481 mpfr_clears (a, x, y, (mpfr_ptr) 0);
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. */
487 atan2_bug_20071003 (void)
491 mpfr_inits (a, x, y, z, (mpfr_ptr) 0);
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))
503 printf ("mpfr_atan2 fails on:\n");
508 printf ("Expected ");
515 mpfr_clears (a, x, y, z, (mpfr_ptr) 0);
518 /* Bug found on 2009-04-29 by Christopher Creutzig.
519 * With r6179: atan.c:62: MPFR assertion failed: r > n
522 atan2_different_prec (void)
530 mpfr_set_ui (x, 1, MPFR_RNDN);
531 mpfr_set_ui (y, 1, MPFR_RNDN);
533 mpfr_atan2 (a, y, x, MPFR_RNDN);
535 mpfr_clears (a, x, y, (mpfr_ptr) 0);
539 atan2_pow_of_2 (void)
543 int d[] = { 0, -1, 1 };
544 int ntests = sizeof (d) / sizeof (int);
552 mpfr_set_str_binary (g, "1100011000000011110011111001100110101000011010010011E-51");
554 for (i = 0; i < ntests; ++i)
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)
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");
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).
578 reduced_expo_range (void)
580 mpfr_exp_t emin, emax;
583 unsigned int flags, ex_flags;
585 emin = mpfr_get_emin ();
586 emax = mpfr_get_emax ();
588 mpfr_inits2 (12, x, y, ex_y, (mpfr_ptr) 0);
589 mpfr_set_str (x, "0.1e-5", 2, MPFR_RNDN);
594 inex = mpfr_atan (y, x, MPFR_RNDN);
595 flags = __gmpfr_flags;
596 mpfr_set_emin (emin);
597 mpfr_set_emax (emax);
599 mpfr_set_str (ex_y, "0.1e-5", 2, MPFR_RNDN);
601 ex_flags = MPFR_FLAGS_INEXACT;
603 if (SIGN (inex) != ex_inex || flags != ex_flags ||
604 ! mpfr_equal_p (y, ex_y))
606 printf ("Error in reduced_expo_range\non 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);
612 mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
613 printf ("\n inex = %d, flags = %u\n", SIGN (inex), flags);
617 mpfr_clears (x, y, ex_y, (mpfr_ptr) 0);
621 main (int argc, char *argv[])
629 atan2_bug_20071003 ();
630 atan2_different_prec ();
631 reduced_expo_range ();
633 test_generic_atan (2, 200, 17);
634 test_generic_atan2 (2, 200, 17);
635 test_generic_atan2_neg (2, 200, 17);
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);