1/* Test file for mpfr_zeta_ui.
2
3Copyright 2005-2023 Free Software Foundation, Inc.
4Contributed by the AriC and Caramba projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include "mpfr-test.h"
24
25#define TEST_FUNCTION mpfr_zeta_ui
26
27int
28main (int argc, char *argv[])
29{
30  unsigned int prec, yprec;
31  int rnd;
32  mpfr_t x, y, z, t;
33  unsigned long n;
34  int inex;
35  mpfr_exp_t emin, emax;
36  mpfr_flags_t flags, ex_flags;
37  int i;
38
39  tests_start_mpfr ();
40
41  emin = mpfr_get_emin ();
42  emax = mpfr_get_emax ();
43
44  mpfr_init (x);
45  mpfr_init (y);
46  mpfr_init (z);
47  mpfr_init (t);
48
49  if (argc >= 3) /* tzeta_ui n prec [rnd] */
50    {
51      mpfr_set_prec (x, atoi (argv[2]));
52      mpfr_zeta_ui (x, atoi (argv[1]),
53                    argc > 3 ? (mpfr_rnd_t) atoi (argv[3]) : MPFR_RNDN);
54      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
55      printf ("\n");
56      goto clear_and_exit;
57    }
58
59  mpfr_set_prec (x, 33);
60  mpfr_set_prec (y, 33);
61  mpfr_zeta_ui (x, 3, MPFR_RNDZ);
62  mpfr_set_str_binary (y, "0.100110011101110100000000001001111E1");
63  if (mpfr_cmp (x, y))
64    {
65      printf ("Error for zeta(3), prec=33, MPFR_RNDZ\n");
66      printf ("expected "); mpfr_dump (y);
67      printf ("got      "); mpfr_dump (x);
68      exit (1);
69    }
70
71  mpfr_clear_flags ();
72  inex = mpfr_zeta_ui (x, 0, MPFR_RNDN);
73  flags = __gmpfr_flags;
74  MPFR_ASSERTN (inex == 0 && mpfr_cmp_si_2exp (x, -1, -1) == 0 && flags == 0);
75
76  for (i = -2; i <= 2; i += 2)
77    RND_LOOP_NO_RNDF (rnd)
78      {
79        int ex_inex;
80
81        set_emin (i);
82        set_emax (i);
83        mpfr_clear_flags ();
84        inex = mpfr_zeta_ui (x, 0, (mpfr_rnd_t) rnd);
85        flags = __gmpfr_flags;
86        if (i < 0)
87          {
88            mpfr_set_inf (y, -1);
89            if (rnd == MPFR_RNDU || rnd == MPFR_RNDZ)
90              {
91                mpfr_nextabove (y);
92                ex_inex = 1;
93              }
94            else
95              {
96                ex_inex = -1;
97              }
98            ex_flags = MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT;
99          }
100        else if (i > 0)
101          {
102            mpfr_set_zero (y, -1);
103            if (rnd == MPFR_RNDD || rnd == MPFR_RNDA)
104              {
105                mpfr_nextbelow (y);
106                ex_inex = -1;
107              }
108            else
109              {
110                ex_inex = 1;
111              }
112            ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT;
113          }
114        else
115          {
116            mpfr_set_str_binary (y, "-1e-1");
117            ex_inex = 0;
118            ex_flags = 0;
119          }
120        set_emin (emin);
121        set_emax (emax);
122        if (! (mpfr_equal_p (x, y) && MPFR_IS_NEG (x) &&
123               SAME_SIGN (inex, ex_inex) && flags == ex_flags))
124          {
125            printf ("Failure for zeta(0) in %s, exponent range [%d,%d]\n",
126                    mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), i, i);
127            printf ("Expected ");
128            mpfr_dump (y);
129            printf ("  with inex ~ %d, flags =", ex_inex);
130            flags_out (ex_flags);
131            printf ("Got      ");
132            mpfr_dump (x);
133            printf ("  with inex = %d, flags =", inex);
134            flags_out (flags);
135            exit (1);
136          }
137      }
138
139  mpfr_clear_divby0 ();
140  inex = mpfr_zeta_ui (x, 1, MPFR_RNDN);
141  MPFR_ASSERTN (inex == 0 && MPFR_IS_INF (x) && MPFR_IS_POS (x)
142                && mpfr_divby0_p ());
143
144  for (prec = MPFR_PREC_MIN; prec <= 100; prec++)
145    {
146      mpfr_set_prec (x, prec);
147      mpfr_set_prec (z, prec);
148      mpfr_set_prec (t, prec);
149      yprec = prec + 10;
150      mpfr_set_prec (y, yprec);
151
152      for (n = 0; n < 50; n++)
153        RND_LOOP_NO_RNDF (rnd)
154          {
155            mpfr_zeta_ui (y, n, MPFR_RNDN);
156            if (mpfr_can_round (y, yprec, MPFR_RNDN, MPFR_RNDZ, prec
157                                + (rnd == MPFR_RNDN)))
158              {
159                mpfr_set (t, y, (mpfr_rnd_t) rnd);
160                for (i = 0; i <= 1; i++)
161                  {
162                    if (i)
163                      {
164                        mpfr_exp_t e;
165
166                        if (MPFR_IS_SINGULAR (t))
167                          break;
168                        e = mpfr_get_exp (t);
169                        set_emin (e);
170                        set_emax (e);
171                      }
172                    mpfr_zeta_ui (z, n, (mpfr_rnd_t) rnd);
173                    if (i)
174                      {
175                        set_emin (emin);
176                        set_emax (emax);
177                      }
178                    if (mpfr_cmp (t, z))
179                      {
180                        printf ("results differ for n = %lu, prec = %u,"
181                                " %s%s\n", n, prec,
182                                mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
183                                i ? ", reduced exponent range" : "");
184                        printf ("  got      ");
185                        mpfr_dump (z);
186                        printf ("  expected ");
187                        mpfr_dump (t);
188                        printf ("  approx   ");
189                        mpfr_dump (y);
190                        exit (1);
191                      }
192                  }
193              }
194          }
195    }
196
197 clear_and_exit:
198  mpfr_clear (x);
199  mpfr_clear (y);
200  mpfr_clear (z);
201  mpfr_clear (t);
202
203  tests_end_mpfr ();
204  return 0;
205}
206