1/* Test file for mpfr_eint.
2
3Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4Contributed by the Arenaire and Cacao 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#define TEST_FUNCTION mpfr_eint
29#define TEST_RANDOM_POS 8
30#define TEST_RANDOM_EMAX 40
31#include "tgeneric.c"
32
33static void
34check_specials (void)
35{
36  mpfr_t  x, y;
37
38  mpfr_init2 (x, 123L);
39  mpfr_init2 (y, 123L);
40
41  mpfr_set_nan (x);
42  mpfr_eint (y, x, MPFR_RNDN);
43  if (! mpfr_nan_p (y))
44    {
45      printf ("Error: eint(NaN) != NaN\n");
46      exit (1);
47    }
48
49  mpfr_set_inf (x, 1);
50  mpfr_eint (y, x, MPFR_RNDN);
51  if (! (mpfr_inf_p (y) && mpfr_sgn (y) > 0))
52    {
53      printf ("Error: eint(+Inf) != +Inf\n");
54      exit (1);
55    }
56
57  mpfr_set_inf (x, -1);
58  mpfr_eint (y, x, MPFR_RNDN);
59  if (! mpfr_nan_p (y))
60    {
61      printf ("Error: eint(-Inf) != NaN\n");
62      exit (1);
63    }
64
65  /* eint(+/-0) = -Inf */
66  mpfr_set_ui (x, 0, MPFR_RNDN);
67  mpfr_eint (y, x, MPFR_RNDN);
68  if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
69    {
70      printf ("Error: eint(+0) != -Inf\n");
71      exit (1);
72    }
73  mpfr_neg (x, x, MPFR_RNDN);
74  mpfr_eint (y, x, MPFR_RNDN);
75  if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
76    {
77      printf ("Error: eint(-0) != -Inf\n");
78      exit (1);
79    }
80
81  /* eint(x) = NaN for x < 0 */
82  mpfr_set_si (x, -1, MPFR_RNDN);
83  mpfr_eint (y, x, MPFR_RNDN);
84  if (! mpfr_nan_p (y))
85    {
86      printf ("Error: eint(-1) != NaN\n");
87      exit (1);
88    }
89
90  mpfr_set_prec (x, 17);
91  mpfr_set_prec (y, 17);
92  mpfr_set_str_binary (x, "1.0111110100100110e-2");
93  mpfr_set_str_binary (y, "-1.0010101001110100e-10");
94  mpfr_eint (x, x, MPFR_RNDZ);
95  if (mpfr_cmp (x, y))
96    {
97      printf ("Error for x=1.0111110100100110e-2, MPFR_RNDZ\n");
98      printf ("expected "); mpfr_dump (y);
99      printf ("got      "); mpfr_dump (x);
100      exit (1);
101    }
102
103  mpfr_set_prec (x, 53);
104  mpfr_set_prec (y, 53);
105  mpfr_set_str_binary (x, "0.10E4");
106  mpfr_eint (x, x, MPFR_RNDN);
107  mpfr_set_str (y, "440.37989953483827", 10, MPFR_RNDN);
108  if (mpfr_cmp (x, y) != 0)
109    {
110      printf ("Error for x=0.10E4, MPFR_RNDZ\n");
111      printf ("expected "); mpfr_dump (y);
112      printf ("got      "); mpfr_dump (x);
113      exit (1);
114    }
115
116  mpfr_set_prec (x, 63);
117  mpfr_set_prec (y, 63);
118  mpfr_set_str_binary (x, "1.01111101011100111000011010001000101101011000011001111101011010e-2");
119  mpfr_eint (x, x, MPFR_RNDZ);
120  mpfr_set_str_binary (y, "1.11010110001101000001010010000100001111001000100100000001011100e-17");
121  if (mpfr_cmp (x, y) != 0)
122    {
123      printf ("Error (1) for MPFR_RNDZ\n");
124      printf ("expected "); mpfr_dump (y);
125      printf ("got      "); mpfr_dump (x);
126      exit (1);
127    }
128
129  /* check large x */
130  mpfr_set_prec (x, 53);
131  mpfr_set_prec (y, 53);
132  mpfr_set_str_binary (x, "1E6");
133  mpfr_eint (x, x, MPFR_RNDN);
134  mpfr_set_str_binary (y, "10100011110001101001110000110010111000100111010001E37");
135  if (mpfr_cmp (x, y) != 0)
136    {
137      printf ("Error for x=2^6, MPFR_RNDN\n");
138      printf ("expected "); mpfr_dump (y);
139      printf ("got      "); mpfr_dump (x);
140      exit (1);
141    }
142  mpfr_set_str_binary (x, "1E7");
143  mpfr_eint (x, x, MPFR_RNDN);
144  mpfr_set_str_binary (y, "11001100100011110000101001011010110111111011110011E128");
145  if (mpfr_cmp (x, y) != 0)
146    {
147      printf ("Error for x=2^7, MPFR_RNDN\n");
148      printf ("expected "); mpfr_dump (y);
149      printf ("got      "); mpfr_dump (x);
150      exit (1);
151    }
152  mpfr_set_str_binary (x, "1E8");
153  mpfr_eint (x, x, MPFR_RNDN);
154  mpfr_set_str_binary (y, "1010000110000101111111011011000101001000101011101001E310");
155  if (mpfr_cmp (x, y) != 0)
156    {
157      printf ("Error for x=2^8, MPFR_RNDN\n");
158      printf ("expected "); mpfr_dump (y);
159      printf ("got      "); mpfr_dump (x);
160      exit (1);
161    }
162  mpfr_set_str_binary (x, "1E9");
163  mpfr_eint (x, x, MPFR_RNDN);
164  mpfr_set_str_binary (y, "11001010101000001010101101110000010110011101110010101E677");
165  if (mpfr_cmp (x, y) != 0)
166    {
167      printf ("Error for x=2^9, MPFR_RNDN\n");
168      printf ("expected "); mpfr_dump (y);
169      printf ("got      "); mpfr_dump (x);
170      exit (1);
171    }
172  mpfr_set_str_binary (x, "1E10");
173  mpfr_eint (x, x, MPFR_RNDN);
174  mpfr_set_str_binary (y, "10011111111010010110110101101000101100101010101101101E1415");
175  if (mpfr_cmp (x, y) != 0)
176    {
177      printf ("Error for x=2^10, MPFR_RNDN\n");
178      printf ("expected "); mpfr_dump (y);
179      printf ("got      "); mpfr_dump (x);
180      exit (1);
181    }
182
183  mpfr_clear (x);
184  mpfr_clear (y);
185}
186
187int
188main (int argc, char *argv[])
189{
190  tests_start_mpfr ();
191
192  if (argc != 1) /* teint x [prec] */
193    {
194      mpfr_t x;
195      mpfr_prec_t p;
196      p = (argc < 3) ? 53 : atoi (argv[2]);
197      mpfr_init2 (x, p);
198      mpfr_set_str (x, argv[1], 10, MPFR_RNDN);
199      printf ("eint(");
200      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
201      printf (")=");
202      mpfr_eint (x, x, MPFR_RNDN);
203      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
204      printf ("\n");
205      mpfr_clear (x);
206    }
207  else
208    {
209      check_specials ();
210
211      test_generic (2, 100, 100);
212    }
213
214  tests_end_mpfr ();
215  return 0;
216}
217