xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tconst_euler.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Test file for mpfr_const_euler.
2 
3 Copyright 2001-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 /* Wrapper for tgeneric */
26 static int
my_const_euler(mpfr_ptr x,mpfr_srcptr y,mpfr_rnd_t r)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 static void
exercise_Ziv(void)37 exercise_Ziv (void)
38 {
39   mpfr_t x, y;
40   int inex;
41 
42   mpfr_init2 (x, 177);
43   mpfr_init2 (y, 177);
44   inex = mpfr_const_euler (x, MPFR_RNDN);
45   mpfr_set_str_binary (y, "0.10010011110001000110011111100011011111011011000011000111101001001101000110111110001111111000000100000001010100101100101101010110101000011100111011001100001110101111011001011101");
46   MPFR_ASSERTN(mpfr_equal_p (x, y));
47   MPFR_ASSERTN(inex > 0);
48   mpfr_clear (x);
49   mpfr_clear (y);
50 }
51 
52 int
main(int argc,char * argv[])53 main (int argc, char *argv[])
54 {
55   mpfr_t gamma, y, z, t;
56   unsigned int err, prec, yprec, p0 = 2, p1 = 200;
57   int rnd;
58 
59   tests_start_mpfr ();
60 
61   prec = (argc < 2) ? 53 : atoi(argv[1]);
62 
63   if (argc > 1)
64     {
65       mpfr_init2 (gamma, prec);
66       mpfr_const_euler (gamma, MPFR_RNDN);
67       printf("gamma="); mpfr_out_str (stdout, 10, 0, gamma, MPFR_RNDD);
68       puts ("");
69       mpfr_clear (gamma);
70       return 0;
71     }
72 
73   exercise_Ziv ();
74 
75   mpfr_init (y);
76   mpfr_init (z);
77   mpfr_init (t);
78 
79   mpfr_set_prec (y, 32);
80   mpfr_set_prec (z, 32);
81   (mpfr_const_euler) (y, MPFR_RNDN);
82   mpfr_set_str_binary (z, "0.10010011110001000110011111100011");
83   if (mpfr_cmp (y, z))
84     {
85       printf ("Error for prec=32\n");
86       exit (1);
87     }
88 
89   for (prec = p0; prec <= p1; prec++)
90     {
91       mpfr_set_prec (z, prec);
92       mpfr_set_prec (t, prec);
93       yprec = prec + 10;
94 
95       RND_LOOP_NO_RNDF (rnd)
96         {
97           mpfr_set_prec (y, yprec);
98           mpfr_const_euler (y, (mpfr_rnd_t) rnd);
99           err = (rnd == MPFR_RNDN) ? yprec + 1 : yprec;
100           /* Note: for rnd = RNDF, rnd1 = RNDF is equivalent to rnd1 = RNDN
101              in mpfr_can_round, thus rnd2 = RNDF reduces to rnd2 = RNDN in that
102              case, we are duplicating the test for rnd = RNDN. */
103           if (mpfr_can_round (y, err, (mpfr_rnd_t) rnd, (mpfr_rnd_t) rnd, prec))
104             {
105               mpfr_set (t, y, (mpfr_rnd_t) rnd);
106               mpfr_const_euler (z, (mpfr_rnd_t) rnd);
107               if (mpfr_cmp (t, z))
108                 {
109                   printf ("results differ for prec=%u rnd_mode=%s\n", prec,
110                           mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
111                   printf ("   got      ");
112                   mpfr_out_str (stdout, 2, prec, z, MPFR_RNDN);
113                   puts ("");
114                   printf ("   expected ");
115                   mpfr_out_str (stdout, 2, prec, t, MPFR_RNDN);
116                   puts ("");
117                   printf ("   approximation was ");
118                   mpfr_dump (y);
119                   exit (1);
120                 }
121             }
122         }
123     }
124 
125   mpfr_clear (y);
126   mpfr_clear (z);
127   mpfr_clear (t);
128 
129   test_generic (MPFR_PREC_MIN, 200, 1);
130 
131   tests_end_mpfr ();
132   return 0;
133 }
134