1/* Test file for mpfr_const_euler.
2
3Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4Contributed by the AriC and Caramel 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
20http://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 <stdio.h>
24#include <stdlib.h>
25
26#include "mpfr-test.h"
27
28/* Wrapper for tgeneric */
29static int
30my_const_euler (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r)
31{
32  return mpfr_const_euler (x, r);
33}
34
35#define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN)
36#define TEST_FUNCTION my_const_euler
37#include "tgeneric.c"
38
39int
40main (int argc, char *argv[])
41{
42  mpfr_t gamma, y, z, t;
43  unsigned int err, prec, yprec, p0 = 2, p1 = 200;
44  int rnd;
45
46  tests_start_mpfr ();
47
48  prec = (argc < 2) ? 53 : atoi(argv[1]);
49
50  if (argc > 1)
51    {
52      mpfr_init2 (gamma, prec);
53      mpfr_const_euler (gamma, MPFR_RNDN);
54      printf("gamma="); mpfr_out_str (stdout, 10, 0, gamma, MPFR_RNDD);
55      puts ("");
56      mpfr_clear (gamma);
57      return 0;
58    }
59
60  mpfr_init (y);
61  mpfr_init (z);
62  mpfr_init (t);
63
64  mpfr_set_prec (y, 32);
65  mpfr_set_prec (z, 32);
66  (mpfr_const_euler) (y, MPFR_RNDN);
67  mpfr_set_str_binary (z, "0.10010011110001000110011111100011");
68  if (mpfr_cmp (y, z))
69    {
70      printf ("Error for prec=32\n");
71      exit (1);
72    }
73
74  for (prec = p0; prec <= p1; prec++)
75    {
76      mpfr_set_prec (z, prec);
77      mpfr_set_prec (t, prec);
78      yprec = prec + 10;
79
80      for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
81        {
82          mpfr_set_prec (y, yprec);
83          mpfr_const_euler (y, (mpfr_rnd_t) rnd);
84          err = (rnd == MPFR_RNDN) ? yprec + 1 : yprec;
85          if (mpfr_can_round (y, err, (mpfr_rnd_t) rnd, (mpfr_rnd_t) rnd, prec))
86            {
87              mpfr_set (t, y, (mpfr_rnd_t) rnd);
88              mpfr_const_euler (z, (mpfr_rnd_t) rnd);
89              if (mpfr_cmp (t, z))
90                {
91                  printf ("results differ for prec=%u rnd_mode=%s\n", prec,
92                          mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
93                  printf ("   got      ");
94                  mpfr_out_str (stdout, 2, prec, z, MPFR_RNDN);
95                  puts ("");
96                  printf ("   expected ");
97                  mpfr_out_str (stdout, 2, prec, t, MPFR_RNDN);
98                  puts ("");
99                  printf ("   approximation was ");
100                  mpfr_print_binary (y);
101                  puts ("");
102                  exit (1);
103                }
104            }
105        }
106    }
107
108  mpfr_clear (y);
109  mpfr_clear (z);
110  mpfr_clear (t);
111
112  test_generic (2, 200, 1);
113
114  tests_end_mpfr ();
115  return 0;
116}
117