1efee5258Smrg /* Test file for mpfr_const_euler.
2efee5258Smrg
3*ba125506Smrg Copyright 2001-2023 Free Software Foundation, Inc.
4efdec83bSmrg Contributed by the AriC and Caramba projects, INRIA.
5efee5258Smrg
6efee5258Smrg This file is part of the GNU MPFR Library.
7efee5258Smrg
8efee5258Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9efee5258Smrg it under the terms of the GNU Lesser General Public License as published by
10efee5258Smrg the Free Software Foundation; either version 3 of the License, or (at your
11efee5258Smrg option) any later version.
12efee5258Smrg
13efee5258Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14efee5258Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15efee5258Smrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16efee5258Smrg License for more details.
17efee5258Smrg
18efee5258Smrg You should have received a copy of the GNU Lesser General Public License
19efee5258Smrg along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
202ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21efee5258Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22efee5258Smrg
23efee5258Smrg #include "mpfr-test.h"
24efee5258Smrg
25efee5258Smrg /* Wrapper for tgeneric */
26efee5258Smrg static int
my_const_euler(mpfr_ptr x,mpfr_srcptr y,mpfr_rnd_t r)27efee5258Smrg my_const_euler (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r)
28efee5258Smrg {
29efee5258Smrg return mpfr_const_euler (x, r);
30efee5258Smrg }
31efee5258Smrg
32efee5258Smrg #define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN)
33efee5258Smrg #define TEST_FUNCTION my_const_euler
34efee5258Smrg #include "tgeneric.c"
35efee5258Smrg
362ba2404bSmrg static void
exercise_Ziv(void)372ba2404bSmrg exercise_Ziv (void)
382ba2404bSmrg {
392ba2404bSmrg mpfr_t x, y;
402ba2404bSmrg int inex;
412ba2404bSmrg
422ba2404bSmrg mpfr_init2 (x, 177);
432ba2404bSmrg mpfr_init2 (y, 177);
442ba2404bSmrg inex = mpfr_const_euler (x, MPFR_RNDN);
452ba2404bSmrg mpfr_set_str_binary (y, "0.10010011110001000110011111100011011111011011000011000111101001001101000110111110001111111000000100000001010100101100101101010110101000011100111011001100001110101111011001011101");
462ba2404bSmrg MPFR_ASSERTN(mpfr_equal_p (x, y));
472ba2404bSmrg MPFR_ASSERTN(inex > 0);
482ba2404bSmrg mpfr_clear (x);
492ba2404bSmrg mpfr_clear (y);
502ba2404bSmrg }
512ba2404bSmrg
52efee5258Smrg int
main(int argc,char * argv[])53efee5258Smrg main (int argc, char *argv[])
54efee5258Smrg {
55efee5258Smrg mpfr_t gamma, y, z, t;
56efee5258Smrg unsigned int err, prec, yprec, p0 = 2, p1 = 200;
57efee5258Smrg int rnd;
58efee5258Smrg
59efee5258Smrg tests_start_mpfr ();
60efee5258Smrg
61efee5258Smrg prec = (argc < 2) ? 53 : atoi(argv[1]);
62efee5258Smrg
63efee5258Smrg if (argc > 1)
64efee5258Smrg {
65efee5258Smrg mpfr_init2 (gamma, prec);
66efee5258Smrg mpfr_const_euler (gamma, MPFR_RNDN);
67efee5258Smrg printf("gamma="); mpfr_out_str (stdout, 10, 0, gamma, MPFR_RNDD);
68efee5258Smrg puts ("");
69efee5258Smrg mpfr_clear (gamma);
70efee5258Smrg return 0;
71efee5258Smrg }
72efee5258Smrg
732ba2404bSmrg exercise_Ziv ();
742ba2404bSmrg
75efee5258Smrg mpfr_init (y);
76efee5258Smrg mpfr_init (z);
77efee5258Smrg mpfr_init (t);
78efee5258Smrg
79efee5258Smrg mpfr_set_prec (y, 32);
80efee5258Smrg mpfr_set_prec (z, 32);
81efee5258Smrg (mpfr_const_euler) (y, MPFR_RNDN);
82efee5258Smrg mpfr_set_str_binary (z, "0.10010011110001000110011111100011");
83efee5258Smrg if (mpfr_cmp (y, z))
84efee5258Smrg {
85efee5258Smrg printf ("Error for prec=32\n");
86efee5258Smrg exit (1);
87efee5258Smrg }
88efee5258Smrg
89efee5258Smrg for (prec = p0; prec <= p1; prec++)
90efee5258Smrg {
91efee5258Smrg mpfr_set_prec (z, prec);
92efee5258Smrg mpfr_set_prec (t, prec);
93efee5258Smrg yprec = prec + 10;
94efee5258Smrg
95299c6f0cSmrg RND_LOOP_NO_RNDF (rnd)
96efee5258Smrg {
97efee5258Smrg mpfr_set_prec (y, yprec);
98efee5258Smrg mpfr_const_euler (y, (mpfr_rnd_t) rnd);
99efee5258Smrg err = (rnd == MPFR_RNDN) ? yprec + 1 : yprec;
100299c6f0cSmrg /* Note: for rnd = RNDF, rnd1 = RNDF is equivalent to rnd1 = RNDN
101299c6f0cSmrg in mpfr_can_round, thus rnd2 = RNDF reduces to rnd2 = RNDN in that
102299c6f0cSmrg case, we are duplicating the test for rnd = RNDN. */
103efee5258Smrg if (mpfr_can_round (y, err, (mpfr_rnd_t) rnd, (mpfr_rnd_t) rnd, prec))
104efee5258Smrg {
105efee5258Smrg mpfr_set (t, y, (mpfr_rnd_t) rnd);
106efee5258Smrg mpfr_const_euler (z, (mpfr_rnd_t) rnd);
107efee5258Smrg if (mpfr_cmp (t, z))
108efee5258Smrg {
109efee5258Smrg printf ("results differ for prec=%u rnd_mode=%s\n", prec,
110efee5258Smrg mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
111efee5258Smrg printf (" got ");
112efee5258Smrg mpfr_out_str (stdout, 2, prec, z, MPFR_RNDN);
113efee5258Smrg puts ("");
114efee5258Smrg printf (" expected ");
115efee5258Smrg mpfr_out_str (stdout, 2, prec, t, MPFR_RNDN);
116efee5258Smrg puts ("");
117efee5258Smrg printf (" approximation was ");
118299c6f0cSmrg mpfr_dump (y);
119efee5258Smrg exit (1);
120efee5258Smrg }
121efee5258Smrg }
122efee5258Smrg }
123efee5258Smrg }
124efee5258Smrg
125efee5258Smrg mpfr_clear (y);
126efee5258Smrg mpfr_clear (z);
127efee5258Smrg mpfr_clear (t);
128efee5258Smrg
129299c6f0cSmrg test_generic (MPFR_PREC_MIN, 200, 1);
130efee5258Smrg
131efee5258Smrg tests_end_mpfr ();
132efee5258Smrg return 0;
133efee5258Smrg }
134