Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tzeta_ui.c
1 /* Test file for mpfr_zeta_ui.
2
3 Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 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_zeta_ui
29
30 int
31 main (int argc, char *argv[])
32 {
33 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
34   unsigned int prec, yprec;
35   int rnd;
36   mpfr_t x, y, z, t;
37   unsigned long n;
38   int inex;
39
40   tests_start_mpfr ();
41
42   mpfr_init (x);
43   mpfr_init (y);
44   mpfr_init (z);
45   mpfr_init (t);
46
47   if (argc >= 3) /* tzeta_ui n prec [rnd] */
48     {
49       mpfr_set_prec (x, atoi (argv[2]));
50       mpfr_zeta_ui (x, atoi (argv[1]),
51                     argc > 3 ? (mpfr_rnd_t) atoi (argv[3]) : MPFR_RNDN);
52       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
53       printf ("\n");
54       goto clear_and_exit;
55     }
56
57   mpfr_set_prec (x, 33);
58   mpfr_set_prec (y, 33);
59   mpfr_zeta_ui (x, 3, MPFR_RNDZ);
60   mpfr_set_str_binary (y, "0.100110011101110100000000001001111E1");
61   if (mpfr_cmp (x, y))
62     {
63       printf ("Error for zeta(3), prec=33, MPFR_RNDZ\n");
64       printf ("expected "); mpfr_dump (y);
65       printf ("got      "); mpfr_dump (x);
66       exit (1);
67     }
68
69   mpfr_clear_divby0 ();
70   inex = mpfr_zeta_ui (x, 0, MPFR_RNDN);
71   MPFR_ASSERTN (inex == 0 && mpfr_cmp_si_2exp (x, -1, -1) == 0
72                 && !mpfr_divby0_p ());
73
74   mpfr_clear_divby0 ();
75   inex = mpfr_zeta_ui (x, 1, MPFR_RNDN);
76   MPFR_ASSERTN (inex == 0 && MPFR_IS_INF (x) && MPFR_IS_POS (x)
77                 && mpfr_divby0_p ());
78
79   for (prec = MPFR_PREC_MIN; prec <= 100; prec++)
80     {
81       mpfr_set_prec (x, prec);
82       mpfr_set_prec (z, prec);
83       mpfr_set_prec (t, prec);
84       yprec = prec + 10;
85       mpfr_set_prec (y, yprec);
86
87       for (n = 0; n < 50; n++)
88         for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
89           {
90             mpfr_zeta_ui (y, n, MPFR_RNDN);
91             if (mpfr_can_round (y, yprec, MPFR_RNDN, MPFR_RNDZ, prec
92                                 + (rnd == MPFR_RNDN)))
93               {
94                 mpfr_set (t, y, (mpfr_rnd_t) rnd);
95                 mpfr_zeta_ui (z, n, (mpfr_rnd_t) rnd);
96                 if (mpfr_cmp (t, z))
97                   {
98                     printf ("results differ for n=%lu", n);
99                     printf (" prec=%u rnd_mode=%s\n", prec,
100                             mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
101                     printf ("   got      ");
102                     mpfr_dump (z);
103                     printf ("   expected ");
104                     mpfr_dump (t);
105                     printf ("   approx   ");
106                     mpfr_dump (y);
107                     exit (1);
108                   }
109               }
110           }
111     }
112
113  clear_and_exit:
114   mpfr_clear (x);
115   mpfr_clear (y);
116   mpfr_clear (z);
117   mpfr_clear (t);
118
119   tests_end_mpfr ();
120 #endif
121   return 0;
122 }