1 /* Test file for the beta function 2 3 Copyright 2017-2018 Free Software Foundation, Inc. 4 Contributed by ChemicalDevelopment. 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 "mpfr-test.h" 24 25 /* TODO: Test the ternary value and the flags. Add tgeneric tests. */ 26 27 #define FAILED(p, r, z, w, expected, rnd_mode) do { \ 28 printf ("prec=%d, rnd=%s case failed for:", \ 29 (int) p, mpfr_print_rnd_mode (rnd_mode)); \ 30 printf("\n z ="); \ 31 mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN); \ 32 printf("\n w ="); \ 33 mpfr_out_str (stdout, 2, 0, w, MPFR_RNDN); \ 34 printf("\n ex.="); \ 35 mpfr_out_str (stdout, 2, 0, expected, MPFR_RNDN); \ 36 printf("\n ac.="); \ 37 mpfr_out_str (stdout, 2, 0, r, MPFR_RNDN); \ 38 printf("\n\n"); \ 39 } while (0) 40 41 #define TEST(p, r, z, w, expected) TESTRND(p, r, z, w, expected, MPFR_RNDN) 42 43 #define TESTRND(p, r, z, w, expected, rnd_mode) do { \ 44 mpfr_beta (r, z, w, rnd_mode); \ 45 if (! SAME_VAL (r, expected)) \ 46 FAILED(p, r, z, w, expected, rnd_mode); \ 47 } while (0) 48 49 static void 50 test_beta_special (mpfr_prec_t prec) 51 { 52 mpfr_t z, w, r, expect; 53 54 mpfr_init2 (r, prec); 55 mpfr_init2 (z, prec); 56 mpfr_init2 (w, prec); 57 mpfr_init2 (expect, prec); 58 59 mpfr_set_inf (z, 1); 60 mpfr_set_inf (w, 1); 61 mpfr_set_zero (expect, 1); 62 TEST(prec, r, z, w, expect); 63 64 mpfr_set_inf (z, 1); 65 mpfr_set_zero (w, 1); 66 mpfr_set_nan (expect); 67 TEST(prec, r, z, w, expect); 68 69 mpfr_set_inf (z, 1); 70 mpfr_set_zero (w, -1); 71 mpfr_set_nan (expect); 72 TEST(prec, r, z, w, expect); 73 74 mpfr_set_inf (z, 1); 75 mpfr_set_str (w, "-.1e0", 2, MPFR_RNDN); 76 mpfr_set_inf (expect, 1); 77 TEST(prec, r, z, w, expect); 78 79 mpfr_set_inf (z, 1); 80 mpfr_set_str (w, "-1.1e0", 2, MPFR_RNDN); 81 mpfr_set_inf (expect, -1); 82 TEST(prec, r, z, w, expect); 83 84 mpfr_set_inf (z, 1); 85 mpfr_set_str (w, "-1e0", 2, MPFR_RNDN); 86 mpfr_set_nan (expect); 87 TEST(prec, r, z, w, expect); 88 89 mpfr_set_inf (z, 1); 90 mpfr_set_str (w, "-2e0", 2, MPFR_RNDN); 91 mpfr_set_nan (expect); 92 TEST(prec, r, z, w, expect); 93 94 if (prec > 81) 95 { 96 mpfr_set_inf (z, 1); 97 mpfr_set_str (w, "-1e80", 2, MPFR_RNDN); 98 mpfr_set_nan (expect); 99 TEST(prec, r, z, w, expect); 100 101 mpfr_set_inf (z, 1); 102 mpfr_set_str (w, "-1e80", 2, MPFR_RNDN); 103 mpfr_sub_d (w, w, .1, MPFR_RNDN); 104 mpfr_set_inf (expect, 1); 105 TEST(prec, r, z, w, expect); 106 107 mpfr_set_str (w, "-1e80", 2, MPFR_RNDN); 108 mpfr_sub_d (w, w, 1.1, MPFR_RNDN); 109 mpfr_set_inf (expect, -1); 110 TEST(prec, r, z, w, expect); 111 } 112 113 mpfr_set_str (z, "1.1e0", 2, MPFR_RNDN); 114 mpfr_set_inf (w, -1); 115 mpfr_set_nan (expect); 116 TEST(prec, r, z, w, expect); 117 118 mpfr_set_str (z, "11e0", 2, MPFR_RNDN); 119 mpfr_set_inf (w, -1); 120 mpfr_set_zero (expect, -1); 121 TEST(prec, r, z, w, expect); 122 123 mpfr_set_str (z, "10e0", 2, MPFR_RNDN); 124 mpfr_set_inf (w, -1); 125 mpfr_set_zero (expect, 1); 126 TEST(prec, r, z, w, expect); 127 128 mpfr_set_inf (z, -1); 129 mpfr_set_inf (w, -1); 130 mpfr_set_nan (expect); 131 TEST(prec, r, z, w, expect); 132 133 mpfr_clear (r); 134 mpfr_clear (z); 135 mpfr_clear (w); 136 mpfr_clear (expect); 137 } 138 139 static void 140 test_beta_2exp (mpfr_prec_t prec, int trials, int spread) 141 { 142 mpfr_t r, z, w, expect; 143 int i; 144 145 mpfr_init2 (r, prec); 146 mpfr_init2 (z, prec); 147 mpfr_init2 (w, prec); 148 mpfr_init2 (expect, prec); 149 for (i = -(spread*trials)/2; spread*i < trials / 2; i += spread) 150 { 151 mpfr_set_si_2exp (z, 1, i, MPFR_RNDN); 152 mpfr_set_ui (w, 1, MPFR_RNDN); 153 mpfr_set_si_2exp (expect, 1, -i, MPFR_RNDN); 154 155 TEST(prec, r, z, w, expect); 156 } 157 158 mpfr_clear (r); 159 mpfr_clear (z); 160 mpfr_clear (w); 161 mpfr_clear (expect); 162 } 163 164 /* 165 Tests values such that z and w are not integers, but (z+w) is. 166 167 An example that was given: 168 beta(-.3, -1.7) = gamma(-0.3)*gamma(-1.7)/gamma(-2) 169 170 Sage gives this as 0, and Lefevre said that we should return +0 171 172 */ 173 static void 174 test_beta_zw_sum_int (mpfr_prec_t prec) 175 { 176 mpfr_t r, z, w, expect; 177 int sum; 178 179 if (prec < 4) 180 prec = 4; 181 182 mpfr_init2 (r, prec); 183 mpfr_init2 (z, prec); 184 mpfr_init2 (w, prec); 185 mpfr_init2 (expect, prec); 186 187 sum = -3; 188 mpfr_set_str (z, "-1.1e0", 2, MPFR_RNDN); 189 mpfr_si_sub (w, sum, z, MPFR_RNDN); 190 mpfr_set_zero (expect, 1); 191 TEST(prec, r, z, w, expect); 192 193 sum = -12; 194 mpfr_set_str (z, "-1.101e0", 2, MPFR_RNDN); 195 mpfr_si_sub (w, sum, z, MPFR_RNDN); 196 mpfr_set_zero (expect, 1); 197 TEST(prec, r, z, w, expect); 198 199 sum = -1; 200 mpfr_set_str (z, "-.11e0", 2, MPFR_RNDN); 201 mpfr_si_sub (w, sum, z, MPFR_RNDN); 202 mpfr_set_zero (expect, 1); 203 TEST(prec, r, z, w, expect); 204 205 sum = -13; 206 mpfr_set_str (z, "-.001e0", 2, MPFR_RNDN); 207 mpfr_si_sub (w, sum, z, MPFR_RNDN); 208 mpfr_set_zero (expect, 1); 209 TEST(prec, r, z, w, expect); 210 211 mpfr_clear (r); 212 mpfr_clear (z); 213 mpfr_clear (w); 214 mpfr_clear (expect); 215 } 216 217 218 static void 219 test_beta_hardcoded (mpfr_prec_t prec) 220 { 221 mpfr_t r, z, w, expect; 222 mpfr_prec_t oprec = 1; 223 224 if (prec < 10) 225 prec = 10; 226 227 mpfr_init2 (z, prec); 228 mpfr_init2 (w, prec); 229 mpfr_init2 (r, oprec); 230 mpfr_init2 (expect, oprec); 231 232 mpfr_set_ui (z, 3, MPFR_RNDN); 233 mpfr_set_ui (w, 3, MPFR_RNDN); 234 mpfr_set_str (expect, "1e-5", 2, MPFR_RNDN); 235 TESTRND(prec, r, z, w, expect, MPFR_RNDN); 236 237 mpfr_set_str (expect, "1.1e-5", 2, MPFR_RNDN); 238 TESTRND(prec, r, z, w, expect, MPFR_RNDU); 239 240 mpfr_set_str (expect, "1e-5", 2, MPFR_RNDN); 241 TESTRND(prec, r, z, w, expect, MPFR_RNDD); 242 243 mpfr_set_ui (z, 5, MPFR_RNDN); 244 mpfr_set_ui (w, 27, MPFR_RNDN); 245 mpfr_set_str (expect, "1e-20", 2, MPFR_RNDN); 246 TESTRND(prec, r, z, w, expect, MPFR_RNDN); 247 248 mpfr_set_str (expect, "1e-19", 2, MPFR_RNDN); 249 TESTRND(prec, r, z, w, expect, MPFR_RNDU); 250 251 mpfr_set_ui (z, 5, MPFR_RNDN); 252 mpfr_set_ui (w, 27, MPFR_RNDN); 253 mpfr_set_str (expect, "1e-20", 2, MPFR_RNDN); 254 TESTRND(prec, r, z, w, expect, MPFR_RNDN); 255 256 257 mpfr_set_ui (z, 121, MPFR_RNDN); 258 mpfr_set_ui (w, 2, MPFR_RNDN); 259 mpfr_set_str (expect, "1e-14", 2, MPFR_RNDN); 260 TESTRND(prec, r, z, w, expect, MPFR_RNDN); 261 262 mpfr_set_ui (z, 121, MPFR_RNDN); 263 mpfr_set_ui (w, 151, MPFR_RNDN); 264 mpfr_set_str (expect, "1e-271", 2, MPFR_RNDN); 265 TESTRND(prec, r, z, w, expect, MPFR_RNDN); 266 267 mpfr_set_str (expect, "1e-272", 2, MPFR_RNDN); 268 TESTRND(prec, r, z, w, expect, MPFR_RNDD); 269 270 mpfr_set_str (expect, "1e-271", 2, MPFR_RNDN); 271 TESTRND(prec, r, z, w, expect, MPFR_RNDU); 272 273 mpfr_clear (r); 274 mpfr_clear (z); 275 mpfr_clear (w); 276 mpfr_clear (expect); 277 } 278 279 /* makes sure beta(a, b) = beta(b, a) */ 280 static void 281 test_beta_refl (mpfr_prec_t prec, mpfr_rnd_t rnd_mode) 282 { 283 mpfr_t r, z, w, expect; 284 285 mpfr_init2 (z, prec); 286 mpfr_init2 (w, prec); 287 mpfr_init2 (r, prec); 288 mpfr_init2 (expect, prec); 289 290 mpfr_set_ui (z, 3, MPFR_RNDN); 291 mpfr_set_ui (w, 3, MPFR_RNDN); 292 mpfr_beta (expect, w, z, rnd_mode); 293 TESTRND(prec, r, z, w, expect, rnd_mode); 294 295 mpfr_set_ui (z, 5, MPFR_RNDN); 296 mpfr_set_ui (w, 100, MPFR_RNDN); 297 mpfr_beta (expect, w, z, rnd_mode); 298 TESTRND(prec, r, z, w, expect, rnd_mode); 299 300 mpfr_set_nan (z); 301 mpfr_set_ui (w, 100, MPFR_RNDN); 302 mpfr_beta (expect, w, z, rnd_mode); 303 TESTRND(prec, r, z, w, expect, rnd_mode); 304 305 mpfr_set_nan (z); 306 mpfr_set_ui (w, 1, MPFR_RNDN); 307 mpfr_beta (expect, w, z, rnd_mode); 308 TESTRND(prec, r, z, w, expect, rnd_mode); 309 310 mpfr_set_nan (z); 311 mpfr_set_nan (w); 312 mpfr_beta (expect, w, z, rnd_mode); 313 TESTRND(prec, r, z, w, expect, rnd_mode); 314 mpfr_set_nan (z); 315 mpfr_set_nan (w); 316 mpfr_beta (expect, w, z, rnd_mode); 317 TESTRND(prec, r, z, w, expect, rnd_mode); 318 319 mpfr_clear (r); 320 mpfr_clear (z); 321 mpfr_clear (w); 322 mpfr_clear (expect); 323 } 324 325 #define TEST_FUNCTION mpfr_beta 326 #define TWO_ARGS 327 #define TEST_RANDOM_EMIN -16 328 #define TEST_RANDOM_EMAX 16 329 #include "tgeneric.c" 330 331 int 332 main (void) 333 { 334 tests_start_mpfr (); 335 336 test_beta_special (10); 337 test_beta_special (100); 338 test_beta_special (1000); 339 340 test_beta_2exp (1, 10, 1); 341 test_beta_2exp (100, 40, 3); 342 343 test_beta_hardcoded (10); 344 test_beta_hardcoded (100); 345 346 test_beta_refl (1, MPFR_RNDN); 347 test_beta_refl (100, MPFR_RNDD); 348 349 test_beta_zw_sum_int (10); 350 test_beta_zw_sum_int (100); 351 352 test_generic (MPFR_PREC_MIN, 100, 20); 353 354 tests_end_mpfr (); 355 return 0; 356 } 357