~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tpow.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for mpfr_pow, mpfr_pow_ui and mpfr_pow_si.
 
2
 
 
3
Copyright 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR 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 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The MPFR 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.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <float.h>
 
25
#include <math.h>
 
26
 
 
27
#include "mpfr-test.h"
 
28
 
 
29
#ifdef CHECK_EXTERNAL
 
30
static int
 
31
test_pow (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mp_rnd_t rnd_mode)
 
32
{
 
33
  int res;
 
34
  int ok = rnd_mode == GMP_RNDN && mpfr_number_p (b) && mpfr_number_p (c)
 
35
    && mpfr_get_prec (a) >= 53;
 
36
  if (ok)
 
37
    {
 
38
      mpfr_print_raw (b);
 
39
      printf (" ");
 
40
      mpfr_print_raw (c);
 
41
    }
 
42
  res = mpfr_pow (a, b, c, rnd_mode);
 
43
  if (ok)
 
44
    {
 
45
      printf (" ");
 
46
      mpfr_print_raw (a);
 
47
      printf ("\n");
 
48
    }
 
49
  return res;
 
50
}
 
51
#else
 
52
#define test_pow mpfr_pow
 
53
#endif
 
54
 
 
55
#define TEST_FUNCTION test_pow
 
56
#define TWO_ARGS
 
57
#include "tgeneric.c"
 
58
 
 
59
#define TEST_FUNCTION mpfr_pow_ui
 
60
#define INTEGER_TYPE  unsigned long
 
61
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1)
 
62
#include "tgeneric_ui.c"
 
63
 
 
64
#define TEST_FUNCTION mpfr_pow_si
 
65
#define INTEGER_TYPE  long
 
66
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1)
 
67
#define test_generic_ui test_generic_si
 
68
#include "tgeneric_ui.c"
 
69
 
 
70
static void
 
71
check_pow_ui (void)
 
72
{
 
73
  mpfr_t a, b;
 
74
  int res;
 
75
 
 
76
  mpfr_init2 (a, 53);
 
77
  mpfr_init2 (b, 53);
 
78
 
 
79
  /* check in-place operations */
 
80
  mpfr_set_str (b, "0.6926773", 10, GMP_RNDN);
 
81
  mpfr_pow_ui (a, b, 10, GMP_RNDN);
 
82
  mpfr_pow_ui (b, b, 10, GMP_RNDN);
 
83
  if (mpfr_cmp (a, b))
 
84
    {
 
85
      printf ("Error for mpfr_pow_ui (b, b, ...)\n");
 
86
      exit (1);
 
87
    }
 
88
 
 
89
  /* check large exponents */
 
90
  mpfr_set_ui (b, 1, GMP_RNDN);
 
91
  mpfr_pow_ui (a, b, 4294967295UL, GMP_RNDN);
 
92
 
 
93
  mpfr_set_inf (a, -1);
 
94
  mpfr_pow_ui (a, a, 4049053855UL, GMP_RNDN);
 
95
  if (!mpfr_inf_p (a) || (mpfr_sgn (a) >= 0))
 
96
    {
 
97
      printf ("Error for (-Inf)^4049053855\n");
 
98
      exit (1);
 
99
    }
 
100
 
 
101
  mpfr_set_inf (a, -1);
 
102
  mpfr_pow_ui (a, a, (unsigned long) 30002752, GMP_RNDN);
 
103
  if (!mpfr_inf_p (a) || (mpfr_sgn (a) <= 0))
 
104
    {
 
105
      printf ("Error for (-Inf)^30002752\n");
 
106
      exit (1);
 
107
    }
 
108
 
 
109
  /* Check underflow */
 
110
  mpfr_set_str_binary (a, "1E-1");
 
111
  res = mpfr_pow_ui (a, a, -mpfr_get_emin (), GMP_RNDN);
 
112
  if (MPFR_GET_EXP (a) != mpfr_get_emin () + 1)
 
113
    {
 
114
      printf ("Error for (1e-1)^MPFR_EMAX_MAX\n");
 
115
      mpfr_dump (a);
 
116
      exit (1);
 
117
    }
 
118
 
 
119
  mpfr_set_str_binary (a, "1E-10");
 
120
  res = mpfr_pow_ui (a, a, -mpfr_get_emin (), GMP_RNDZ);
 
121
  if (!MPFR_IS_ZERO (a))
 
122
    {
 
123
      printf ("Error for (1e-10)^MPFR_EMAX_MAX\n");
 
124
      mpfr_dump (a);
 
125
      exit (1);
 
126
    }
 
127
 
 
128
  /* Check overflow */
 
129
  mpfr_set_str_binary (a, "1E10");
 
130
  res = mpfr_pow_ui (a, a, ULONG_MAX, GMP_RNDN);
 
131
  if (!MPFR_IS_INF (a) || MPFR_SIGN (a) < 0)
 
132
    {
 
133
      printf ("Error for (1e10)^ULONG_MAX\n");
 
134
      exit (1);
 
135
    }
 
136
 
 
137
  /* Check 0 */
 
138
  MPFR_SET_ZERO (a);
 
139
  MPFR_SET_POS (a);
 
140
  mpfr_set_si (b, -1, GMP_RNDN);
 
141
  res = mpfr_pow_ui (b, a, 1, GMP_RNDN);
 
142
  if (res != 0 || MPFR_IS_NEG (b))
 
143
    {
 
144
      printf ("Error for (0+)^1\n");
 
145
      exit (1);
 
146
    }
 
147
  MPFR_SET_ZERO (a);
 
148
  MPFR_SET_NEG (a);
 
149
  mpfr_set_ui (b, 1, GMP_RNDN);
 
150
  res = mpfr_pow_ui (b, a, 5, GMP_RNDN);
 
151
  if (res != 0 || MPFR_IS_POS (b))
 
152
    {
 
153
      printf ("Error for (0-)^5\n");
 
154
      exit (1);
 
155
    }
 
156
  MPFR_SET_ZERO (a);
 
157
  MPFR_SET_NEG (a);
 
158
  mpfr_set_si (b, -1, GMP_RNDN);
 
159
  res = mpfr_pow_ui (b, a, 6, GMP_RNDN);
 
160
  if (res != 0 || MPFR_IS_NEG (b))
 
161
    {
 
162
      printf ("Error for (0-)^6\n");
 
163
      exit (1);
 
164
    }
 
165
 
 
166
  mpfr_set_prec (a, 122);
 
167
  mpfr_set_prec (b, 122);
 
168
  mpfr_set_str_binary (a, "0.10000010010000111101001110100101101010011110011100001111000001001101000110011001001001001011001011010110110110101000111011E1");
 
169
  mpfr_set_str_binary (b, "0.11111111100101001001000001000001100011100000001110111111100011111000111011100111111111110100011000111011000100100011001011E51290375");
 
170
  mpfr_pow_ui (a, a, 2026876995UL, GMP_RNDU);
 
171
  if (mpfr_cmp (a, b) != 0)
 
172
    {
 
173
      printf ("Error for x^2026876995\n");
 
174
      exit (1);
 
175
    }
 
176
 
 
177
  mpfr_set_prec (a, 29);
 
178
  mpfr_set_prec (b, 29);
 
179
  mpfr_set_str_binary (a, "1.0000000000000000000000001111");
 
180
  mpfr_set_str_binary (b, "1.1001101111001100111001010111e165");
 
181
  mpfr_pow_ui (a, a, 2055225053, GMP_RNDZ);
 
182
  if (mpfr_cmp (a, b) != 0)
 
183
    {
 
184
      printf ("Error for x^2055225053\n");
 
185
      printf ("Expected ");
 
186
      mpfr_out_str (stdout, 2, 0, b, GMP_RNDN);
 
187
      printf ("\nGot      ");
 
188
      mpfr_out_str (stdout, 2, 0, a, GMP_RNDN);
 
189
      printf ("\n");
 
190
      exit (1);
 
191
    }
 
192
 
 
193
  mpfr_clear (a);
 
194
  mpfr_clear (b);
 
195
}
 
196
 
 
197
static void
 
198
check_pow_si (void)
 
199
{
 
200
  mpfr_t x;
 
201
 
 
202
  mpfr_init (x);
 
203
 
 
204
  mpfr_set_nan (x);
 
205
  mpfr_pow_si (x, x, -1, GMP_RNDN);
 
206
  MPFR_ASSERTN(mpfr_nan_p (x));
 
207
 
 
208
  mpfr_set_inf (x, 1);
 
209
  mpfr_pow_si (x, x, -1, GMP_RNDN);
 
210
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
 
211
 
 
212
  mpfr_set_inf (x, -1);
 
213
  mpfr_pow_si (x, x, -1, GMP_RNDN);
 
214
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG(x));
 
215
 
 
216
  mpfr_set_inf (x, -1);
 
217
  mpfr_pow_si (x, x, -2, GMP_RNDN);
 
218
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
 
219
 
 
220
  mpfr_set_ui (x, 0, GMP_RNDN);
 
221
  mpfr_pow_si (x, x, -1, GMP_RNDN);
 
222
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
223
 
 
224
  mpfr_set_ui (x, 0, GMP_RNDN);
 
225
  mpfr_neg (x, x, GMP_RNDN);
 
226
  mpfr_pow_si (x, x, -1, GMP_RNDN);
 
227
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) < 0);
 
228
 
 
229
  mpfr_set_ui (x, 0, GMP_RNDN);
 
230
  mpfr_neg (x, x, GMP_RNDN);
 
231
  mpfr_pow_si (x, x, -2, GMP_RNDN);
 
232
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
233
 
 
234
  mpfr_clear (x);
 
235
}
 
236
 
 
237
static void
 
238
check_special_pow_si ()
 
239
{
 
240
  mpfr_t a, b;
 
241
  mp_exp_t emin;
 
242
 
 
243
  mpfr_init (a);
 
244
  mpfr_init (b);
 
245
  mpfr_set_str (a, "2E100000000", 10, GMP_RNDN);
 
246
  mpfr_set_si (b, -10, GMP_RNDN);
 
247
  test_pow (b, a, b, GMP_RNDN);
 
248
  if (!MPFR_IS_ZERO(b))
 
249
    {
 
250
      printf("Pow(2E10000000, -10) failed\n");
 
251
      mpfr_dump (a);
 
252
      mpfr_dump (b);
 
253
      exit(1);
 
254
    }
 
255
 
 
256
  emin = mpfr_get_emin ();
 
257
  mpfr_set_emin (-10);
 
258
  mpfr_set_si (a, -2, GMP_RNDN);
 
259
  mpfr_pow_si (b, a, -10000, GMP_RNDN);
 
260
  if (!MPFR_IS_ZERO (b))
 
261
    {
 
262
      printf ("Pow_so (1, -10000) doesn't underflow if emin=-10.\n");
 
263
      mpfr_dump (a);
 
264
      mpfr_dump (b);
 
265
      exit (1);
 
266
    }
 
267
  mpfr_set_emin (emin);
 
268
  mpfr_clear (a);
 
269
  mpfr_clear (b);
 
270
}
 
271
 
 
272
static void
 
273
check_inexact (mp_prec_t p)
 
274
{
 
275
  mpfr_t x, y, z, t;
 
276
  unsigned long u;
 
277
  mp_prec_t q;
 
278
  int inexact, cmp;
 
279
  int rnd;
 
280
 
 
281
  mpfr_init2 (x, p);
 
282
  mpfr_init (y);
 
283
  mpfr_init (z);
 
284
  mpfr_init (t);
 
285
  mpfr_random (x);
 
286
  u = randlimb () % 2;
 
287
  for (q = 2; q <= p; q++)
 
288
    for (rnd = 0; rnd < GMP_RND_MAX; rnd++)
 
289
      {
 
290
        mpfr_set_prec (y, q);
 
291
        mpfr_set_prec (z, q + 10);
 
292
        mpfr_set_prec (t, q);
 
293
        inexact = mpfr_pow_ui (y, x, u, (mp_rnd_t) rnd);
 
294
        cmp = mpfr_pow_ui (z, x, u, (mp_rnd_t) rnd);
 
295
        if (mpfr_can_round (z, q + 10, (mp_rnd_t) rnd, (mp_rnd_t) rnd, q))
 
296
          {
 
297
            cmp = mpfr_set (t, z, (mp_rnd_t) rnd) || cmp;
 
298
            if (mpfr_cmp (y, t))
 
299
              {
 
300
                printf ("results differ for u=%lu rnd=%s\n",
 
301
                        u, mpfr_print_rnd_mode ((mp_rnd_t) rnd));
 
302
                printf ("x="); mpfr_print_binary (x); puts ("");
 
303
                printf ("y="); mpfr_print_binary (y); puts ("");
 
304
                printf ("t="); mpfr_print_binary (t); puts ("");
 
305
                printf ("z="); mpfr_print_binary (z); puts ("");
 
306
                exit (1);
 
307
              }
 
308
            if (((inexact == 0) && (cmp != 0)) ||
 
309
                ((inexact != 0) && (cmp == 0)))
 
310
              {
 
311
                printf ("Wrong inexact flag for p=%u, q=%u, rnd=%s\n",
 
312
                        (unsigned int) p, (unsigned int) q,
 
313
                        mpfr_print_rnd_mode ((mp_rnd_t) rnd));
 
314
                printf ("expected %d, got %d\n", cmp, inexact);
 
315
                printf ("u=%lu x=", u); mpfr_print_binary (x); puts ("");
 
316
                printf ("y="); mpfr_print_binary (y); puts ("");
 
317
                exit (1);
 
318
              }
 
319
          }
 
320
      }
 
321
 
 
322
  /* check exact power */
 
323
  mpfr_set_prec (x, p);
 
324
  mpfr_set_prec (y, p);
 
325
  mpfr_set_prec (z, p);
 
326
  mpfr_set_ui (x, 4, GMP_RNDN);
 
327
  mpfr_set_str (y, "0.5", 10, GMP_RNDN);
 
328
  test_pow (z, x, y, GMP_RNDZ);
 
329
 
 
330
  mpfr_clear (x);
 
331
  mpfr_clear (y);
 
332
  mpfr_clear (z);
 
333
  mpfr_clear (t);
 
334
}
 
335
 
 
336
static void
 
337
special ()
 
338
{
 
339
  mpfr_t x, y, z, t;
 
340
 
 
341
  mpfr_init2 (x, 53);
 
342
  mpfr_init2 (y, 53);
 
343
  mpfr_init2 (z, 53);
 
344
  mpfr_init2 (t, 2);
 
345
 
 
346
  mpfr_set_ui (x, 2, GMP_RNDN);
 
347
  mpfr_pow_si (x, x, -2, GMP_RNDN);
 
348
  if (mpfr_cmp_ui_2exp (x, 1, -2))
 
349
    {
 
350
      printf ("Error in pow_si(x,x,-2) for x=2\n");
 
351
      exit (1);
 
352
    }
 
353
  mpfr_set_ui (x, 2, GMP_RNDN);
 
354
  mpfr_set_si (y, -2, GMP_RNDN);
 
355
  test_pow (x, x, y, GMP_RNDN);
 
356
  if (mpfr_cmp_ui_2exp (x, 1, -2))
 
357
    {
 
358
      printf ("Error in pow(x,x,y) for x=2, y=-2\n");
 
359
      exit (1);
 
360
    }
 
361
 
 
362
  mpfr_set_prec (x, 2);
 
363
  mpfr_set_str_binary (x, "1.0e-1");
 
364
  mpfr_set_prec (y, 53);
 
365
  mpfr_set_str_binary (y, "0.11010110011100101010110011001010100111000001000101110E-1");
 
366
  mpfr_set_prec (z, 2);
 
367
  test_pow (z, x, y, GMP_RNDZ);
 
368
  mpfr_set_str_binary (x, "1.0e-1");
 
369
  if (mpfr_cmp (x, z))
 
370
    {
 
371
      printf ("Error in mpfr_pow (1)\n");
 
372
      exit (1);
 
373
    }
 
374
 
 
375
  mpfr_set_prec (x, 64);
 
376
  mpfr_set_prec (y, 64);
 
377
  mpfr_set_prec (z, 64);
 
378
  mpfr_set_prec (t, 64);
 
379
  mpfr_set_str_binary (x, "0.111011000111100000111010000101010100110011010000011");
 
380
  mpfr_set_str_binary (y, "0.111110010100110000011101100011010111000010000100101");
 
381
  mpfr_set_str_binary (t, "0.1110110011110110001000110100100001001111010011111000010000011001");
 
382
 
 
383
  test_pow (z, x, y, GMP_RNDN);
 
384
  if (mpfr_cmp (z, t))
 
385
    {
 
386
      printf ("Error in mpfr_pow for prec=64, rnd=GMP_RNDN\n");
 
387
      exit (1);
 
388
    }
 
389
 
 
390
  mpfr_set_prec (x, 53);
 
391
  mpfr_set_prec (y, 53);
 
392
  mpfr_set_prec (z, 53);
 
393
  mpfr_set_str (x, "5.68824667828621954868e-01", 10, GMP_RNDN);
 
394
  mpfr_set_str (y, "9.03327850535952658895e-01", 10, GMP_RNDN);
 
395
  test_pow (z, x, y, GMP_RNDZ);
 
396
  if (mpfr_cmp_d(z, 0.60071044650456473235))
 
397
    {
 
398
      printf ("Error in mpfr_pow for prec=53, rnd=GMP_RNDZ\n");
 
399
      exit (1);
 
400
    }
 
401
 
 
402
  mpfr_set_prec (t, 2);
 
403
  mpfr_set_prec (x, 30);
 
404
  mpfr_set_prec (y, 30);
 
405
  mpfr_set_prec (z, 30);
 
406
  mpfr_set_str (x, "1.00000000001010111110001111011e1", 2, GMP_RNDN);
 
407
  mpfr_set_str (t, "-0.5", 10, GMP_RNDN);
 
408
  test_pow (z, x, t, GMP_RNDN);
 
409
  mpfr_set_str (y, "1.01101001111010101110000101111e-1", 2, GMP_RNDN);
 
410
  if (mpfr_cmp (z, y))
 
411
    {
 
412
      printf ("Error in mpfr_pow for prec=30, rnd=GMP_RNDN\n");
 
413
      exit (1);
 
414
    }
 
415
 
 
416
  mpfr_set_prec (x, 21);
 
417
  mpfr_set_prec (y, 21);
 
418
  mpfr_set_prec (z, 21);
 
419
  mpfr_set_str (x, "1.11111100100001100101", 2, GMP_RNDN);
 
420
  test_pow (z, x, t, GMP_RNDZ);
 
421
  mpfr_set_str (y, "1.01101011010001100000e-1", 2, GMP_RNDN);
 
422
  if (mpfr_cmp (z, y))
 
423
    {
 
424
      printf ("Error in mpfr_pow for prec=21, rnd=GMP_RNDZ\n");
 
425
      printf ("Expected ");
 
426
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
427
      printf ("\nGot      ");
 
428
      mpfr_out_str (stdout, 2, 0, z, GMP_RNDN);
 
429
      printf ("\n");
 
430
      exit (1);
 
431
    }
 
432
 
 
433
  mpfr_set_inf (x, 1);
 
434
  mpfr_set_prec (y, 2);
 
435
  mpfr_set_str_binary (y, "1E10");
 
436
  test_pow (z, x, y, GMP_RNDN);
 
437
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
438
  mpfr_set_inf (x, -1);
 
439
  test_pow (z, x, y, GMP_RNDN);
 
440
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
441
  mpfr_set_prec (y, 10);
 
442
  mpfr_set_str_binary (y, "1.000000001E9");
 
443
  test_pow (z, x, y, GMP_RNDN);
 
444
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_NEG(z));
 
445
  mpfr_set_str_binary (y, "1.000000001E8");
 
446
  test_pow (z, x, y, GMP_RNDN);
 
447
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
448
 
 
449
  mpfr_set_inf (x, -1);
 
450
  mpfr_set_prec (y, 2 * mp_bits_per_limb);
 
451
  mpfr_set_ui (y, 1, GMP_RNDN);
 
452
  mpfr_mul_2exp (y, y, mp_bits_per_limb - 1, GMP_RNDN);
 
453
  /* y = 2^(mp_bits_per_limb - 1) */
 
454
  test_pow (z, x, y, GMP_RNDN);
 
455
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
456
  mpfr_nextabove (y);
 
457
  test_pow (z, x, y, GMP_RNDN);
 
458
  /* y = 2^(mp_bits_per_limb - 1) + epsilon */
 
459
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
460
  mpfr_nextbelow (y);
 
461
  mpfr_div_2exp (y, y, 1, GMP_RNDN);
 
462
  mpfr_nextabove (y);
 
463
  test_pow (z, x, y, GMP_RNDN);
 
464
  /* y = 2^(mp_bits_per_limb - 2) + epsilon */
 
465
  MPFR_ASSERTN(mpfr_inf_p (z) && MPFR_IS_POS(z));
 
466
 
 
467
  mpfr_set_si (x, -1, GMP_RNDN);
 
468
  mpfr_set_prec (y, 2);
 
469
  mpfr_set_str_binary (y, "1E10");
 
470
  test_pow (z, x, y, GMP_RNDN);
 
471
  MPFR_ASSERTN(mpfr_cmp_ui (z, 1) == 0);
 
472
 
 
473
  /* Check (-0)^(17.0001) */
 
474
  mpfr_set_prec (x, 6);
 
475
  mpfr_set_prec (y, 640);
 
476
  MPFR_SET_ZERO (x); MPFR_SET_NEG (x);
 
477
  mpfr_set_ui (y, 17, GMP_RNDN); mpfr_nextabove (y);
 
478
  test_pow (z, x, y, GMP_RNDN);
 
479
  MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z));
 
480
 
 
481
  mpfr_clear (x);
 
482
  mpfr_clear (y);
 
483
  mpfr_clear (z);
 
484
  mpfr_clear (t);
 
485
}
 
486
 
 
487
static void
 
488
particular_cases (void)
 
489
{
 
490
  mpfr_t t[11], r;
 
491
  static const char *name[11] = {
 
492
    "NaN", "+inf", "-inf", "+0", "-0", "+1", "-1", "+2", "-2", "+0.5", "-0.5"};
 
493
  int i, j;
 
494
  int error = 0;
 
495
 
 
496
  for (i = 0; i < 11; i++)
 
497
    mpfr_init2 (t[i], 2);
 
498
  mpfr_init2 (r, 6);
 
499
 
 
500
  mpfr_set_nan (t[0]);
 
501
  mpfr_set_inf (t[1], 1);
 
502
  mpfr_set_ui (t[3], 0, GMP_RNDN);
 
503
  mpfr_set_ui (t[5], 1, GMP_RNDN);
 
504
  mpfr_set_ui (t[7], 2, GMP_RNDN);
 
505
  mpfr_div_2ui (t[9], t[5], 1, GMP_RNDN);
 
506
  for (i = 1; i < 11; i += 2)
 
507
    mpfr_neg (t[i+1], t[i], GMP_RNDN);
 
508
 
 
509
  for (i = 0; i < 11; i++)
 
510
    for (j = 0; j < 11; j++)
 
511
      {
 
512
        double d;
 
513
        int p;
 
514
        static int q[11][11] = {
 
515
          /*          NaN +inf -inf  +0   -0   +1   -1   +2   -2  +0.5 -0.5 */
 
516
          /*  NaN */ { 0,   0,   0,  128, 128,  0,   0,   0,   0,   0,   0  },
 
517
          /* +inf */ { 0,   1,   2,  128, 128,  1,   2,   1,   2,   1,   2  },
 
518
          /* -inf */ { 0,   1,   2,  128, 128, -1,  -2,   1,   2,   1,   2  },
 
519
          /*  +0  */ { 0,   2,   1,  128, 128,  2,   1,   2,   1,   2,   1  },
 
520
          /*  -0  */ { 0,   2,   1,  128, 128, -2,  -1,   2,   1,   2,   1  },
 
521
          /*  +1  */ {128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 },
 
522
          /*  -1  */ { 0,  128, 128, 128, 128,-128,-128, 128, 128,  0,   0  },
 
523
          /*  +2  */ { 0,   1,   2,  128, 128, 256,  64, 512,  32, 180,  90 },
 
524
          /*  -2  */ { 0,   1,   2,  128, 128,-256, -64, 512,  32,  0,   0  },
 
525
          /* +0.5 */ { 0,   2,   1,  128, 128,  64, 256,  32, 512,  90, 180 },
 
526
          /* -0.5 */ { 0,   2,   1,  128, 128, -64,-256,  32, 512,  0,   0  }
 
527
        };
 
528
        test_pow (r, t[i], t[j], GMP_RNDN);
 
529
        p = mpfr_nan_p (r) ? 0 : mpfr_inf_p (r) ? 1 :
 
530
          mpfr_cmp_ui (r, 0) == 0 ? 2 :
 
531
          (d = mpfr_get_d (r, GMP_RNDN), (int) (ABS(d) * 128.0));
 
532
        if (p != 0 && MPFR_SIGN(r) < 0)
 
533
          p = -p;
 
534
        if (p != q[i][j])
 
535
          {
 
536
            printf ("Error in mpfr_pow for particular case (%s)^(%s) (%d,%d):\n"
 
537
                    "got %d instead of %d\n", name[i], name[j], i,j,p, q[i][j]);
 
538
            mpfr_dump (r);
 
539
            error = 1;
 
540
          }
 
541
      }
 
542
 
 
543
  for (i = 0; i < 11; i++)
 
544
    mpfr_clear (t[i]);
 
545
  mpfr_clear (r);
 
546
 
 
547
  if (error)
 
548
    exit (1);
 
549
}
 
550
 
 
551
static void
 
552
underflows (void)
 
553
{
 
554
  mpfr_t x, y;
 
555
  int i;
 
556
 
 
557
  mpfr_init2 (x, 64);
 
558
  mpfr_init2 (y, 64);
 
559
 
 
560
  mpfr_set_ui (x, 1, GMP_RNDN);
 
561
  mpfr_set_exp (x, mpfr_get_emin());
 
562
 
 
563
  for (i = 3; i < 10; i++)
 
564
    {
 
565
      mpfr_set_ui (y, i, GMP_RNDN);
 
566
      mpfr_div_2ui (y, y, 1, GMP_RNDN);
 
567
      test_pow (y, x, y, GMP_RNDN);
 
568
      if (!MPFR_IS_FP(y) || mpfr_cmp_ui (y, 0))
 
569
        {
 
570
          printf ("Error in mpfr_pow for ");
 
571
          mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
572
          printf (" ^ (%d/2)\nGot ", i);
 
573
          mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
574
          printf (" instead of 0.\n");
 
575
          exit (1);
 
576
        }
 
577
    }
 
578
 
 
579
  mpfr_clear (x);
 
580
  mpfr_clear (y);
 
581
}
 
582
 
 
583
static void
 
584
overflows (void)
 
585
{
 
586
  mpfr_t a, b;
 
587
 
 
588
  /* bug found by Ming J. Tsai <mingjt@delvron.us>, 4 Oct 2003 */
 
589
 
 
590
  mpfr_init_set_str (a, "5.1e32", 10, GMP_RNDN);
 
591
  mpfr_init (b);
 
592
 
 
593
  test_pow (b, a, a, GMP_RNDN);
 
594
  if (!(mpfr_inf_p (b) && mpfr_sgn (b) > 0))
 
595
    {
 
596
      printf ("Error for a^a for a=5.1e32\n");
 
597
      printf ("Expected +Inf, got ");
 
598
      mpfr_out_str (stdout, 10, 0, b, GMP_RNDN);
 
599
      printf ("\n");
 
600
      exit (1);
 
601
    }
 
602
 
 
603
  mpfr_clear(a);
 
604
  mpfr_clear(b);
 
605
}
 
606
 
 
607
int
 
608
main (void)
 
609
{
 
610
  mp_prec_t p;
 
611
 
 
612
  MPFR_TEST_USE_RANDS ();
 
613
  tests_start_mpfr ();
 
614
 
 
615
  special ();
 
616
  particular_cases ();
 
617
  check_pow_ui ();
 
618
  check_pow_si ();
 
619
  check_special_pow_si ();
 
620
  for (p = 2; p < 100; p++)
 
621
    check_inexact (p);
 
622
  underflows ();
 
623
  overflows ();
 
624
 
 
625
  test_generic (2, 100, 100);
 
626
  test_generic_ui (2, 100, 100);
 
627
  test_generic_si (2, 100, 100);
 
628
 
 
629
  tests_end_mpfr ();
 
630
  return 0;
 
631
}