1
/* Test file for mpfr_pow, mpfr_pow_ui and mpfr_pow_si.
3
Copyright 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
This file is part of the MPFR Library.
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.
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.
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. */
27
#include "mpfr-test.h"
31
test_pow (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mp_rnd_t rnd_mode)
34
int ok = rnd_mode == GMP_RNDN && mpfr_number_p (b) && mpfr_number_p (c)
35
&& mpfr_get_prec (a) >= 53;
42
res = mpfr_pow (a, b, c, rnd_mode);
52
#define test_pow mpfr_pow
55
#define TEST_FUNCTION test_pow
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"
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"
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);
85
printf ("Error for mpfr_pow_ui (b, b, ...)\n");
89
/* check large exponents */
90
mpfr_set_ui (b, 1, GMP_RNDN);
91
mpfr_pow_ui (a, b, 4294967295UL, GMP_RNDN);
94
mpfr_pow_ui (a, a, 4049053855UL, GMP_RNDN);
95
if (!mpfr_inf_p (a) || (mpfr_sgn (a) >= 0))
97
printf ("Error for (-Inf)^4049053855\n");
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))
105
printf ("Error for (-Inf)^30002752\n");
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)
114
printf ("Error for (1e-1)^MPFR_EMAX_MAX\n");
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))
123
printf ("Error for (1e-10)^MPFR_EMAX_MAX\n");
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)
133
printf ("Error for (1e10)^ULONG_MAX\n");
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))
144
printf ("Error for (0+)^1\n");
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))
153
printf ("Error for (0-)^5\n");
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))
162
printf ("Error for (0-)^6\n");
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)
173
printf ("Error for x^2026876995\n");
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)
184
printf ("Error for x^2055225053\n");
185
printf ("Expected ");
186
mpfr_out_str (stdout, 2, 0, b, GMP_RNDN);
188
mpfr_out_str (stdout, 2, 0, a, GMP_RNDN);
205
mpfr_pow_si (x, x, -1, GMP_RNDN);
206
MPFR_ASSERTN(mpfr_nan_p (x));
209
mpfr_pow_si (x, x, -1, GMP_RNDN);
210
MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
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));
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));
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);
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);
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);
238
check_special_pow_si ()
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))
250
printf("Pow(2E10000000, -10) failed\n");
256
emin = mpfr_get_emin ();
258
mpfr_set_si (a, -2, GMP_RNDN);
259
mpfr_pow_si (b, a, -10000, GMP_RNDN);
260
if (!MPFR_IS_ZERO (b))
262
printf ("Pow_so (1, -10000) doesn't underflow if emin=-10.\n");
267
mpfr_set_emin (emin);
273
check_inexact (mp_prec_t p)
287
for (q = 2; q <= p; q++)
288
for (rnd = 0; rnd < GMP_RND_MAX; rnd++)
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))
297
cmp = mpfr_set (t, z, (mp_rnd_t) rnd) || cmp;
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 ("");
308
if (((inexact == 0) && (cmp != 0)) ||
309
((inexact != 0) && (cmp == 0)))
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 ("");
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);
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))
350
printf ("Error in pow_si(x,x,-2) for x=2\n");
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))
358
printf ("Error in pow(x,x,y) for x=2, y=-2\n");
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");
371
printf ("Error in mpfr_pow (1)\n");
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");
383
test_pow (z, x, y, GMP_RNDN);
386
printf ("Error in mpfr_pow for prec=64, rnd=GMP_RNDN\n");
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))
398
printf ("Error in mpfr_pow for prec=53, rnd=GMP_RNDZ\n");
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);
412
printf ("Error in mpfr_pow for prec=30, rnd=GMP_RNDN\n");
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);
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);
428
mpfr_out_str (stdout, 2, 0, z, GMP_RNDN);
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));
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));
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));
461
mpfr_div_2exp (y, y, 1, GMP_RNDN);
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));
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);
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));
488
particular_cases (void)
491
static const char *name[11] = {
492
"NaN", "+inf", "-inf", "+0", "-0", "+1", "-1", "+2", "-2", "+0.5", "-0.5"};
496
for (i = 0; i < 11; i++)
497
mpfr_init2 (t[i], 2);
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);
509
for (i = 0; i < 11; i++)
510
for (j = 0; j < 11; j++)
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 }
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)
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]);
543
for (i = 0; i < 11; i++)
560
mpfr_set_ui (x, 1, GMP_RNDN);
561
mpfr_set_exp (x, mpfr_get_emin());
563
for (i = 3; i < 10; i++)
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))
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");
588
/* bug found by Ming J. Tsai <mingjt@delvron.us>, 4 Oct 2003 */
590
mpfr_init_set_str (a, "5.1e32", 10, GMP_RNDN);
593
test_pow (b, a, a, GMP_RNDN);
594
if (!(mpfr_inf_p (b) && mpfr_sgn (b) > 0))
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);
612
MPFR_TEST_USE_RANDS ();
619
check_special_pow_si ();
620
for (p = 2; p < 100; p++)
625
test_generic (2, 100, 100);
626
test_generic_ui (2, 100, 100);
627
test_generic_si (2, 100, 100);