1 /* tjn -- test file for the Bessel function of first kind
2 
3 Copyright 2007-2020 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 /* mpfr_jn doesn't terminate. Bug reported by Alex Coplan on 2020-07-03.
26  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96044
27  * Note: This test is enabled only with MPFR_CHECK_EXPENSIVE. But do not
28  * use that with --enable-assert=full, as it may take more than 1 hour!
29  */
30 static void
bug20200703(void)31 bug20200703 (void)
32 {
33   mpfr_t x, y;
34 
35   mpfr_init (x);
36   mpfr_init (y);
37   mpfr_set_si (x, 73333, MPFR_RNDN);
38   mpfr_jn (y, 73333, x, MPFR_RNDN);
39   mpfr_set_si (x, 733333, MPFR_RNDN);
40   mpfr_jn (y, 733333, x, MPFR_RNDN);
41   mpfr_clear (x);
42   mpfr_clear (y);
43 }
44 
45 int
main(int argc,char * argv[])46 main (int argc, char *argv[])
47 {
48   mpfr_t x, y;
49   long n;
50 
51   if (argc > 1)
52     {
53       mpfr_init2 (x, atoi (argv[1]));
54       mpfr_set_str (x, argv[3], 10, MPFR_RNDN);
55       mpfr_jn (x, atoi (argv[2]), x, MPFR_RNDN);
56       mpfr_out_str (stdout, 10, 10, x, MPFR_RNDN);
57       printf ("\n");
58       mpfr_clear (x);
59       return 0;
60     }
61 
62   tests_start_mpfr ();
63 
64   if (getenv ("MPFR_CHECK_EXPENSIVE") != NULL)
65     bug20200703 ();
66 
67   mpfr_init (x);
68   mpfr_init (y);
69 
70   /* special values */
71   mpfr_set_nan (x);
72   mpfr_jn (y, 17, x, MPFR_RNDN);
73   MPFR_ASSERTN(mpfr_nan_p (y));
74 
75   mpfr_set_inf (x, 1); /* +Inf */
76   mpfr_jn (y, 17, x, MPFR_RNDN);
77   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
78 
79   mpfr_set_inf (x, -1); /* -Inf */
80   mpfr_jn (y, 17, x, MPFR_RNDN);
81   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
82 
83   mpfr_set_ui (x, 0, MPFR_RNDN); /* +0 */
84   mpfr_jn (y, 0, x, MPFR_RNDN);
85   MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(+0)=1 */
86   mpfr_jn (y, 17, x, MPFR_RNDN);
87   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j17(+0)=+0 */
88   mpfr_jn (y, -17, x, MPFR_RNDN);
89   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG (y)); /* j-17(+0)=-0 */
90   mpfr_jn (y, 42, x, MPFR_RNDN);
91   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j42(+0)=+0 */
92 
93   mpfr_set_ui (x, 0, MPFR_RNDN);
94   mpfr_neg (x, x, MPFR_RNDN); /* -0 */
95   mpfr_jn (y, 0, x, MPFR_RNDN);
96   MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(-0)=1 */
97   mpfr_jn (y, 17, x, MPFR_RNDN);
98   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG (y)); /* j17(-0)=-0 */
99   mpfr_jn (y, -17, x, MPFR_RNDN);
100   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j-17(-0)=+0 */
101   mpfr_jn (y, 42, x, MPFR_RNDN);
102   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j42(-0)=+0 */
103 
104   mpfr_set_prec (x, 53);
105   mpfr_set_prec (y, 53);
106 
107   mpfr_set_ui (x, 1, MPFR_RNDN);
108   mpfr_jn (y, 0, x, MPFR_RNDN);
109   mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
110   if (mpfr_cmp (x, y))
111     {
112       printf ("Error in mpfr_jn for n=0, x=1, rnd=MPFR_RNDN\n");
113       printf ("Expected "); mpfr_dump (x);
114       printf ("Got      "); mpfr_dump (y);
115       exit (1);
116     }
117 
118   mpfr_set_si (x, -1, MPFR_RNDN);
119   mpfr_jn (y, 0, x, MPFR_RNDN);
120   mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
121   if (mpfr_cmp (x, y))
122     {
123       printf ("Error in mpfr_jn for n=0, x=-1, rnd=MPFR_RNDN\n");
124       printf ("Expected "); mpfr_dump (x);
125       printf ("Got      "); mpfr_dump (y);
126       exit (1);
127     }
128 
129   mpfr_set_ui (x, 1, MPFR_RNDN);
130   mpfr_jn (y, 1, x, MPFR_RNDN);
131   mpfr_set_str_binary (x, "0.0111000010100111001001111011101001011100001100011011");
132   if (mpfr_cmp (x, y))
133     {
134       printf ("Error in mpfr_jn for n=1, x=1, rnd=MPFR_RNDN\n");
135       printf ("Expected "); mpfr_dump (x);
136       printf ("Got      "); mpfr_dump (y);
137       exit (1);
138     }
139 
140   mpfr_set_ui (x, 1, MPFR_RNDN);
141   mpfr_jn (y, 17, x, MPFR_RNDN);
142   mpfr_set_str_binary (x, "0.1100011111001010101001001001000110110000010001011E-65");
143   if (mpfr_cmp (x, y))
144     {
145       printf ("Error in mpfr_jn for n=17, x=1, rnd=MPFR_RNDN\n");
146       printf ("Expected "); mpfr_dump (x);
147       printf ("Got      "); mpfr_dump (y);
148       exit (1);
149     }
150 
151   mpfr_set_ui (x, 1, MPFR_RNDN);
152   mpfr_jn (y, 42, x, MPFR_RNDN);
153   mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
154   if (mpfr_cmp (x, y))
155     {
156       printf ("Error in mpfr_jn for n=42, x=1, rnd=MPFR_RNDN\n");
157       printf ("Expected "); mpfr_dump (x);
158       printf ("Got      "); mpfr_dump (y);
159       exit (1);
160     }
161 
162   mpfr_set_ui (x, 1, MPFR_RNDN);
163   mpfr_jn (y, -42, x, MPFR_RNDN);
164   mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
165   if (mpfr_cmp (x, y))
166     {
167       printf ("Error in mpfr_jn for n=-42, x=1, rnd=MPFR_RNDN\n");
168       printf ("Expected "); mpfr_dump (x);
169       printf ("Got      "); mpfr_dump (y);
170       exit (1);
171     }
172 
173   mpfr_set_si (x, -1, MPFR_RNDN);
174   mpfr_jn (y, 42, x, MPFR_RNDN);
175   mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
176   if (mpfr_cmp (x, y))
177     {
178       printf ("Error in mpfr_jn for n=42, x=-1, rnd=MPFR_RNDN\n");
179       printf ("Expected "); mpfr_dump (x);
180       printf ("Got      "); mpfr_dump (y);
181       exit (1);
182     }
183 
184   mpfr_set_si (x, -1, MPFR_RNDN);
185   mpfr_jn (y, -42, x, MPFR_RNDN);
186   mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
187   if (mpfr_cmp (x, y))
188     {
189       printf ("Error in mpfr_jn for n=-42, x=-1, rnd=MPFR_RNDN\n");
190       printf ("Expected "); mpfr_dump (x);
191       printf ("Got      "); mpfr_dump (y);
192       exit (1);
193     }
194 
195   mpfr_set_ui (x, 17, MPFR_RNDN);
196   mpfr_jn (y, 4, x, MPFR_RNDN);
197   mpfr_set_str_binary (x, "-0.0001110001011001100010100111100111100000111110111011111");
198   if (mpfr_cmp (x, y))
199     {
200       printf ("Error in mpfr_jn for n=4, x=17, rnd=MPFR_RNDN\n");
201       printf ("Expected "); mpfr_dump (x);
202       printf ("Got      "); mpfr_dump (y);
203       exit (1);
204     }
205 
206   mpfr_set_ui (x, 17, MPFR_RNDN);
207   mpfr_jn (y, 16, x, MPFR_RNDN);
208   mpfr_set_str_binary (x, "0.0011101111100111101111010100000111111001111001001010011");
209   if (mpfr_cmp (x, y))
210     {
211       printf ("Error in mpfr_jn for n=16, x=17, rnd=MPFR_RNDN\n");
212       printf ("Expected "); mpfr_dump (x);
213       printf ("Got      "); mpfr_dump (y);
214       exit (1);
215     }
216 
217   mpfr_set_ui (x, 17, MPFR_RNDN);
218   mpfr_jn (y, 256, x, MPFR_RNDN);
219   mpfr_set_str_binary (x, "0.11111101111100110000000010111101101011101011110001011E-894");
220   if (mpfr_cmp (x, y))
221     {
222       printf ("Error in mpfr_jn for n=256, x=17, rnd=MPFR_RNDN\n");
223       printf ("Expected "); mpfr_dump (x);
224       printf ("Got      "); mpfr_dump (y);
225       exit (1);
226     }
227 
228   mpfr_set_ui (x, 17, MPFR_RNDN);
229   mpfr_jn (y, 65536, x, MPFR_RNDN);
230   mpfr_set_str_binary (x, "100010010010011010110101100001000100011100010111011E-751747");
231   if (mpfr_cmp (x, y))
232     {
233       printf ("Error in mpfr_jn for n=65536, x=17, rnd=MPFR_RNDN\n");
234       printf ("Expected "); mpfr_dump (x);
235       printf ("Got      "); mpfr_dump (y);
236       exit (1);
237     }
238 
239   mpfr_set_ui (x, 17, MPFR_RNDN);
240   mpfr_jn (y, 131072, x, MPFR_RNDN);
241   mpfr_set_str_binary (x, "1000001001110011111001110110000010011010000001001101E-1634508");
242   if (mpfr_cmp (x, y))
243     {
244       printf ("Error in mpfr_jn for n=131072, x=17, rnd=MPFR_RNDN\n");
245       printf ("Expected "); mpfr_dump (x);
246       printf ("Got      "); mpfr_dump (y);
247       exit (1);
248     }
249 
250   mpfr_set_ui (x, 17, MPFR_RNDN);
251   mpfr_jn (y, 262144, x, MPFR_RNDN);
252   mpfr_set_str_binary (x, "1010011011000100111011001011110001000010000010111111E-3531100");
253   if (mpfr_cmp (x, y))
254     {
255       printf ("Error in mpfr_jn for n=262144, x=17, rnd=MPFR_RNDN\n");
256       printf ("Expected "); mpfr_dump (x);
257       printf ("Got      "); mpfr_dump (y);
258       exit (1);
259     }
260 
261   mpfr_set_ui (x, 17, MPFR_RNDN);
262   mpfr_jn (y, 524288, x, MPFR_RNDN);
263   mpfr_set_str_binary (x, "110000001010001111011011000011001011010100010001011E-7586426");
264   if (mpfr_cmp (x, y))
265     {
266       printf ("Error in mpfr_jn for n=524288, x=17, rnd=MPFR_RNDN\n");
267       printf ("Expected "); mpfr_dump (x);
268       printf ("Got      "); mpfr_dump (y);
269       exit (1);
270     }
271 
272   n = LONG_MAX;
273   /* ensures n is odd */
274   if (n % 2 == 0)
275     n --;
276   mpfr_set_ui (x, 17, MPFR_RNDN);
277   mpfr_jn (y, n, x, MPFR_RNDN);
278   mpfr_set_str_binary (x, "0.0");
279   if (mpfr_cmp (x, y))
280     {
281       printf ("Error in mpfr_jn for n=%ld, x=17, rnd=MPFR_RNDN\n", n);
282       printf ("Expected "); mpfr_dump (x);
283       printf ("Got      "); mpfr_dump (y);
284       exit (1);
285     }
286 
287   mpfr_set_si (x, -17, MPFR_RNDN);
288   mpfr_jn (y, n, x, MPFR_RNDN);
289   mpfr_set_str_binary (x, "-0.0");
290   if (mpfr_cmp (x, y))
291     {
292       printf ("Error in mpfr_jn for n=%ld, x=-17, rnd=MPFR_RNDN\n", n);
293       printf ("Expected "); mpfr_dump (x);
294       printf ("Got      "); mpfr_dump (y);
295       exit (1);
296     }
297 
298   mpfr_set_ui (x, 17, MPFR_RNDN);
299   mpfr_jn (y, -n, x, MPFR_RNDN);
300   mpfr_set_str_binary (x, "-0.0");
301   if (mpfr_cmp (x, y))
302     {
303       printf ("Error in mpfr_jn for n=%ld, x=17, rnd=MPFR_RNDN\n", -n);
304       printf ("Expected "); mpfr_dump (x);
305       printf ("Got      "); mpfr_dump (y);
306       exit (1);
307     }
308 
309   mpfr_set_si (x, -17, MPFR_RNDN);
310   mpfr_jn (y, -n, x, MPFR_RNDN);
311   mpfr_set_str_binary (x, "0.0");
312   if (mpfr_cmp (x, y))
313     {
314       printf ("Error in mpfr_jn for n=%ld, x=-17, rnd=MPFR_RNDN\n", -n);
315       printf ("Expected "); mpfr_dump (x);
316       printf ("Got      "); mpfr_dump (y);
317       exit (1);
318     }
319 
320   mpfr_clear (x);
321   mpfr_clear (y);
322 
323   tests_end_mpfr ();
324 
325   return 0;
326 }
327