1/* tpow_ui -- test file for mpc_pow_ui.
2
3Copyright (C) 2009, 2010 INRIA
4
5This file is part of GNU MPC.
6
7GNU MPC is free software; you can redistribute it and/or modify it under
8the terms of the GNU Lesser General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with this program. If not, see http://www.gnu.org/licenses/ .
19*/
20
21#include <limits.h> /* for CHAR_BIT */
22#include "mpc-tests.h"
23
24static void
25compare_mpc_pow (mpfr_prec_t pmax, int iter, unsigned long nbits)
26{
27  mpfr_prec_t p;
28  mpc_t x, y, z, t;
29  unsigned long n;
30  int i, inex_pow, inex_pow_ui;
31  mpc_rnd_t rnd;
32
33  mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN);
34  for (p = MPFR_PREC_MIN; p <= pmax; p++)
35    for (i = 0; i < iter; i++)
36      {
37        mpc_init2 (x, p);
38        mpc_init2 (z, p);
39        mpc_init2 (t, p);
40        mpc_urandom (x, rands);
41        n = gmp_urandomb_ui (rands, nbits); /* 0 <= n < 2^nbits */
42        mpc_set_ui (y, n, MPC_RNDNN);
43        for (rnd = 0; rnd < 16; rnd ++)
44          {
45            inex_pow = mpc_pow (z, x, y, rnd);
46            inex_pow_ui = mpc_pow_ui (t, x, n, rnd);
47            if (mpc_cmp (z, t) != 0)
48              {
49                printf ("mpc_pow and mpc_pow_ui differ for x=");
50                mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
51                printf (" n=%lu\n", n);
52                printf ("mpc_pow gives ");
53                mpc_out_str (stdout, 10, 0, z, MPC_RNDNN);
54                printf ("\nmpc_pow_ui gives ");
55                mpc_out_str (stdout, 10, 0, t, MPC_RNDNN);
56                printf ("\n");
57                exit (1);
58              }
59            if (inex_pow != inex_pow_ui)
60              {
61                printf ("mpc_pow and mpc_pow_ui give different flags for x=");
62                mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
63                printf (" n=%lu\n", n);
64                printf ("mpc_pow gives %d\n", inex_pow);
65                printf ("mpc_pow_ui gives %d\n", inex_pow_ui);
66                exit (1);
67              }
68          }
69        mpc_clear (x);
70        mpc_clear (z);
71        mpc_clear (t);
72      }
73  mpc_clear (y);
74}
75
76int
77main (int argc, char *argv[])
78{
79  mpc_t z;
80
81  DECL_FUNC (CCU, f, mpc_pow_ui);
82
83  if (argc != 1)
84    {
85      mpfr_prec_t p;
86      long int n, k;
87      mpc_t res;
88      if (argc != 3 && argc != 4)
89        {
90          printf ("Usage: tpow_ui precision exponent [k]\n");
91          exit (1);
92        }
93      p = atoi (argv[1]);
94      n = atoi (argv[2]);
95      MPC_ASSERT (n >= 0);
96      k = (argc > 3) ? atoi (argv[3]) : 1;
97      MPC_ASSERT (k >= 0);
98      mpc_init2 (z, p);
99      mpc_init2 (res, p);
100      mpfr_const_pi (mpc_realref (z), GMP_RNDN);
101      mpfr_div_2exp (mpc_realref (z), mpc_realref (z), 2, GMP_RNDN);
102      mpfr_const_log2 (mpc_imagref (z), GMP_RNDN);
103      while (k--)
104        mpc_pow_ui (res, z, (unsigned long int) n, MPC_RNDNN);
105      mpc_clear (z);
106      mpc_clear (res);
107      return 0;
108    }
109
110  test_start ();
111  data_check (f, "pow_ui.dat");
112
113  compare_mpc_pow (100, 5, 19);
114
115  test_end ();
116
117  return 0;
118}
119