File: tinit_set.c

package info (click to toggle)
mpfi 1.5.3%2Bds-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,408 kB
  • sloc: ansic: 14,992; makefile: 156; sh: 6
file content (437 lines) | stat: -rw-r--r-- 13,495 bytes parent folder | download | duplicates (7)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* tinit_set.c -- Test file for mpfi_init_set functions.

Copyright 2009,
                     Spaces project, Inria Lorraine
                     and Salsa project, INRIA Rocquencourt,
                     and Arenaire project, Inria Rhone-Alpes, France
                     and Lab. ANO, USTL (Univ. of Lille),  France


This file is part of the MPFI Library.

The MPFI 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 2.1 of the License, or (at your
option) any later version.

The MPFI 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 MPFI Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */

#include <math.h>
#include "mpfi-tests.h"

void
check_endpoints (mpfi_ptr i, const char *left, const char *right,
                 const char *function_name)
{
  mpfr_t l,r;

  mpfr_inits2 (mpfi_get_prec (i), l, r, (mpfr_ptr)0);

  mpfr_set_str (l, left, 0, MPFI_RNDD);
  mpfr_set_str (r, right, 0, MPFI_RNDU);

  if (!mpfr_equal_p (&(i->left), l) || !mpfr_equal_p (&(i->right), r)) {
    printf ("Error in %s\nexpected [%s, %s]\n     got ",
            function_name, left, right);
    mpfi_out_str (stdout, 16, 0, i);
    putchar ('\n');
    exit (1);
  }

  mpfr_clears (l, r, (mpfr_ptr)0);
}

void
check_ui (unsigned long ui, int expected_inex,
          const char *left, const char *right)
{
  int inex;
  mpfi_t fi;

  inex = mpfi_init_set_ui (fi, ui);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_ui returns unexpected value with input=%lu and "
            "output precision=%lu\nexpected return value: %u, got: %u\n",
            ui, mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_ui");
  if (ui == 0 && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_ui does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  mpfi_clear (fi);
}

void
check_si (long si, int expected_inex,
          const char *left, const char *right)
{
  mpfi_t fi;
  int inex;
  inex = mpfi_init_set_si (fi, si);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_si returns unexpected value with input=%lu and "
            "output precision=%lu\nexpected return value: %u, got: %u\n",
            si, mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_si");
  if (si == 0 && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_si does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  mpfi_clear(fi);
}

void
check_d (double d, int expected_inex,
         const char *left, const char *right)
{
  mpfi_t fi;
  int inex;
  inex = mpfi_init_set_d (fi, d);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_d returns unexpected value with input=%g and "
            "output precision=%lu\nexpected return value: %u, got: %u\n",
            d, mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_d");
  if (d == 0.0
      && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_d does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  mpfi_clear(fi);
}

void
check_z (const char *value, int expected_inex,
         const char *left, const char *right)
{
  mpfi_t fi;
  int inex;
  mpz_t z;
  mpz_init (z);

  mpz_set_str (z, value, 0);
  inex = mpfi_init_set_z (fi, z);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_z returns unexpected value with input=");
    mpz_out_str (stdout, 10, z);
    printf (" and output precision=%lu\nexpected return value: %u, got: %u\n",
            mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_z");

  if (mpz_cmp_ui (z,0) == 0
      && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_z does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }

  mpz_clear (z);
  mpfi_clear(fi);
}

void
check_q (const char *value, int expected_inex,
         const char *left, const char *right)
{
  mpfi_t fi;
  int inex;
  mpq_t q;
  mpq_init (q);

  mpq_set_str (q, value, 0);
  inex = mpfi_init_set_q (fi, q);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_q returns unexpected value with input=");
    mpq_out_str (stdout, 10, q);
    printf (" and output precision=%lu\nexpected return value: %u, got: %u\n",
            mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_q");

  if (mpq_cmp_ui (q, 0, 1) == 0
      && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_q does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }

  mpq_clear (q);
  mpfi_clear(fi);
}

void
check_fr (mpfr_ptr fr, int expected_inex,
         const char *left, const char *right)
{
  mpfi_t fi;
  int inex;

  inex = mpfi_init_set_fr (fi, fr);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set_fr returns unexpected value with input=");
    mpfr_out_str (stdout, 10, 0, fr, MPFI_RNDD);
    printf (" and output precision=%lu\nexpected return value: %u, got: %u\n",
            mpfi_get_prec (fi), expected_inex, inex);
    exit (1);
  }
  check_endpoints (fi, left, right, "mpfi_init_set_fr");

  if (mpfr_zero_p (fr)
      && (mpfr_signbit (&(fi->left)) || !mpfr_signbit (&(fi->right)))) {
    printf ("Error: mpfi_init_set_fr does not handle signed zeros correctly\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }

  mpfi_clear(fi);
}

void
check_fi (mpfi_ptr in, int expected_inex,
          const char *left, const char *right)
{
  mpfi_t out;
  int inex;

  inex = mpfi_init_set (out, in);
  if (inex != expected_inex) {
    printf ("Error: mpfi_init_set returns unexpected value with input=");
    mpfi_out_str (stdout, 10, 0, in);
    printf (" and output precision=%lu\nexpected return value: %u, got: %u\n",
            mpfi_get_prec (out), expected_inex, inex);
    exit (1);
  }
  if (!MPFI_NAN_P (in)) {
    check_endpoints (out, left, right, "mpfi_init_set");
  }
  else if (!mpfr_nan_p (&(out->left)) || !mpfr_nan_p (&(out->right))) {
    printf ("Error: mpfi_init_set_fr does not accept NAN\ngot: ");
    mpfi_out_str (stdout, 10, 0, out);
    putchar ('\n');
    exit (1);
  }

  mpfi_clear(out);
}

int
main (int argc, char **argv)
{
  int inex;
  double d;
  mpfr_t fr;
  mpfi_t fi;

  /* check mpfi_init_set_ui */
  mpfr_set_default_prec(2);
  check_ui (0x87654321L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0b1p+31", "0b11p+30");
  mpfr_set_default_prec(16);
  check_ui (0x87654321L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0x8765@+4", "0x8766@+4");
  mpfr_set_default_prec(64);
  check_ui (0x87654321L,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x87654321", "0x87654321");
  check_ui (0,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");


  /* check mpfi_init_set_si */
  mpfr_set_default_prec(2);
  check_si (0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0b10p+18", "0b11p+18");
  check_si (-0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "-0b11p+18", "-0b10p+18");
  mpfr_set_default_prec(16);
  check_si (0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0x8000@+1", "0x8001@+1");
  check_si (-0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "-0x8001@+1", "-0x8000@+1");
  mpfr_set_default_prec(20);
  check_si (0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x80001", "0x80001");
  check_si (-0x80001L,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "-0x80001", "-0x80001");
  check_si (0,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");


  /* check mpfi_init_set_d */
  mpfr_set_default_prec(53);
  check_d (1.0e6,
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "1.0e6", "1.0e6");
  mpfr_set_default_prec(12);
  check_d (0.1,
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0x1999@-4", "0x199A@-4");
  check_d (-0.1,
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "-0x199A@-4", "-0x1999@-4");
  check_d (+0.0,
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");
  check_d (-0.0,
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");

  /* NAN, INFINITY, and DBL_MANT_DIG are symbols defined in math.h or float.h
     in C99 mode */
#ifdef NAN
  d = NAN;
  mpfi_init_set_d (fi, d);
  if (!mpfr_nan_p (&(fi->left)) || !mpfr_nan_p (&(fi->right))) {
    printf ("Error: mpfi_init_set_d does not accept NAN\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  mpfi_clear (fi);
#endif /* NAN */

#ifdef INFINITY
  d = INFINITY;
  inex = mpfi_init_set_d (fi, d);
  if (!MPFI_INF_P (fi)) {
    printf ("Error: mpfi_init_set_d does not accept INFINITY\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  if (!MPFI_BOTH_ARE_EXACT (inex)) {
    printf ("Error: mpfi_init_set_d(INF) returns unexpected value, "
            "expected: %u, got: %u\n", MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, inex);
    exit (1);
  }
  mpfi_clear (fi);
#endif /* INFINITY */

#ifdef DBL_MANT_DIG
  d = 0.1;
  mpfr_set_default_prec (DBL_MANT_DIG);
  inex = mpfi_init_set_d (fi, d);
  if (!MPFI_BOTH_ARE_EXACT (inex)) {
    printf ("Error: mpfi_init_set_d(%g) returns unexpected value, expected: %u, "
            "got: %u\n", d, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, inex);
    exit (1);
  }

# if DBL_MANT_DIG == 53
  check_endpoints (fi, "0x1999999999999A@-14", "0x1999999999999A@-14", "mpfi_init_set_d");
# endif
  mpfi_clear (fi);
#endif /* DBL_MANT_DIG */


  /* check mpfi_init_set_z */
  mpfr_set_default_prec(12);
  check_z ("0xffffffffffffff",
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0xffff@+10", "0x10000@+10");
  check_z ("-0xfeffffffffffff",
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "-0xff00@+10", "-0xfeff@+10");
  mpfr_set_default_prec(56);
  check_z ("0xffffffffffffff",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0xffffffffffffff", "0xffffffffffffff");
  check_z ("-0xfeffffffffffff",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "-0xfeffffffffffff", "-0xfeffffffffffff");
  check_z ("0",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");


  /* check mpfi_init_set_q */
  mpfr_set_default_prec(2);
  check_q ("1/2",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x1p-1", "0x1p-1");
  check_q ("5/2",
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "2", "3");
  mpfr_set_default_prec(53);
  check_q ("-1/2",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "-0x1p-1", "-0x1p-1");
  check_q ("-1/3",
           MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "-0xAAAAAAAAAAAABP-53", "-0x15555555555555P-54");
  check_q ("0/1",
           MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");


  /* check mpfi_init_set_fr */
  mpfr_init2 (fr, 53);

  mpfr_set_default_prec(2);
  mpfr_set_str (fr, "0.5", 10, MPFI_RNDD);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x8@-1", "0x8@-1");
  mpfr_set_str (fr, "0.1", 10, MPFI_RNDU);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0b11p-5", "0b10p-4");

  mpfr_set_default_prec(53);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT,
            "0x1999999999999A@-14", "0x1999999999999A@-14");

  mpfr_set_ui (fr, 0, MPFI_RNDD);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");
  mpfr_neg (fr, fr, MPFI_RNDD);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");
  mpfr_set_inf (fr, -1);
  check_fr (fr, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "-@inf@", "-@inf@");

  mpfr_set_nan (fr);
  mpfi_init_set_fr (fi, fr);
  if (!mpfr_nan_p (&(fi->left)) || !mpfr_nan_p (&(fi->right))) {
    printf ("Error: mpfi_init_set_fr does not accept NAN\ngot: ");
    mpfi_out_str (stdout, 10, 0, fi);
    putchar ('\n');
    exit (1);
  }
  mpfi_clear (fi);
  mpfr_clear (fr);


  /* check mpfi_init_set */
  mpfi_init2 (fi, 53);

  mpfr_set_default_prec(2);
  mpfi_set_str (fi, "0.5", 10);
  check_fi (fi, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x8@-1", "0x8@-1");
  mpfi_set_str (fi, "0.1", 10);
  check_fi (fi, MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT, "0b11p-5", "0b10p-4");

  mpfr_set_default_prec(53);
  check_fi (fi,
            MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "0x19999999999999@-14", "0x1999999999999A@-14");

  mpfr_set_inf (&(fi->left), -1);
  mpfr_set_inf (&(fi->right), +1);
  check_fi (fi, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "-@inf@", "+@inf@");

  mpfr_set_ui (&(fi->left), 0, MPFI_RNDD);
  mpfr_neg (&(fi->right), &(fi->left), MPFI_RNDU);
  check_fi (fi, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "+0", "-0");

  mpfr_set_nan (&(fi->left));
  mpfr_set_nan (&(fi->right));
  check_fi (fi, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT, "@nan@", "@nan@");

  mpfi_clear (fi);

  return 0;
}