1 /* Test file for mpfr_acosu.
2
3 Copyright 2021-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_acosu
26 #define ULONG_ARG2
27 #include "tgeneric.c"
28
29 int
main(void)30 main (void)
31 {
32 mpfr_t x, y;
33 int r, inex;
34 int unsigned long u;
35
36 tests_start_mpfr ();
37
38 mpfr_init (x);
39 mpfr_init (y);
40
41 /* check singular cases */
42 MPFR_SET_NAN(x);
43 mpfr_acosu (y, x, 1, MPFR_RNDN);
44 if (mpfr_nan_p (y) == 0)
45 {
46 printf ("Error: acosu (NaN, 1) != NaN\n");
47 exit (1);
48 }
49
50 mpfr_set_inf (x, 1);
51 mpfr_acosu (y, x, 1, MPFR_RNDN);
52 if (mpfr_nan_p (y) == 0)
53 {
54 printf ("Error: acosu (+Inf, 1) != NaN\n");
55 exit (1);
56 }
57
58 mpfr_set_inf (x, -1);
59 mpfr_acosu (y, x, 1, MPFR_RNDN);
60 if (mpfr_nan_p (y) == 0)
61 {
62 printf ("Error: acosu (-Inf, 1) != NaN\n");
63 exit (1);
64 }
65
66 /* acosu (0,u) = u/4 */
67 RND_LOOP (r)
68 {
69 mpfr_set_ui (x, 0, MPFR_RNDN); /* exact */
70 mpfr_acosu (y, x, 17, (mpfr_rnd_t) r);
71 mpfr_set_ui_2exp (x, 17, -2, MPFR_RNDN);
72 if (!mpfr_equal_p (x, y))
73 {
74 printf ("Error: acosu(0,17) != 17/4 for rnd=%s\n",
75 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
76 exit (1);
77 }
78 }
79
80 /* check case |x| > 1 */
81 mpfr_set_ui (x, 2, MPFR_RNDN);
82 mpfr_acosu (y, x, 1, MPFR_RNDN);
83 if (mpfr_nan_p (y) == 0)
84 {
85 printf ("Error: acosu (2, 1) != NaN\n");
86 exit (1);
87 }
88
89 mpfr_set_si (x, -2, MPFR_RNDN);
90 mpfr_acosu (y, x, 1, MPFR_RNDN);
91 if (mpfr_nan_p (y) == 0)
92 {
93 printf ("Error: acosu (-2, 1) != NaN\n");
94 exit (1);
95 }
96
97 /* check case |x| > 1 with u=0 */
98 mpfr_set_ui (x, 2, MPFR_RNDN);
99 mpfr_acosu (y, x, 0, MPFR_RNDN);
100 if (mpfr_nan_p (y) == 0)
101 {
102 printf ("Error: acosu (2, 0) != NaN\n");
103 exit (1);
104 }
105
106 mpfr_set_si (x, -2, MPFR_RNDN);
107 mpfr_acosu (y, x, 0, MPFR_RNDN);
108 if (mpfr_nan_p (y) == 0)
109 {
110 printf ("Error: acosu (-2, 0) != NaN\n");
111 exit (1);
112 }
113
114 /* check case u=0 */
115 mpfr_set_ui_2exp (x, 1, -1, MPFR_RNDN);
116 mpfr_acosu (y, x, 0, MPFR_RNDN);
117 if (!mpfr_zero_p (y) || MPFR_SIGN(y) < 0)
118 {
119 printf ("Error: acosu (1/2, 0) != +0\n");
120 printf ("got: "); mpfr_dump (y);
121 exit (1);
122 }
123
124 /* acosu (1,u) = +0 */
125 mpfr_set_ui (x, 1, MPFR_RNDN);
126 mpfr_acosu (y, x, 1, MPFR_RNDN);
127 if (MPFR_NOTZERO (y) || MPFR_IS_NEG (y))
128 {
129 printf ("Error: acosu(1,1) != +0.0\n");
130 exit (1);
131 }
132
133 /* acosu (-1,u) = u/2 */
134 RND_LOOP (r)
135 {
136 mpfr_set_si (x, -1, MPFR_RNDN); /* exact */
137 mpfr_acosu (y, x, 17, (mpfr_rnd_t) r);
138 mpfr_set_ui_2exp (x, 17, -1, MPFR_RNDN);
139 if (!mpfr_equal_p (x, y))
140 {
141 printf ("Error: acosu(1,17) != 17/2 for rnd=%s\n",
142 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
143 exit (1);
144 }
145 }
146
147 /* acosu (1/2,u) = u/6 */
148 for (u = 1; u < 100; u++)
149 RND_LOOP (r)
150 {
151 mpfr_set_ui_2exp (x, 1, -1, MPFR_RNDN); /* exact */
152 mpfr_acosu (y, x, u, (mpfr_rnd_t) r);
153 inex = mpfr_set_ui (x, u, MPFR_RNDN);
154 MPFR_ASSERTN(inex == 0);
155 inex = mpfr_div_ui (x, x, 6, (mpfr_rnd_t) r);
156 if ((r != MPFR_RNDF || inex == 0) && !mpfr_equal_p (x, y))
157 {
158 printf ("Error: acosu(1/2,u) != u/6 for u=%lu and rnd=%s\n",
159 u, mpfr_print_rnd_mode ((mpfr_rnd_t) r));
160 printf ("got: "); mpfr_dump (y);
161 exit (1);
162 }
163 }
164
165 /* acosu (-1/2,u) = u/3 */
166 for (u = 1; u < 100; u++)
167 RND_LOOP (r)
168 {
169 mpfr_set_si_2exp (x, -1, -1, MPFR_RNDN); /* exact */
170 mpfr_acosu (y, x, u, (mpfr_rnd_t) r);
171 inex = mpfr_set_ui (x, u, MPFR_RNDN);
172 MPFR_ASSERTN(inex == 0);
173 inex = mpfr_div_ui (x, x, 3, (mpfr_rnd_t) r);
174 if ((r != MPFR_RNDF || inex == 0) && !mpfr_equal_p (x, y))
175 {
176 printf ("Error: acosu(-1/2,u) != u/3 for u=%lu and rnd=%s\n",
177 u, mpfr_print_rnd_mode ((mpfr_rnd_t) r));
178 printf ("got: "); mpfr_dump (y);
179 exit (1);
180 }
181 }
182
183 test_generic (MPFR_PREC_MIN, 100, 100);
184
185 mpfr_clear (x);
186 mpfr_clear (y);
187
188 tests_end_mpfr ();
189 return 0;
190 }
191