xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tlngamma.c (revision 8585484ef87f5a04d32332313cdb799625f4faf8)
1 /* mpfr_tlngamma -- test file for lngamma function
2 
3 Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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_lngamma
29 #define TEST_RANDOM_POS 16
30 #include "tgeneric.c"
31 
32 static void
33 special (void)
34 {
35   mpfr_t x, y;
36   int inex;
37 
38   mpfr_init (x);
39   mpfr_init (y);
40 
41   mpfr_set_nan (x);
42   mpfr_lngamma (y, x, MPFR_RNDN);
43   if (!mpfr_nan_p (y))
44     {
45       printf ("Error for lngamma(NaN)\n");
46       exit (1);
47     }
48 
49   mpfr_set_inf (x, -1);
50   mpfr_lngamma (y, x, MPFR_RNDN);
51   if (!mpfr_nan_p (y))
52     {
53       printf ("Error for lngamma(-Inf)\n");
54       exit (1);
55     }
56 
57   mpfr_set_inf (x, 1);
58   mpfr_lngamma (y, x, MPFR_RNDN);
59   if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
60     {
61       printf ("Error for lngamma(+Inf)\n");
62       exit (1);
63     }
64 
65   mpfr_set_ui (x, 0, MPFR_RNDN);
66   mpfr_lngamma (y, x, MPFR_RNDN);
67   if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
68     {
69       printf ("Error for lngamma(+0)\n");
70       exit (1);
71     }
72 
73   mpfr_set_ui (x, 0, MPFR_RNDN);
74   mpfr_neg (x, x, MPFR_RNDN);
75   mpfr_lngamma (y, x, MPFR_RNDN);
76   if (!mpfr_nan_p (y))
77     {
78       printf ("Error for lngamma(-0)\n");
79       exit (1);
80     }
81 
82   mpfr_set_ui (x, 1, MPFR_RNDN);
83   mpfr_lngamma (y, x, MPFR_RNDN);
84   if (MPFR_IS_NAN (y) || mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y))
85     {
86       printf ("Error for lngamma(1)\n");
87       exit (1);
88     }
89 
90   mpfr_set_si (x, -1, MPFR_RNDN);
91   mpfr_lngamma (y, x, MPFR_RNDN);
92   if (!mpfr_nan_p (y))
93     {
94       printf ("Error for lngamma(-1)\n");
95       exit (1);
96     }
97 
98   mpfr_set_ui (x, 2, MPFR_RNDN);
99   mpfr_lngamma (y, x, MPFR_RNDN);
100   if (MPFR_IS_NAN (y) || mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y))
101     {
102       printf ("Error for lngamma(2)\n");
103       exit (1);
104     }
105 
106   mpfr_set_prec (x, 53);
107   mpfr_set_prec (y, 53);
108 
109 #define CHECK_X1 "1.0762904832837976166"
110 #define CHECK_Y1 "-0.039418362817587634939"
111 
112   mpfr_set_str (x, CHECK_X1, 10, MPFR_RNDN);
113   mpfr_lngamma (y, x, MPFR_RNDN);
114   mpfr_set_str (x, CHECK_Y1, 10, MPFR_RNDN);
115   if (MPFR_IS_NAN (y) || mpfr_cmp (y, x))
116     {
117       printf ("mpfr_lngamma("CHECK_X1") is wrong:\n"
118               "expected ");
119       mpfr_print_binary (x); putchar ('\n');
120       printf ("got      ");
121       mpfr_print_binary (y); putchar ('\n');
122       exit (1);
123     }
124 
125 #define CHECK_X2 "9.23709516716202383435e-01"
126 #define CHECK_Y2 "0.049010669407893718563"
127   mpfr_set_str (x, CHECK_X2, 10, MPFR_RNDN);
128   mpfr_lngamma (y, x, MPFR_RNDN);
129   mpfr_set_str (x, CHECK_Y2, 10, MPFR_RNDN);
130   if (MPFR_IS_NAN (y) || mpfr_cmp (y, x))
131     {
132       printf ("mpfr_lngamma("CHECK_X2") is wrong:\n"
133               "expected ");
134       mpfr_print_binary (x); putchar ('\n');
135       printf ("got      ");
136       mpfr_print_binary (y); putchar ('\n');
137       exit (1);
138     }
139 
140   mpfr_set_prec (x, 8);
141   mpfr_set_prec (y, 175);
142   mpfr_set_ui (x, 33, MPFR_RNDN);
143   mpfr_lngamma (y, x, MPFR_RNDU);
144   mpfr_set_prec (x, 175);
145   mpfr_set_str_binary (x, "0.1010001100011101101011001101110010100001000001000001110011000001101100001111001001000101011011100100010101011110100111110101010100010011010010000101010111001100011000101111E7");
146   if (MPFR_IS_NAN (y) || mpfr_cmp (x, y))
147     {
148       printf ("Error in mpfr_lngamma (1)\n");
149       exit (1);
150     }
151 
152   mpfr_set_prec (x, 21);
153   mpfr_set_prec (y, 8);
154   mpfr_set_ui (y, 120, MPFR_RNDN);
155   mpfr_lngamma (x, y, MPFR_RNDZ);
156   mpfr_set_prec (y, 21);
157   mpfr_set_str_binary (y, "0.111000101000001100101E9");
158   if (MPFR_IS_NAN (x) || mpfr_cmp (x, y))
159     {
160       printf ("Error in mpfr_lngamma (120)\n");
161       printf ("Expected "); mpfr_print_binary (y); puts ("");
162       printf ("Got      "); mpfr_print_binary (x); puts ("");
163       exit (1);
164     }
165 
166   mpfr_set_prec (x, 3);
167   mpfr_set_prec (y, 206);
168   mpfr_set_str_binary (x, "0.110e10");
169   inex = mpfr_lngamma (y, x, MPFR_RNDN);
170   mpfr_set_prec (x, 206);
171   mpfr_set_str_binary (x, "0.10000111011000000011100010101001100110001110000111100011000100100110110010001011011110101001111011110110000001010100111011010000000011100110110101100111000111010011110010000100010111101010001101000110101001E13");
172   if (MPFR_IS_NAN (y) || mpfr_cmp (x, y))
173     {
174       printf ("Error in mpfr_lngamma (768)\n");
175       exit (1);
176     }
177   if (inex >= 0)
178     {
179       printf ("Wrong flag for mpfr_lngamma (768)\n");
180       exit (1);
181     }
182 
183   mpfr_set_prec (x, 4);
184   mpfr_set_prec (y, 4);
185   mpfr_set_str_binary (x, "0.1100E-66");
186   mpfr_lngamma (y, x, MPFR_RNDN);
187   mpfr_set_str_binary (x, "0.1100E6");
188   if (MPFR_IS_NAN (y) || mpfr_cmp (x, y))
189     {
190       printf ("Error for lngamma(0.1100E-66)\n");
191       exit (1);
192     }
193 
194   mpfr_set_prec (x, 256);
195   mpfr_set_prec (y, 32);
196   mpfr_set_si_2exp (x, -1, 200, MPFR_RNDN);
197   mpfr_add_ui (x, x, 1, MPFR_RNDN);
198   mpfr_div_2ui (x, x, 1, MPFR_RNDN);
199   mpfr_lngamma (y, x, MPFR_RNDN);
200   mpfr_set_prec (x, 32);
201   mpfr_set_str_binary (x, "-0.10001000111011111011000010100010E207");
202   if (MPFR_IS_NAN (y) || mpfr_cmp (x, y))
203     {
204       printf ("Error for lngamma(-2^199+0.5)\n");
205       printf ("Got        ");
206       mpfr_dump (y);
207       printf ("instead of ");
208       mpfr_dump (x);
209       exit (1);
210     }
211 
212   mpfr_set_prec (x, 256);
213   mpfr_set_prec (y, 32);
214   mpfr_set_si_2exp (x, -1, 200, MPFR_RNDN);
215   mpfr_sub_ui (x, x, 1, MPFR_RNDN);
216   mpfr_div_2ui (x, x, 1, MPFR_RNDN);
217   mpfr_lngamma (y, x, MPFR_RNDN);
218   if (!mpfr_nan_p (y))
219     {
220       printf ("Error for lngamma(-2^199-0.5)\n");
221       exit (1);
222     }
223 
224   mpfr_clear (x);
225   mpfr_clear (y);
226 }
227 
228 int
229 main (void)
230 {
231   tests_start_mpfr ();
232 
233   special ();
234   test_generic (2, 100, 2);
235 
236   tests_end_mpfr ();
237   return 0;
238 }
239