xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tconst_euler.c (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1 /* Test file for mpfr_const_euler.
2 
3 Copyright 2001-2018 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 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 "mpfr-test.h"
24 
25 /* Wrapper for tgeneric */
26 static int
27 my_const_euler (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r)
28 {
29   return mpfr_const_euler (x, r);
30 }
31 
32 #define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN)
33 #define TEST_FUNCTION my_const_euler
34 #include "tgeneric.c"
35 
36 int
37 main (int argc, char *argv[])
38 {
39   mpfr_t gamma, y, z, t;
40   unsigned int err, prec, yprec, p0 = 2, p1 = 200;
41   int rnd;
42 
43   tests_start_mpfr ();
44 
45   prec = (argc < 2) ? 53 : atoi(argv[1]);
46 
47   if (argc > 1)
48     {
49       mpfr_init2 (gamma, prec);
50       mpfr_const_euler (gamma, MPFR_RNDN);
51       printf("gamma="); mpfr_out_str (stdout, 10, 0, gamma, MPFR_RNDD);
52       puts ("");
53       mpfr_clear (gamma);
54       return 0;
55     }
56 
57   mpfr_init (y);
58   mpfr_init (z);
59   mpfr_init (t);
60 
61   mpfr_set_prec (y, 32);
62   mpfr_set_prec (z, 32);
63   (mpfr_const_euler) (y, MPFR_RNDN);
64   mpfr_set_str_binary (z, "0.10010011110001000110011111100011");
65   if (mpfr_cmp (y, z))
66     {
67       printf ("Error for prec=32\n");
68       exit (1);
69     }
70 
71   for (prec = p0; prec <= p1; prec++)
72     {
73       mpfr_set_prec (z, prec);
74       mpfr_set_prec (t, prec);
75       yprec = prec + 10;
76 
77       RND_LOOP_NO_RNDF (rnd)
78         {
79           mpfr_set_prec (y, yprec);
80           mpfr_const_euler (y, (mpfr_rnd_t) rnd);
81           err = (rnd == MPFR_RNDN) ? yprec + 1 : yprec;
82           /* Note: for rnd = RNDF, rnd1 = RNDF is equivalent to rnd1 = RNDN
83              in mpfr_can_round, thus rnd2 = RNDF reduces to rnd2 = RNDN in that
84              case, we are duplicating the test for rnd = RNDN. */
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_dump (y);
101                   exit (1);
102                 }
103             }
104         }
105     }
106 
107   mpfr_clear (y);
108   mpfr_clear (z);
109   mpfr_clear (t);
110 
111   test_generic (MPFR_PREC_MIN, 200, 1);
112 
113   tests_end_mpfr ();
114   return 0;
115 }
116