xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tacos.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /* Test file for mpfr_acos.
2 
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by Mathieu Dutour.
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 #define TEST_FUNCTION mpfr_acos
29 #include "tgeneric.c"
30 
31 static void
32 special (void)
33 {
34   mpfr_t x, y;
35   int inex1, inex2;
36 
37   mpfr_init2 (x, 32);
38   mpfr_init2 (y, 32);
39 
40   mpfr_set_str_binary (x, "0.10001000001001011000100001E-6");
41   mpfr_acos (y, x, MPFR_RNDN);
42   mpfr_set_str_binary (x, "1.10001111111111110001110110001");
43   if (mpfr_cmp (x, y))
44     {
45       printf ("Error in mpfr_acos (1)\n");
46       exit (1);
47     }
48 
49   mpfr_set_str_binary (x, "-0.01101011110111100111010011001011");
50   mpfr_acos (y, x, MPFR_RNDZ);
51   mpfr_set_str_binary (x, "10.0000000101111000011101000101");
52   if (mpfr_cmp (x, y))
53     {
54       printf ("Error in mpfr_acos (2)\n");
55       mpfr_print_binary (y); printf ("\n");
56       exit (1);
57     }
58 
59   mpfr_set_prec (x, 2);
60   mpfr_set_ui (x, 0, MPFR_RNDN);
61   inex1 = mpfr_acos (x, x, MPFR_RNDN); /* Pi/2 */
62   inex2 = mpfr_const_pi (x, MPFR_RNDN);
63   if (inex1 != inex2)
64     {
65       printf ("Error in mpfr_acos (3) for prec=2\n");
66       exit (1);
67     }
68 
69   mpfr_clear (y);
70   mpfr_clear (x);
71 }
72 
73 static void
74 special_overflow (void)
75 {
76   mpfr_t x, y;
77   mpfr_exp_t emin, emax;
78 
79   emin = mpfr_get_emin ();
80   emax = mpfr_get_emax ();
81 
82   set_emin (-125);
83   set_emax (128);
84   mpfr_init2 (x, 24);
85   mpfr_init2 (y, 48);
86   mpfr_set_str_binary (x, "0.101100100000000000110100E0");
87   mpfr_acos (y, x, MPFR_RNDN);
88   if (mpfr_cmp_str (y, "0.110011010100101111000100111010111011010000001001E0",
89                     2, MPFR_RNDN))
90     {
91       printf("Special Overflow error.\n");
92       mpfr_dump (y);
93       exit (1);
94     }
95   mpfr_clear (y);
96   mpfr_clear (x);
97   set_emin (emin);
98   set_emax (emax);
99 }
100 
101 int
102 main (void)
103 {
104   mpfr_t x, y;
105   int r;
106 
107   tests_start_mpfr ();
108 
109   special_overflow ();
110   special ();
111 
112   mpfr_init (x);
113   mpfr_init (y);
114 
115   MPFR_SET_NAN(x);
116   mpfr_acos (y, x, MPFR_RNDN);
117   if (mpfr_nan_p(y) == 0)
118     {
119       printf ("Error: acos(NaN) != NaN\n");
120       exit (1);
121     }
122 
123   mpfr_set_ui (x, 2, MPFR_RNDN);
124   mpfr_acos (y, x, MPFR_RNDN);
125   if (mpfr_nan_p(y) == 0)
126     {
127       printf ("Error: acos(2) != NaN\n");
128       exit (1);
129     }
130 
131   mpfr_set_si (x, -2, MPFR_RNDN);
132   mpfr_acos (y, x, MPFR_RNDN);
133   if (mpfr_nan_p(y) == 0)
134     {
135       printf ("Error: acos(-2) != NaN\n");
136       exit (1);
137     }
138 
139   /* acos (1) = 0 */
140   mpfr_set_ui (x, 1, MPFR_RNDN);
141   mpfr_acos (y, x, MPFR_RNDN);
142   if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0)
143     {
144       printf ("Error: acos(1) != +0.0\n");
145       exit (1);
146     }
147 
148   /* acos (0) = Pi/2 */
149   for (r = 0; r < MPFR_RND_MAX; r++)
150     {
151       mpfr_set_ui (x, 0, MPFR_RNDN); /* exact */
152       mpfr_acos (y, x, (mpfr_rnd_t) r);
153       mpfr_const_pi (x, (mpfr_rnd_t) r);
154       mpfr_div_2exp (x, x, 1, MPFR_RNDN); /* exact */
155       if (mpfr_cmp (x, y))
156         {
157           printf ("Error: acos(0) != Pi/2 for rnd=%s\n",
158                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
159           exit (1);
160         }
161     }
162 
163   /* acos (-1) = Pi */
164   for (r = 0; r < MPFR_RND_MAX; r++)
165     {
166       mpfr_set_si (x, -1, MPFR_RNDN); /* exact */
167       mpfr_acos (y, x, (mpfr_rnd_t) r);
168       mpfr_const_pi (x, (mpfr_rnd_t) r);
169       if (mpfr_cmp (x, y))
170         {
171           printf ("Error: acos(1) != Pi for rnd=%s\n",
172                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
173           exit (1);
174         }
175     }
176 
177   test_generic (2, 100, 7);
178 
179   mpfr_clear (x);
180   mpfr_clear (y);
181 
182   data_check ("data/acos", mpfr_acos, "mpfr_acos");
183   bad_cases (mpfr_acos, mpfr_cos, "mpfr_acos", 0, -40, 2, 4, 128, 800, 30);
184 
185   tests_end_mpfr ();
186   return 0;
187 }
188