xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tj0.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* tj0 -- test file for the Bessel function of first kind (order 0)
2 
3 Copyright 2007-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_j0
26 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 8, RANDS)
27 #define REDUCE_EMAX 262143 /* otherwise arg. reduction is too expensive */
28 #include "tgeneric.c"
29 
30 /* bug found in revision 14399 with GMP_CHECK_RANDOMIZE=1612721106588971 */
31 static void
bug20210208(void)32 bug20210208 (void)
33 {
34   mpfr_t x, y;
35   int inex;
36 
37   mpfr_init2 (x, 79);
38   mpfr_init2 (y, 1);
39   mpfr_set_str (x, "2.552495117262005805960565e+02", 10, MPFR_RNDN);
40   mpfr_clear_flags ();
41   inex = mpfr_j0 (y, x, MPFR_RNDZ);
42   MPFR_ASSERTN (mpfr_cmp_si_2exp (y, -1, -5) == 0);
43   MPFR_ASSERTN (inex > 0);
44   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
45   mpfr_clear (x);
46   mpfr_clear (y);
47 }
48 
49 int
main(int argc,char * argv[])50 main (int argc, char *argv[])
51 {
52   mpfr_t x, y;
53   int inex;
54 
55   tests_start_mpfr ();
56 
57   bug20210208 ();
58 
59   mpfr_init (x);
60   mpfr_init (y);
61 
62   /* special values */
63   mpfr_set_nan (x);
64   mpfr_j0 (y, x, MPFR_RNDN);
65   MPFR_ASSERTN(mpfr_nan_p (y));
66 
67   mpfr_set_inf (x, 1); /* +Inf */
68   mpfr_j0 (y, x, MPFR_RNDN);
69   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
70 
71   mpfr_set_inf (x, -1); /* -Inf */
72   mpfr_j0 (y, x, MPFR_RNDN);
73   MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y));
74 
75   mpfr_set_ui (x, 0, MPFR_RNDN); /* +0 */
76   mpfr_j0 (y, x, MPFR_RNDN);
77   MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(+0)=1 */
78 
79   mpfr_set_ui (x, 0, MPFR_RNDN);
80   mpfr_neg (x, x, MPFR_RNDN); /* -0 */
81   mpfr_j0 (y, x, MPFR_RNDN);
82   MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); /* j0(-0)=1 */
83 
84   mpfr_set_prec (x, 53);
85   mpfr_set_prec (y, 53);
86 
87   mpfr_set_ui (x, 1, MPFR_RNDN);
88   mpfr_j0 (y, x, MPFR_RNDN);
89   mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
90   if (mpfr_cmp (x, y))
91     {
92       printf ("Error in mpfr_j0 for x=1, rnd=MPFR_RNDN\n");
93       printf ("Expected "); mpfr_dump (x);
94       printf ("Got      "); mpfr_dump (y);
95       exit (1);
96     }
97 
98   mpfr_set_si (x, -1, MPFR_RNDN);
99   mpfr_j0 (y, x, MPFR_RNDN);
100   mpfr_set_str_binary (x, "0.1100001111100011111111101101111010111101110001111");
101   if (mpfr_cmp (x, y))
102     {
103       printf ("Error in mpfr_j0 for x=-1, rnd=MPFR_RNDN\n");
104       printf ("Expected "); mpfr_dump (x);
105       printf ("Got      "); mpfr_dump (y);
106       exit (1);
107     }
108 
109   /* Bug reported on 2007-07-03 by Sisyphus (assertion failed in r4619) */
110   mpfr_set_si (x, 70000, MPFR_RNDN);
111   mpfr_j0 (y, x, MPFR_RNDN);
112 
113   /* Bug reported by Kevin Rauch on 27 Oct 2007 */
114   mpfr_set_prec (x, 7);
115   mpfr_set_prec (y, 7);
116   mpfr_set_si (x, -100, MPFR_RNDN);
117   mpfr_j0 (y, x, MPFR_RNDN);
118   MPFR_ASSERTN (! mpfr_nan_p (y) && mpfr_cmp_ui_2exp (y, 41, -11) == 0);
119 
120   /* Bug reported by Fredrik Johansson on 19 Jan 2016 */
121   mpfr_set_prec (x, 53);
122   mpfr_set_str (x, "0x4.3328p+0", 0, MPFR_RNDN);
123   mpfr_set_prec (y, 2);
124   mpfr_j0 (y, x, MPFR_RNDD);
125   /* y should be -0.5 */
126   MPFR_ASSERTN (! mpfr_nan_p (y) && mpfr_cmp_si_2exp (y, -1, -1) == 0);
127   mpfr_set_prec (y, 3);
128   mpfr_j0 (y, x, MPFR_RNDD);
129   /* y should be -0.4375 */
130   MPFR_ASSERTN (! mpfr_nan_p (y) && mpfr_cmp_si_2exp (y, -7, -4) == 0);
131 
132   /* Case for which s = 0 in mpfr_jn */
133   mpfr_set_prec (x, 44);
134   mpfr_set_prec (y, 44);
135   mpfr_set_si (x, 2, MPFR_RNDN);
136   mpfr_clear_flags ();
137   inex = mpfr_j0 (y, x, MPFR_RNDN);
138   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
139   mpfr_set_str (x, "0x.e5439fd9267p-2", 0, MPFR_RNDN);
140   if (! mpfr_equal_p (y, x))
141     {
142       printf ("Error on 2:\n");
143       printf ("Expected ");
144       mpfr_dump (x);
145       printf ("Got      ");
146       mpfr_dump (y);
147       exit (1);
148     }
149   if (inex >= 0)
150     {
151       printf ("Bad ternary value on 2: expected negative, got %d\n", inex);
152       exit (1);
153     }
154 
155   mpfr_clear (x);
156   mpfr_clear (y);
157 
158   test_generic (MPFR_PREC_MIN, 100, 10);
159 
160   data_check ("data/j0", mpfr_j0, "mpfr_j0");
161 
162   tests_end_mpfr ();
163 
164   return 0;
165 }
166