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