summaryrefslogtreecommitdiff
path: root/tests/tset_si.c
blob: e54c6e66845792eb697c257d44435caff3961892 (plain)
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
/* Test file for mpfr_set_si and mpfr_set_ui.

Copyright 1999, 2001, 2002, 2003 Free Software Foundation, Inc.

This file is part of the MPFR Library.

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

The 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 MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#include "mpfr-test.h"


/* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
   have all tests examine the bits in mpfr_t for what should come out.  */


int
main (int argc, char *argv[])
{
  mpfr_t x;
  long k, z, d;
  unsigned long zl, dl, N;
  int inex;
  
  tests_start_mpfr ();

  mpfr_init2 (x, 100);

  N = (argc==1) ? 500000 : atoi (argv[1]);

  for (k = 1; k <= N; k++)
    {
      z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
      inex = mpfr_set_si (x, z, GMP_RNDZ);
      d = mpfr_get_si (x, GMP_RNDZ);
      if (d != z)
        {
          fprintf (stderr, "Error in mpfr_set_si: expected %ld got %ld\n",
                   z, d);
          exit (1);
        }
      if (inex)
        {
          fprintf (stderr, 
                   "Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
                   z, inex);
          exit (1);
        }
    }

  for (k = 1; k <= N; k++)
    {
      zl = randlimb ();
      inex = mpfr_set_ui (x, zl, GMP_RNDZ);
      dl = mpfr_get_ui (x, GMP_RNDZ);
      if (dl != zl)
        {
          fprintf (stderr, "Error in mpfr_set_ui: expected %lu got %lu\n",
                   zl, dl);
          exit (1);
        }
      if (inex)
        {
          fprintf (stderr,
                   "Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
                   zl, inex);
          exit (1);
        }
    }

  mpfr_set_prec (x, 2);
  if (mpfr_set_si (x, 5, GMP_RNDZ) >= 0)
    {
      fprintf (stderr, "Wrong inexact flag for x=5, rnd=GMP_RNDZ\n");
      exit (1);
    }

  mpfr_set_prec (x, 2);
  if (mpfr_set_si (x, -5, GMP_RNDZ) <= 0)
    {
      fprintf (stderr, "Wrong inexact flag for x=-5, rnd=GMP_RNDZ\n");
      exit (1);
    }

  mpfr_set_prec (x, 3);
  inex = mpfr_set_si (x, 77617, GMP_RNDD); /* should be 65536 */
  if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
      || inex >= 0)
    {
      fprintf (stderr, "Error in mpfr_set_si(x:3, 77617, GMP_RNDD)\n");
      mpfr_print_binary (x);
      puts ("");
      exit (1);
    }
  inex = mpfr_set_ui (x, 77617, GMP_RNDD); /* should be 65536 */
  if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
      || inex >= 0)
    {
      fprintf (stderr, "Error in mpfr_set_ui(x:3, 77617, GMP_RNDD)\n");
      mpfr_print_binary (x);
      puts ("");
      exit (1);
    }

  mpfr_set_prec (x, 2);
  inex = mpfr_set_si (x, 33096, GMP_RNDU);
  if (mpfr_get_si (x, GMP_RNDZ) != 49152 || inex <= 0)
    {
    fprintf (stderr, "Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
             mpfr_get_si (x, GMP_RNDZ), inex);
    exit (1);
    }
  inex = mpfr_set_ui (x, 33096, GMP_RNDU);
  if (mpfr_get_si (x, GMP_RNDZ) != 49152)
    {
      fprintf (stderr, "Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
               mpfr_get_si (x, GMP_RNDZ), inex);
      exit (1);
    }

  mpfr_set_si (x, -1, GMP_RNDN);
  mpfr_set_ui (x, 0, GMP_RNDN);
  if (MPFR_SIGN (x) < 0)
    {
      fprintf (stderr, "mpfr_set_ui (x, 0) gives -0\n");
      exit (1);
    }

  mpfr_set_si (x, -1, GMP_RNDN);
  mpfr_set_si (x, 0, GMP_RNDN);
  if (MPFR_SIGN (x) < 0)
    {
      fprintf (stderr, "mpfr_set_si (x, 0) gives -0\n");
      exit (1);
    }

  /* check potential bug in case mp_limb_t is unsigned */
  mpfr_set_emax (0);
  mpfr_set_si (x, -1, GMP_RNDN);
  if (mpfr_sgn (x) >= 0)
    {
      fprintf (stderr, "mpfr_set_si (x, -1) fails\n");
      exit (1);
    }

  mpfr_set_emax (5);
  mpfr_set_prec (x, 2);
  mpfr_set_si (x, -31, GMP_RNDN);
  if (mpfr_sgn (x) >= 0)
    {
      fprintf (stderr, "mpfr_set_si (x, -31) fails\n");
      exit (1);
    }

  mpfr_clear (x);

  tests_end_mpfr ();
  return 0;
}