1 /* tsum -- test file for the list summation function
3 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
25 #include "mpfr-test.h"
28 check_is_sorted (unsigned long n, mpfr_srcptr *perm)
32 for (i = 0; i < n - 1; i++)
33 if (MPFR_GET_EXP(perm[i]) < MPFR_GET_EXP(perm[i+1]))
39 sum_tab (mpfr_ptr ret, mpfr_t *tab, unsigned long n, mpfr_rnd_t rnd)
44 MPFR_TMP_DECL(marker);
46 MPFR_TMP_MARK(marker);
47 tabtmp = (mpfr_ptr *) MPFR_TMP_ALLOC(n * sizeof(mpfr_srcptr));
48 for (i = 0; i < n; i++)
51 inexact = mpfr_sum (ret, tabtmp, n, rnd);
52 MPFR_TMP_FREE(marker);
58 get_prec_max (mpfr_t *tab, unsigned long n, mpfr_prec_t f)
65 while (MPFR_IS_ZERO (tab[i]))
69 return MPFR_PREC_MIN; /* all values are 0 */
72 if (! mpfr_check (tab[i]))
74 printf ("tab[%lu] is not valid.\n", i);
77 MPFR_ASSERTN (MPFR_IS_FP (tab[i]));
78 min = max = MPFR_GET_EXP(tab[i]);
81 if (! mpfr_check (tab[i]))
83 printf ("tab[%lu] is not valid.\n", i);
86 MPFR_ASSERTN (MPFR_IS_FP (tab[i]));
87 if (! MPFR_IS_ZERO (tab[i]))
89 if (MPFR_GET_EXP(tab[i]) > max)
90 max = MPFR_GET_EXP(tab[i]);
91 if (MPFR_GET_EXP(tab[i]) < min)
92 min = MPFR_GET_EXP(tab[i]);
97 res += __gmpfr_ceil_log2 (n) + 1;
103 algo_exact (mpfr_t somme, mpfr_t *tab, unsigned long n, mpfr_prec_t f)
106 mpfr_prec_t prec_max;
108 prec_max = get_prec_max(tab, n, f);
109 mpfr_set_prec (somme, prec_max);
110 mpfr_set_ui (somme, 0, MPFR_RNDN);
111 for (i = 0; i < n; i++)
113 if (mpfr_add(somme, somme, tab[i], MPFR_RNDN))
115 printf ("FIXME: algo_exact is buggy.\n");
121 /* Test the sorting function */
123 test_sort (mpfr_prec_t f, unsigned long n)
131 tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof (mpfr_t));
132 for (i = 0; i < n; i++)
133 mpfr_init2 (tab[i], f);
134 tabtmp = (mpfr_ptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_ptr));
135 perm = (mpfr_srcptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_srcptr));
137 for (i = 0; i < n; i++)
139 mpfr_urandomb (tab[i], RANDS);
143 mpfr_sum_sort ((mpfr_srcptr *)tabtmp, n, perm);
145 if (check_is_sorted (n, perm) == 0)
147 printf ("mpfr_sum_sort incorrect.\n");
148 for (i = 0; i < n; i++)
154 for (i = 0; i < n; i++)
156 (*__gmp_free_func) (tab, n * sizeof (mpfr_t));
157 (*__gmp_free_func) (tabtmp, n * sizeof(mpfr_ptr));
158 (*__gmp_free_func) (perm, n * sizeof(mpfr_srcptr));
162 test_sum (mpfr_prec_t f, unsigned long n)
164 mpfr_t sum, real_sum, real_non_rounded;
170 tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof(mpfr_t));
171 for (i = 0; i < n; i++)
172 mpfr_init2 (tab[i], f);
173 mpfr_inits2 (f, sum, real_sum, real_non_rounded, (mpfr_ptr) 0);
176 for (i = 0; i < n; i++)
177 mpfr_urandomb (tab[i], RANDS);
178 algo_exact (real_non_rounded, tab, n, f);
179 for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++)
181 sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode);
182 mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode);
183 if (mpfr_cmp (real_sum, sum) != 0)
185 printf ("mpfr_sum incorrect.\n");
186 mpfr_dump (real_sum);
192 /* Then non uniform */
193 for (i = 0; i < n; i++)
195 mpfr_urandomb (tab[i], RANDS);
196 if (! mpfr_zero_p (tab[i]))
197 mpfr_set_exp (tab[i], randlimb () % 1000);
199 algo_exact (real_non_rounded, tab, n, f);
200 for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++)
202 sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode);
203 mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode);
204 if (mpfr_cmp (real_sum, sum) != 0)
206 printf ("mpfr_sum incorrect.\n");
207 mpfr_dump (real_sum);
214 for (i = 0; i < n; i++)
216 mpfr_clears (sum, real_sum, real_non_rounded, (mpfr_ptr) 0);
217 (*__gmp_free_func) (tab, n * sizeof(mpfr_t));
221 void check_special (void)
227 mpfr_inits (tab[0], tab[1], tab[2], r, (mpfr_ptr) 0);
232 i = mpfr_sum (r, tabp, 0, MPFR_RNDN);
233 if (!MPFR_IS_ZERO (r) || !MPFR_IS_POS (r) || i != 0)
235 printf ("Special case n==0 failed!\n");
239 mpfr_set_ui (tab[0], 42, MPFR_RNDN);
240 i = mpfr_sum (r, tabp, 1, MPFR_RNDN);
241 if (mpfr_cmp_ui (r, 42) || i != 0)
243 printf ("Special case n==1 failed!\n");
247 mpfr_set_ui (tab[1], 17, MPFR_RNDN);
248 MPFR_SET_NAN (tab[2]);
249 i = mpfr_sum (r, tabp, 3, MPFR_RNDN);
250 if (!MPFR_IS_NAN (r) || i != 0)
252 printf ("Special case NAN failed!\n");
256 MPFR_SET_INF (tab[2]);
257 MPFR_SET_POS (tab[2]);
258 i = mpfr_sum (r, tabp, 3, MPFR_RNDN);
259 if (!MPFR_IS_INF (r) || !MPFR_IS_POS (r) || i != 0)
261 printf ("Special case +INF failed!\n");
265 MPFR_SET_INF (tab[2]);
266 MPFR_SET_NEG (tab[2]);
267 i = mpfr_sum (r, tabp, 3, MPFR_RNDN);
268 if (!MPFR_IS_INF (r) || !MPFR_IS_NEG (r) || i != 0)
270 printf ("Special case -INF failed!\n");
274 MPFR_SET_ZERO (tab[1]);
275 i = mpfr_sum (r, tabp, 2, MPFR_RNDN);
276 if (mpfr_cmp_ui (r, 42) || i != 0)
278 printf ("Special case 42+0 failed!\n");
282 MPFR_SET_NAN (tab[0]);
283 i = mpfr_sum (r, tabp, 3, MPFR_RNDN);
284 if (!MPFR_IS_NAN (r) || i != 0)
286 printf ("Special case NAN+0+-INF failed!\n");
290 mpfr_set_inf (tab[0], 1);
291 mpfr_set_ui (tab[1], 59, MPFR_RNDN);
292 mpfr_set_inf (tab[2], -1);
293 i = mpfr_sum (r, tabp, 3, MPFR_RNDN);
294 if (!MPFR_IS_NAN (r) || i != 0)
296 printf ("Special case +INF + 59 +-INF failed!\n");
300 mpfr_clears (tab[0], tab[1], tab[2], r, (mpfr_ptr) 0);
313 test_sort (1764, 1026);
314 for (p = 2 ; p < 444 ; p += 17)
315 for (n = 2 ; n < 1026 ; n += 42 + p)