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