1
/* mpfr_tgamma -- test file for gamma function
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
5
This file is part of the MPFR Library, and was contributed by Mathieu Dutour.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
25
#include "mpfr-test.h"
27
#define TEST_FUNCTION mpfr_gamma
28
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1)
41
mpfr_gamma (y, x, GMP_RNDN);
44
printf ("Error for gamma(NaN)\n");
49
mpfr_gamma (y, x, GMP_RNDN);
52
printf ("Error for gamma(-Inf)\n");
57
mpfr_gamma (y, x, GMP_RNDN);
58
if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
60
printf ("Error for gamma(+Inf)\n");
64
mpfr_set_ui (x, 0, GMP_RNDN);
65
mpfr_gamma (y, x, GMP_RNDN);
66
if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
68
printf ("Error for gamma(+0)\n");
72
mpfr_set_ui (x, 0, GMP_RNDN);
73
mpfr_neg (x, x, GMP_RNDN);
74
mpfr_gamma (y, x, GMP_RNDN);
75
if (!mpfr_inf_p (y) || mpfr_sgn (y) > 0)
77
printf ("Error for gamma(-0)\n");
81
mpfr_set_ui (x, 1, GMP_RNDN);
82
mpfr_gamma (y, x, GMP_RNDN);
83
if (mpfr_cmp_ui (y, 1))
85
printf ("Error for gamma(1)\n");
89
mpfr_set_si (x, -1, GMP_RNDN);
90
mpfr_gamma (y, x, GMP_RNDN);
93
printf ("Error for gamma(-1)\n");
97
mpfr_set_prec (x, 53);
98
mpfr_set_prec (y, 53);
100
#define CHECK_X1 "1.0762904832837976166"
101
#define CHECK_Y1 "0.96134843256452096050"
103
mpfr_set_str (x, CHECK_X1, 10, GMP_RNDN);
104
mpfr_gamma (y, x, GMP_RNDN);
105
mpfr_set_str (x, CHECK_Y1, 10, GMP_RNDN);
108
printf ("mpfr_gamma("CHECK_X1") is wrong: expected %1.20e, got %1.20e\n",
109
mpfr_get_d (x, GMP_RNDN), mpfr_get_d (y, GMP_RNDN));
113
#define CHECK_X2 "9.23709516716202383435e-01"
114
#define CHECK_Y2 "1.0502315560291053398"
115
mpfr_set_str (x, CHECK_X2, 10, GMP_RNDN);
116
mpfr_gamma (y, x, GMP_RNDN);
117
mpfr_set_str (x, CHECK_Y2, 10, GMP_RNDN);
120
printf ("mpfr_gamma("CHECK_X2") is wrong: expected %1.20e, got %1.20e\n",
121
mpfr_get_d (x, GMP_RNDN), mpfr_get_d (y, GMP_RNDN));
125
mpfr_set_prec (x, 8);
126
mpfr_set_prec (y, 175);
127
mpfr_set_ui (x, 33, GMP_RNDN);
128
mpfr_gamma (y, x, GMP_RNDU);
129
mpfr_set_prec (x, 175);
130
mpfr_set_str_binary (x, "0.110010101011010101101000010101010111000110011101001000101011000001100010111001101001011E118");
133
printf ("Error in mpfr_gamma (1)\n");
137
mpfr_set_prec (x, 21);
138
mpfr_set_prec (y, 8);
139
mpfr_set_ui (y, 120, GMP_RNDN);
140
mpfr_gamma (x, y, GMP_RNDZ);
141
mpfr_set_prec (y, 21);
142
mpfr_set_str_binary (y, "0.101111101110100110110E654");
145
printf ("Error in mpfr_gamma (120)\n");
146
printf ("Expected "); mpfr_print_binary (y); puts ("");
147
printf ("Got "); mpfr_print_binary (x); puts ("");
151
mpfr_set_prec (x, 3);
152
mpfr_set_prec (y, 206);
153
mpfr_set_str_binary (x, "0.110e10");
154
inex = mpfr_gamma (y, x, GMP_RNDN);
155
mpfr_set_prec (x, 206);
156
mpfr_set_str_binary (x, "0.110111100001000001101010010001000111000100000100111000010011100011011111001100011110101000111101101100110001001100110100001001111110000101010000100100011100010011101110000001000010001100010000101001111E6250");
159
printf ("Error in mpfr_gamma (768)\n");
164
printf ("Wrong flag for mpfr_gamma (768)\n");
168
/* worst case to exercise retry */
169
mpfr_set_prec (x, 1000);
170
mpfr_set_prec (y, 869);
171
mpfr_const_pi (x, GMP_RNDN);
172
mpfr_gamma (y, x, GMP_RNDN);
174
mpfr_set_prec (x, 4);
175
mpfr_set_prec (y, 4);
176
mpfr_set_str_binary (x, "-0.1100E-66");
177
mpfr_gamma (y, x, GMP_RNDN);
178
mpfr_set_str_binary (x, "-0.1011E67");
181
printf ("Error for gamma(-0.1100E-66)\n");
190
special_overflow (void)
193
mp_exp_t emin = mpfr_get_emin ();
200
mpfr_set_str_binary (x, "0.101100100000000000110100E7");
201
mpfr_gamma (y, x, GMP_RNDN);
204
printf ("Overflow error.\n");
209
/* problem mentioned by Kenneth Wilder, 18 Aug 2005 */
210
mpfr_set_prec (x, 29);
211
mpfr_set_prec (y, 29);
212
mpfr_set_str (x, "-200000000.5", 10, GMP_RNDN); /* exact */
213
mpfr_gamma (y, x, GMP_RNDN);
214
if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0))
216
printf ("Error for gamma(-200000000.5)\n");
217
printf ("expected -0");
223
mpfr_set_prec (x, 53);
224
mpfr_set_prec (y, 53);
225
mpfr_set_str (x, "-200000000.1", 10, GMP_RNDN);
226
mpfr_gamma (y, x, GMP_RNDN);
227
if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0))
229
printf ("Error for gamma(-200000000.1), prec=53\n");
230
printf ("expected -0");
236
/* another problem mentioned by Kenneth Wilder, 29 Aug 2005 */
237
mpfr_set_prec (x, 333);
238
mpfr_set_prec (y, 14);
239
mpfr_set_str (x, "-2.0000000000000000000000005", 10, GMP_RNDN);
240
mpfr_gamma (y, x, GMP_RNDN);
241
mpfr_set_prec (x, 14);
242
mpfr_set_str_binary (x, "-11010011110001E66");
245
printf ("Error for gamma(-2.0000000000000000000000005)\n");
246
printf ("expected "); mpfr_dump (x);
247
printf ("got "); mpfr_dump (y);
251
/* another tests from Kenneth Wilder, 31 Aug 2005 */
254
mpfr_set_prec (x, 38);
255
mpfr_set_prec (y, 54);
256
mpfr_set_str_binary (x, "0.11101111011100111101001001010110101001E-166");
257
mpfr_gamma (y, x, GMP_RNDN);
258
mpfr_set_prec (x, 54);
259
mpfr_set_str_binary (x, "0.100010001101100001110110001010111111010000100101011E167");
262
printf ("Error for gamma (test 1)\n");
263
printf ("expected "); mpfr_dump (x);
264
printf ("got "); mpfr_dump (y);
270
mpfr_set_prec (x, 38);
271
mpfr_set_prec (y, 71);
272
mpfr_set_str_binary (x, "10101011011100001111111000010111110010E-1034");
273
/* 184083777010*2^(-1034) */
274
mpfr_gamma (y, x, GMP_RNDN);
275
mpfr_set_prec (x, 71);
276
mpfr_set_str_binary (x, "10111111001000011110010001000000000000110011110000000011101011111111100E926");
277
/* 1762885132679550982140*2^926 */
280
printf ("Error for gamma (test 2)\n");
281
printf ("expected "); mpfr_dump (x);
282
printf ("got "); mpfr_dump (y);
286
mpfr_set_prec (x, 38);
287
mpfr_set_prec (y, 88);
288
mpfr_set_str_binary (x, "10111100111001010000100001100100100101E-104");
289
/* 202824096037*2^(-104) */
290
mpfr_gamma (y, x, GMP_RNDN);
291
mpfr_set_prec (x, 88);
292
mpfr_set_str_binary (x, "1010110101111000111010111100010110101010100110111000001011000111000011101100001101110010E-21");
293
/* 209715199999500283894743922*2^(-21) */
296
printf ("Error for gamma (test 3)\n");
297
printf ("expected "); mpfr_dump (x);
298
printf ("got "); mpfr_dump (y);
302
mpfr_set_prec (x, 171);
303
mpfr_set_prec (y, 38);
304
mpfr_set_str (x, "-2993155353253689176481146537402947624254601559176535", 10,
306
mpfr_div_2exp (x, x, 170, GMP_RNDN);
307
mpfr_gamma (y, x, GMP_RNDN);
308
mpfr_set_prec (x, 38);
309
mpfr_set_str (x, "201948391737", 10, GMP_RNDN);
310
mpfr_mul_2exp (x, x, 92, GMP_RNDN);
313
printf ("Error for gamma (test 5)\n");
314
printf ("expected "); mpfr_dump (x);
315
printf ("got "); mpfr_dump (y);
320
mpfr_set_prec (x, 337);
321
mpfr_set_prec (y, 38);
322
mpfr_set_str (x, "-30000.000000000000000000000000000000000000000000001", 10,
324
mpfr_gamma (y, x, GMP_RNDN);
325
mpfr_set_prec (x, 38);
326
mpfr_set_str (x, "-3.623795987425E-121243", 10, GMP_RNDN);
329
printf ("Error for gamma (test 7)\n");
330
printf ("expected "); mpfr_dump (x);
331
printf ("got "); mpfr_dump (y);
335
/* was producing infinite loop */
337
mpfr_set_prec (x, 71);
338
mpfr_set_prec (y, 71);
339
mpfr_set_str (x, "-200000000.1", 10, GMP_RNDN);
340
mpfr_gamma (y, x, GMP_RNDN);
341
if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0))
343
printf ("Error for gamma (test 8)\n");
344
printf ("expected "); mpfr_dump (x);
345
printf ("got "); mpfr_dump (y);
349
set_emax (1073741823);
350
mpfr_set_prec (x, 29);
351
mpfr_set_prec (y, 29);
352
mpfr_set_str (x, "423786866", 10, GMP_RNDN);
353
mpfr_gamma (y, x, GMP_RNDN);
354
if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
356
printf ("Error for gamma(423786866)\n");
362
set_emin (MPFR_EMIN_MIN);
363
set_emax (MPFR_EMAX_MAX);
369
MPFR_TEST_USE_RANDS ();
374
test_generic (2, 100, 2);