1 /* Exercise mpz_bin_ui and mpz_bin_uiui.
3 Copyright 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 try_mpz_bin_ui (mpz_srcptr want, mpz_srcptr n, unsigned long k)
33 mpz_bin_ui (got, n, k);
34 MPZ_CHECK_FORMAT (got);
35 if (mpz_cmp (got, want) != 0)
37 printf ("mpz_bin_ui wrong\n");
38 printf (" n="); mpz_out_str (stdout, 10, n); printf ("\n");
39 printf (" k=%lu\n", k);
40 printf (" got="); mpz_out_str (stdout, 10, got); printf ("\n");
41 printf (" want="); mpz_out_str (stdout, 10, want); printf ("\n");
49 try_mpz_bin_uiui (mpz_srcptr want, unsigned long n, unsigned long k)
54 mpz_bin_uiui (got, n, k);
55 MPZ_CHECK_FORMAT (got);
56 if (mpz_cmp (got, want) != 0)
58 printf ("mpz_bin_uiui wrong\n");
59 printf (" n=%lu\n", n);
60 printf (" k=%lu\n", k);
61 printf (" got="); mpz_out_str (stdout, 10, got); printf ("\n");
62 printf (" want="); mpz_out_str (stdout, 10, want); printf ("\n");
105 { "3", 123456, "0" },
114 { "4", 123456, "0" },
129 { "10", 123456, "0" },
131 /* negatives, using bin(-n,k)=bin(n+k-1,k) */
154 { "40", 20, "137846528820" },
155 { "60", 30, "118264581564861424" },
164 for (i = 0; i < numberof (data); i++)
166 mpz_set_str_or_abort (n, data[i].n, 0);
167 mpz_set_str_or_abort (want, data[i].want, 0);
169 try_mpz_bin_ui (want, n, data[i].k);
171 if (mpz_fits_ulong_p (n))
172 try_mpz_bin_uiui (want, mpz_get_ui (n), data[i].k);
180 /* Test some bin(2k,k) cases. This produces some biggish numbers to
181 exercise the limb accumulating code. */
191 mpz_set_ui (want, (unsigned long) 2);
192 for (k = 1; k < 200; k++)
195 try_mpz_bin_ui (want, n, k);
197 try_mpz_bin_uiui (want, 2*k, k);
199 mpz_mul_ui (want, want, 2*(2*k+1));
200 mpz_fdiv_q_ui (want, want, k+1);