1/* tjn -- test file for the Bessel function of first kind
2
3Copyright 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4Contributed by the Arenaire and Cacao projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <limits.h> /* for LONG_MAX */
26
27#include "mpfr-test.h"
28
29int
30main (int argc, char *argv[])
31{
32  mpfr_t x, y;
33  long n;
34
35  if (argc > 1)
36    {
37      mpfr_init2 (x, atoi (argv[1]));
38      mpfr_set_str (x, argv[3], 10, MPFR_RNDN);
39      mpfr_jn (x, atoi (argv[2]), x, MPFR_RNDN);
40      mpfr_out_str (stdout, 10, 10, x, MPFR_RNDN);
41      printf ("\n");
42      mpfr_clear (x);
43      return 0;
44    }
45
46  tests_start_mpfr ();
47
48  mpfr_init (x);
49  mpfr_init (y);
50
51  /* special values */
52  mpfr_set_nan (x);
53  mpfr_jn (y, 17, x, MPFR_RNDN);
54  MPFR_ASSERTN(mpfr_nan_p (y));
55
56  mpfr_set_inf (x, 1); /* +Inf */
57  mpfr_jn (y, 17, x, MPFR_RNDN);
58  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
59
60  mpfr_set_inf (x, -1); /* -Inf */
61  mpfr_jn (y, 17, x, MPFR_RNDN);
62  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
63
64  mpfr_set_ui (x, 0, MPFR_RNDN); /* +0 */
65  mpfr_jn (y, 0, x, MPFR_RNDN);
66  MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(+0)=1 */
67  mpfr_jn (y, 17, x, MPFR_RNDN);
68  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j17(+0)=+0 */
69  mpfr_jn (y, -17, x, MPFR_RNDN);
70  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG (y)); /* j-17(+0)=-0 */
71  mpfr_jn (y, 42, x, MPFR_RNDN);
72  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j42(+0)=+0 */
73
74  mpfr_set_ui (x, 0, MPFR_RNDN);
75  mpfr_neg (x, x, MPFR_RNDN); /* -0 */
76  mpfr_jn (y, 0, x, MPFR_RNDN);
77  MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(-0)=1 */
78  mpfr_jn (y, 17, x, MPFR_RNDN);
79  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG (y)); /* j17(-0)=-0 */
80  mpfr_jn (y, -17, x, MPFR_RNDN);
81  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j-17(-0)=+0 */
82  mpfr_jn (y, 42, x, MPFR_RNDN);
83  MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j42(-0)=+0 */
84
85  mpfr_set_prec (x, 53);
86  mpfr_set_prec (y, 53);
87
88  mpfr_set_ui (x, 1, MPFR_RNDN);
89  mpfr_jn (y, 0, x, MPFR_RNDN);
90  mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
91  if (mpfr_cmp (x, y))
92    {
93      printf ("Error in mpfr_jn for n=0, x=1, rnd=MPFR_RNDN\n");
94      printf ("Expected "); mpfr_dump (x);
95      printf ("Got      "); mpfr_dump (y);
96      exit (1);
97    }
98
99  mpfr_set_si (x, -1, MPFR_RNDN);
100  mpfr_jn (y, 0, x, MPFR_RNDN);
101  mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
102  if (mpfr_cmp (x, y))
103    {
104      printf ("Error in mpfr_jn for n=0, x=-1, rnd=MPFR_RNDN\n");
105      printf ("Expected "); mpfr_dump (x);
106      printf ("Got      "); mpfr_dump (y);
107      exit (1);
108    }
109
110  mpfr_set_ui (x, 1, MPFR_RNDN);
111  mpfr_jn (y, 1, x, MPFR_RNDN);
112  mpfr_set_str_binary (x, "0.0111000010100111001001111011101001011100001100011011");
113  if (mpfr_cmp (x, y))
114    {
115      printf ("Error in mpfr_jn for n=1, x=1, rnd=MPFR_RNDN\n");
116      printf ("Expected "); mpfr_dump (x);
117      printf ("Got      "); mpfr_dump (y);
118      exit (1);
119    }
120
121  mpfr_set_ui (x, 1, MPFR_RNDN);
122  mpfr_jn (y, 17, x, MPFR_RNDN);
123  mpfr_set_str_binary (x, "0.1100011111001010101001001001000110110000010001011E-65");
124  if (mpfr_cmp (x, y))
125    {
126      printf ("Error in mpfr_jn for n=17, x=1, rnd=MPFR_RNDN\n");
127      printf ("Expected "); mpfr_dump (x);
128      printf ("Got      "); mpfr_dump (y);
129      exit (1);
130    }
131
132  mpfr_set_ui (x, 1, MPFR_RNDN);
133  mpfr_jn (y, 42, x, MPFR_RNDN);
134  mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
135  if (mpfr_cmp (x, y))
136    {
137      printf ("Error in mpfr_jn for n=42, x=1, rnd=MPFR_RNDN\n");
138      printf ("Expected "); mpfr_dump (x);
139      printf ("Got      "); mpfr_dump (y);
140      exit (1);
141    }
142
143  mpfr_set_ui (x, 1, MPFR_RNDN);
144  mpfr_jn (y, -42, x, MPFR_RNDN);
145  mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
146  if (mpfr_cmp (x, y))
147    {
148      printf ("Error in mpfr_jn for n=-42, x=1, rnd=MPFR_RNDN\n");
149      printf ("Expected "); mpfr_dump (x);
150      printf ("Got      "); mpfr_dump (y);
151      exit (1);
152    }
153
154  mpfr_set_si (x, -1, MPFR_RNDN);
155  mpfr_jn (y, 42, x, MPFR_RNDN);
156  mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
157  if (mpfr_cmp (x, y))
158    {
159      printf ("Error in mpfr_jn for n=42, x=-1, rnd=MPFR_RNDN\n");
160      printf ("Expected "); mpfr_dump (x);
161      printf ("Got      "); mpfr_dump (y);
162      exit (1);
163    }
164
165  mpfr_set_si (x, -1, MPFR_RNDN);
166  mpfr_jn (y, -42, x, MPFR_RNDN);
167  mpfr_set_str_binary (x, "0.10000111100011010100111011100111101101000100000001001E-211");
168  if (mpfr_cmp (x, y))
169    {
170      printf ("Error in mpfr_jn for n=-42, x=-1, rnd=MPFR_RNDN\n");
171      printf ("Expected "); mpfr_dump (x);
172      printf ("Got      "); mpfr_dump (y);
173      exit (1);
174    }
175
176  mpfr_set_ui (x, 17, MPFR_RNDN);
177  mpfr_jn (y, 4, x, MPFR_RNDN);
178  mpfr_set_str_binary (x, "-0.0001110001011001100010100111100111100000111110111011111");
179  if (mpfr_cmp (x, y))
180    {
181      printf ("Error in mpfr_jn for n=4, x=17, rnd=MPFR_RNDN\n");
182      printf ("Expected "); mpfr_dump (x);
183      printf ("Got      "); mpfr_dump (y);
184      exit (1);
185    }
186
187  mpfr_set_ui (x, 17, MPFR_RNDN);
188  mpfr_jn (y, 16, x, MPFR_RNDN);
189  mpfr_set_str_binary (x, "0.0011101111100111101111010100000111111001111001001010011");
190  if (mpfr_cmp (x, y))
191    {
192      printf ("Error in mpfr_jn for n=16, x=17, rnd=MPFR_RNDN\n");
193      printf ("Expected "); mpfr_dump (x);
194      printf ("Got      "); mpfr_dump (y);
195      exit (1);
196    }
197
198  mpfr_set_ui (x, 17, MPFR_RNDN);
199  mpfr_jn (y, 256, x, MPFR_RNDN);
200  mpfr_set_str_binary (x, "0.11111101111100110000000010111101101011101011110001011E-894");
201  if (mpfr_cmp (x, y))
202    {
203      printf ("Error in mpfr_jn for n=256, x=17, rnd=MPFR_RNDN\n");
204      printf ("Expected "); mpfr_dump (x);
205      printf ("Got      "); mpfr_dump (y);
206      exit (1);
207    }
208
209  mpfr_set_ui (x, 17, MPFR_RNDN);
210  mpfr_jn (y, 65536, x, MPFR_RNDN);
211  mpfr_set_str_binary (x, "100010010010011010110101100001000100011100010111011E-751747");
212  if (mpfr_cmp (x, y))
213    {
214      printf ("Error in mpfr_jn for n=65536, x=17, rnd=MPFR_RNDN\n");
215      printf ("Expected "); mpfr_dump (x);
216      printf ("Got      "); mpfr_dump (y);
217      exit (1);
218    }
219
220  mpfr_set_ui (x, 17, MPFR_RNDN);
221  mpfr_jn (y, 131072, x, MPFR_RNDN);
222  mpfr_set_str_binary (x, "1000001001110011111001110110000010011010000001001101E-1634508");
223  if (mpfr_cmp (x, y))
224    {
225      printf ("Error in mpfr_jn for n=131072, x=17, rnd=MPFR_RNDN\n");
226      printf ("Expected "); mpfr_dump (x);
227      printf ("Got      "); mpfr_dump (y);
228      exit (1);
229    }
230
231  mpfr_set_ui (x, 17, MPFR_RNDN);
232  mpfr_jn (y, 262144, x, MPFR_RNDN);
233  mpfr_set_str_binary (x, "1010011011000100111011001011110001000010000010111111E-3531100");
234  if (mpfr_cmp (x, y))
235    {
236      printf ("Error in mpfr_jn for n=262144, x=17, rnd=MPFR_RNDN\n");
237      printf ("Expected "); mpfr_dump (x);
238      printf ("Got      "); mpfr_dump (y);
239      exit (1);
240    }
241
242  mpfr_set_ui (x, 17, MPFR_RNDN);
243  mpfr_jn (y, 524288, x, MPFR_RNDN);
244  mpfr_set_str_binary (x, "110000001010001111011011000011001011010100010001011E-7586426");
245  if (mpfr_cmp (x, y))
246    {
247      printf ("Error in mpfr_jn for n=524288, x=17, rnd=MPFR_RNDN\n");
248      printf ("Expected "); mpfr_dump (x);
249      printf ("Got      "); mpfr_dump (y);
250      exit (1);
251    }
252
253  n = LONG_MAX;
254  /* ensures n is odd */
255  if (n % 2 == 0)
256    n --;
257  mpfr_set_ui (x, 17, MPFR_RNDN);
258  mpfr_jn (y, n, x, MPFR_RNDN);
259  mpfr_set_str_binary (x, "0.0");
260  if (mpfr_cmp (x, y))
261    {
262      printf ("Error in mpfr_jn for n=%ld, x=17, rnd=MPFR_RNDN\n", n);
263      printf ("Expected "); mpfr_dump (x);
264      printf ("Got      "); mpfr_dump (y);
265      exit (1);
266    }
267
268  mpfr_set_si (x, -17, MPFR_RNDN);
269  mpfr_jn (y, n, x, MPFR_RNDN);
270  mpfr_set_str_binary (x, "-0.0");
271  if (mpfr_cmp (x, y))
272    {
273      printf ("Error in mpfr_jn for n=%ld, x=-17, rnd=MPFR_RNDN\n", n);
274      printf ("Expected "); mpfr_dump (x);
275      printf ("Got      "); mpfr_dump (y);
276      exit (1);
277    }
278
279  mpfr_set_ui (x, 17, MPFR_RNDN);
280  mpfr_jn (y, -n, x, MPFR_RNDN);
281  mpfr_set_str_binary (x, "-0.0");
282  if (mpfr_cmp (x, y))
283    {
284      printf ("Error in mpfr_jn for n=%ld, x=17, rnd=MPFR_RNDN\n", -n);
285      printf ("Expected "); mpfr_dump (x);
286      printf ("Got      "); mpfr_dump (y);
287      exit (1);
288    }
289
290  mpfr_set_si (x, -17, MPFR_RNDN);
291  mpfr_jn (y, -n, x, MPFR_RNDN);
292  mpfr_set_str_binary (x, "0.0");
293  if (mpfr_cmp (x, y))
294    {
295      printf ("Error in mpfr_jn for n=%ld, x=-17, rnd=MPFR_RNDN\n", -n);
296      printf ("Expected "); mpfr_dump (x);
297      printf ("Got      "); mpfr_dump (y);
298      exit (1);
299    }
300
301  mpfr_clear (x);
302  mpfr_clear (y);
303
304  tests_end_mpfr ();
305
306  return 0;
307}
308