File: tpow_z.c

package info (click to toggle)
mpfr4 4.0.2-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 11,632 kB
  • sloc: ansic: 94,960; sh: 4,549; makefile: 322; perl: 74
file content (377 lines) | stat: -rw-r--r-- 10,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Test file for mpfr_pow_z -- power function x^z with z a MPZ

Copyright 2005-2019 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#include "mpfr-test.h"

#define PRINT_ERROR(str) \
  do { printf ("Error for %s\n", str); exit (1); } while (0)

static void
check_special (void)
{
  mpfr_t x, y;
  mpz_t  z;
  int res;

  mpfr_init (x);
  mpfr_init (y);
  mpz_init (z);

  /* x^0 = 1 except for NAN */
  mpfr_set_ui (x, 23, MPFR_RNDN);
  mpz_set_ui (z, 0);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_cmp_ui (y, 1) != 0)
    PRINT_ERROR ("23^0");
  mpfr_set_nan (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_nan_p (y) || mpfr_cmp_si (y, 1) != 0)
    PRINT_ERROR ("NAN^0");
  mpfr_set_inf (x, 1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_cmp_ui (y, 1) != 0)
    PRINT_ERROR ("INF^0");

  /* sINF^N = INF if s==1 or n even if N > 0*/
  mpz_set_ui (z, 42);
  mpfr_set_inf (x, 1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0)
    PRINT_ERROR ("INF^42");
  mpfr_set_inf (x, -1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0)
    PRINT_ERROR ("-INF^42");
  mpz_set_ui (z, 17);
  mpfr_set_inf (x, 1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0)
    PRINT_ERROR ("INF^17");
  mpfr_set_inf (x, -1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || mpfr_sgn (y) >= 0)
    PRINT_ERROR ("-INF^17");

  mpz_set_si (z, -42);
  mpfr_set_inf (x, 1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("INF^-42");
  mpfr_set_inf (x, -1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("-INF^-42");
  mpz_set_si (z, -17);
  mpfr_set_inf (x, 1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("INF^-17");
  mpfr_set_inf (x, -1);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_POS (y))
    PRINT_ERROR ("-INF^-17");

  /* s0^N = +0 if s==+ or n even if N > 0*/
  mpz_set_ui (z, 42);
  MPFR_SET_ZERO (x); MPFR_SET_POS (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("+0^42");
  MPFR_SET_NEG (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("-0^42");
  mpz_set_ui (z, 17);
  MPFR_SET_POS (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("+0^17");
  MPFR_SET_NEG (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_zero_p (y) == 0 || MPFR_IS_POS (y))
    PRINT_ERROR ("-0^17");

  mpz_set_si (z, -42);
  MPFR_SET_ZERO (x); MPFR_SET_POS (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("+0^-42");
  MPFR_SET_NEG (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("-0^-42");
  mpz_set_si (z, -17);
  MPFR_SET_POS (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || MPFR_IS_NEG (y))
    PRINT_ERROR ("+0^-17");
  MPFR_SET_NEG (x);
  res = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (res != 0 || mpfr_inf_p (y) == 0 || MPFR_IS_POS (y))
    PRINT_ERROR ("-0^-17");

  mpz_clear (z);
  mpfr_clear (y);
  mpfr_clear (x);
}

static void
check_integer (mpfr_prec_t begin, mpfr_prec_t end, unsigned long max)
{
  mpfr_t x, y1, y2;
  mpz_t z;
  unsigned long i, n;
  mpfr_prec_t p;
  int res1, res2;
  mpfr_rnd_t rnd;

  mpfr_inits2 (begin, x, y1, y2, (mpfr_ptr) 0);
  mpz_init (z);
  for (p = begin ; p < end ; p+=4)
    {
      mpfr_set_prec (x, p);
      mpfr_set_prec (y1, p);
      mpfr_set_prec (y2, p);
      for (i = 0 ; i < max ; i++)
        {
          mpz_urandomb (z, RANDS, GMP_NUMB_BITS);
          if ((i & 1) != 0)
            mpz_neg (z, z);
          mpfr_urandomb (x, RANDS);
          mpfr_mul_2ui (x, x, 1, MPFR_RNDN); /* 0 <= x < 2 */
          rnd = RND_RAND ();
          if (mpz_fits_slong_p (z))
            {
              n = mpz_get_si (z);
              /* printf ("New test for x=%ld\nCheck Pow_si\n", n); */
              res1 = mpfr_pow_si (y1, x, n, rnd);
              /* printf ("Check pow_z\n"); */
              res2 = mpfr_pow_z  (y2, x, z, rnd);
              if (mpfr_cmp (y1, y2) != 0)
                {
                  printf ("Error for p = %lu, z = %lu, rnd = %s and x = ",
                          (unsigned long) p, n, mpfr_print_rnd_mode (rnd));
                  mpfr_dump (x);
                  printf ("Ypowsi = "); mpfr_dump (y1);
                  printf ("Ypowz  = "); mpfr_dump (y2);
                  exit (1);
                }
              if (res1 != res2)
                {
                  printf ("Wrong inexact flags for p = %lu, z = %lu, rnd = %s"
                          " and x = ", (unsigned long) p, n,
                          mpfr_print_rnd_mode (rnd));
                  mpfr_dump (x);
                  printf ("Ypowsi(inex = %2d) = ", res1); mpfr_dump (y1);
                  printf ("Ypowz (inex = %2d) = ", res2); mpfr_dump (y2);
                  exit (1);
                }
            }
        } /* for i */
    } /* for p */
  mpfr_clears (x, y1, y2, (mpfr_ptr) 0);
  mpz_clear (z);
}

static void
check_regression (void)
{
  mpfr_t x, y;
  mpz_t  z;
  int res1, res2;

  mpz_init_set_ui (z, 2026876995);
  mpfr_init2 (x, 122);
  mpfr_init2 (y, 122);

  mpfr_set_str_binary (x, "0.10000010010000111101001110100101101010011110011100001111000001001101000110011001001001001011001011010110110110101000111011E1");
  res1 = mpfr_pow_z (y, x, z, MPFR_RNDU);
  res2 = mpfr_pow_ui (x, x, 2026876995UL, MPFR_RNDU);
  if (mpfr_cmp (x, y) || res1 != res2)
    {
      printf ("Regression (1) tested failed (%d=?%d)\n",res1, res2);
      printf ("pow_ui: "); mpfr_dump (x);
      printf ("pow_z:  "); mpfr_dump (y);

      exit (1);
    }

  mpfr_clear (x);
  mpfr_clear (y);
  mpz_clear (z);
}

/* Bug found by Kevin P. Rauch */
static void
bug20071104 (void)
{
  mpfr_t x, y;
  mpz_t z;
  int inex;

  mpz_init_set_si (z, -2);
  mpfr_inits2 (20, x, y, (mpfr_ptr) 0);
  mpfr_set_ui (x, 0, MPFR_RNDN);
  mpfr_nextbelow (x);  /* x = -2^(emin-1) */
  mpfr_clear_flags ();
  inex = mpfr_pow_z (y, x, z, MPFR_RNDN);
  if (! mpfr_inf_p (y) || MPFR_IS_NEG (y))
    {
      printf ("Error in bug20071104: expected +Inf, got ");
      mpfr_dump (y);
      exit (1);
    }
  if (inex <= 0)
    {
      printf ("Error in bug20071104: bad ternary value (%d)\n", inex);
      exit (1);
    }
  if (__gmpfr_flags != (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT))
    {
      printf ("Error in bug20071104: bad flags (%u)\n",
              (unsigned int) __gmpfr_flags);
      exit (1);
    }
  mpfr_clears (x, y, (mpfr_ptr) 0);
  mpz_clear (z);
}

static void
check_overflow (void)
{
  mpfr_t a;
  mpz_t z;
  unsigned long n;
  int res;

  mpfr_init2 (a, 53);

  mpfr_set_str_binary (a, "1E10");
  mpz_init_set_ui (z, ULONG_MAX);
  res = mpfr_pow_z (a, a, z, MPFR_RNDN);
  if (! MPFR_IS_INF (a) || MPFR_IS_NEG (a) || res <= 0)
    {
      printf ("Error for (1e10)^ULONG_MAX, expected +Inf,\ngot ");
      mpfr_dump (a);
      exit (1);
    }

  /* Bug in pow_z.c up to r5109: if x = y (same mpfr_t argument), the
     input argument is negative and not a power of two, z is positive
     and odd, an overflow or underflow occurs, and the temporary result
     res is positive, then the result gets a wrong sign (positive
     instead of negative). */
  mpfr_set_str_binary (a, "-1.1E10");
  n = (ULONG_MAX ^ (ULONG_MAX >> 1)) + 1;
  mpz_set_ui (z, n);
  res = mpfr_pow_z (a, a, z, MPFR_RNDN);
  if (! MPFR_IS_INF (a) || MPFR_IS_POS (a) || res >= 0)
    {
      printf ("Error for (-1e10)^%lu, expected -Inf,\ngot ", n);
      mpfr_dump (a);
      exit (1);
    }

  mpfr_clear (a);
  mpz_clear (z);
}

/* bug reported by Carl Witty (32-bit architecture) */
static void
bug20080223 (void)
{
  mpfr_t a, exp, answer;

  mpfr_init2 (a, 53);
  mpfr_init2 (exp, 53);
  mpfr_init2 (answer, 53);

  mpfr_set_si (exp, -1073741824, MPFR_RNDN);

  mpfr_set_str (a, "1.999999999", 10, MPFR_RNDN);
  /* a = 562949953139837/2^48 */
  mpfr_pow (answer, a, exp, MPFR_RNDN);
  mpfr_set_str_binary (a, "0.110110101111011001110000111111100011101000111011101E-1073741823");
  MPFR_ASSERTN(mpfr_cmp0 (answer, a) == 0);

  mpfr_clear (a);
  mpfr_clear (exp);
  mpfr_clear (answer);
}

static void
bug20080904 (void)
{
  mpz_t exp;
  mpfr_t a, answer;
  mpfr_exp_t emin_default;

  mpz_init (exp);
  mpfr_init2 (a, 70);
  mpfr_init2 (answer, 70);

  emin_default = mpfr_get_emin ();
  mpfr_set_emin (MPFR_EMIN_MIN);

  mpz_set_str (exp, "-4eb92f8c7b7bf81e", 16);
  mpfr_set_str_binary (a, "1.110000101110100110100011111000011110111101000011111001111001010011100");

  mpfr_pow_z (answer, a, exp, MPFR_RNDN);
  /* The correct result is near 2^(-2^62), so it underflows when
     MPFR_EMIN_MIN > -2^62 (i.e. with 32 and 64 bits machines). */
  mpfr_set_str (a, "AA500C0D7A69275DBp-4632850503556296886", 16, MPFR_RNDN);
  if (! mpfr_equal_p (answer, a))
    {
      printf ("Error in bug20080904:\n");
      printf ("Expected ");
      mpfr_out_str (stdout, 16, 0, a, MPFR_RNDN);
      putchar ('\n');
      printf ("Got      ");
      mpfr_out_str (stdout, 16, 0, answer, MPFR_RNDN);
      putchar ('\n');
      exit (1);
    }

  mpfr_set_emin (emin_default);

  mpz_clear (exp);
  mpfr_clear (a);
  mpfr_clear (answer);
}

int
main (void)
{
  tests_start_mpfr ();

  check_special ();

  check_integer (2, 163, 100);
  check_regression ();
  bug20071104 ();
  bug20080223 ();
  bug20080904 ();
  check_overflow ();

  tests_end_mpfr ();
  return 0;
}