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