xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tdigamma.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* test file for digamma function
2 
3 Copyright 2009-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_digamma
26 #include "tgeneric.c"
27 
28 static void
special(void)29 special (void)
30 {
31   mpfr_t x, y;
32 
33   mpfr_init (x);
34   mpfr_init (y);
35 
36   mpfr_set_inf (y, -1);
37   mpfr_set_inf (x, 1);
38   mpfr_digamma (y, x, MPFR_RNDN);
39   if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) < 0)
40     {
41       printf ("error for Psi(+Inf)\n");
42       printf ("expected +Inf\n");
43       printf ("got      ");
44       mpfr_dump (y);
45       exit (1);
46     }
47 
48   mpfr_clear (x);
49   mpfr_clear (y);
50 }
51 
52 /* With some GMP_CHECK_RANDOMIZE values, test_generic triggers an error
53      tests_addsize(): too much memory (576460752303432776 bytes)
54   Each time on prec = 200, n = 3, xprec = 140.
55   The following test is a more general testcase.
56 */
57 static void
bug20210206(void)58 bug20210206 (void)
59 {
60 #define NPREC 4
61   mpfr_t x, y[NPREC], z;
62   mpfr_exp_t emin, emax;
63   int i, precx, precy[NPREC] = { 200, 400, 520, 1416 };
64 
65   emin = mpfr_get_emin ();
66   emax = mpfr_get_emax ();
67   set_emin (MPFR_EMIN_MIN);
68   set_emax (MPFR_EMAX_MAX);
69 
70   for (i = 0; i < NPREC; i++)
71     mpfr_init2 (y[i], precy[i]);
72   mpfr_init2 (z, precy[0]);
73 
74   for (precx = MPFR_PREC_MIN; precx < 150; precx++)
75     {
76       mpfr_init2 (x, precx);
77       mpfr_setmax (x, __gmpfr_emax);
78       for (i = 0; i < NPREC; i++)
79         mpfr_digamma (y[i], x, MPFR_RNDA);
80       mpfr_set (z, y[1], MPFR_RNDA);
81       MPFR_ASSERTN (mpfr_equal_p (y[0], z));
82       mpfr_clear (x);
83     }
84 
85   for (i = 0; i < NPREC; i++)
86     mpfr_clear (y[i]);
87   mpfr_clear (z);
88 
89   set_emin (emin);
90   set_emax (emax);
91 }
92 
93 /* another test that fails with GMP_CHECK_RANDOMIZE=1612741376857003
94    on revision 14398 */
95 static void
bug20210208(void)96 bug20210208 (void)
97 {
98   mpfr_t x, y;
99   int inex;
100 
101   mpfr_init2 (x, 73);
102   mpfr_init2 (y, 1);
103   mpfr_set_str (x, "1.4613470547060071827450", 10, MPFR_RNDN);
104   mpfr_clear_flags ();
105   inex = mpfr_digamma (y, x, MPFR_RNDU);
106   MPFR_ASSERTN (mpfr_cmp_si_2exp (y, -1, -12) == 0);
107   MPFR_ASSERTN (inex > 0);
108   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
109   mpfr_clear (x);
110   mpfr_clear (y);
111 }
112 
113 /* another test that fails with GMP_CHECK_RANDOMIZE=1613197421465830
114    on revision 14429 */
115 static void
bug20210215(void)116 bug20210215 (void)
117 {
118   mpfr_t x, y;
119   int inex;
120 
121   mpfr_init2 (x, 510);
122   mpfr_init2 (y, 4);
123   mpfr_set_str (x, "-8.2923051438433494998166335341807999322052669984208422481227138906096000469898717007386115912802685588348601663465077353194268894939972221117314512518182580e+35", 10, MPFR_RNDN);
124   mpfr_clear_flags ();
125   inex = mpfr_digamma (y, x, MPFR_RNDU);
126   MPFR_ASSERTN (mpfr_cmp_ui0 (y, 88) == 0);
127   MPFR_ASSERTN (inex > 0);
128   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
129   mpfr_clear (x);
130   mpfr_clear (y);
131 }
132 
133 int
main(int argc,char * argv[])134 main (int argc, char *argv[])
135 {
136   tests_start_mpfr ();
137 
138   special ();
139   bug20210206 ();
140   bug20210208 ();
141   bug20210215 ();
142 
143   test_generic (MPFR_PREC_MIN, 200, 20);
144 
145   data_check ("data/digamma", mpfr_digamma, "mpfr_digamma");
146 
147   tests_end_mpfr ();
148   return 0;
149 }
150