summaryrefslogtreecommitdiff
path: root/mini-gmp/tests/t-mpq_double.c
blob: 3cfba60840355dbd68035c99e62a670ae40ba445 (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
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
/* Test mpq_set_d.

Copyright 2001-2003, 2005, 2013, 2018 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU 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 MP Library test suite 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 General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <math.h>
#include <float.h>
#include <limits.h>

#include "testutils.h"
#include "../mini-mpq.h"

#define COUNT 2000

mp_bitcnt_t
mpz_mantissasizeinbits (const mpz_t z)
{
  return ! mpz_cmp_ui (z, 0) ? 0 :
    mpz_sizeinbase (z, 2) - mpz_scan1 (z, 0);
}

int
mpz_abspow2_p (const mpz_t z)
{
  return mpz_mantissasizeinbits (z) == 1;
}

mp_bitcnt_t
mpq_mantissasizeinbits (const mpq_t q)
{
  if (! mpz_abspow2_p (mpq_denref (q)))
    return ~ (mp_bitcnt_t) 0;

  return mpz_mantissasizeinbits (mpq_numref (q));
}

#if defined(DBL_MANT_DIG) && FLT_RADIX == 2
int
mpz_get_d_exact_p (const mpz_t z)
{
  return mpz_mantissasizeinbits (z) <= DBL_MANT_DIG;
}

int
mpq_get_d_exact_p (const mpq_t q)
{
  return mpq_mantissasizeinbits (q) <= DBL_MANT_DIG;
}
#define HAVE_EXACT_P 1
#endif

void
check_random (void)
{
  unsigned i;
  mpz_t x;
  mpq_t y, z;

  mpz_init (x);
  mpq_init (y);
  mpq_init (z);

  for (i = 0; i < COUNT; i++)
    {
      /* Use volatile, to avoid extended precision in floating point
	 registers, e.g., on m68k and 80387. */
      volatile double d, f;
      unsigned long m;
      int e, c;

      mini_rrandomb (x, CHAR_BIT * sizeof (unsigned long));
      m = mpz_get_ui (x);
      mini_urandomb (x, 8);
      e = mpz_get_ui (x) - 128;

      d = ldexp ((double) m, e);
      mpq_set_d (y, d);
      f = mpq_get_d (y);
      if (f != d)
	{
	  fprintf (stderr, "mpq_set_d/mpq_get_d failed:\n");
	  goto dumperror;
	}

      d = - d;
      mpq_neg (y, y);

      mpq_set_d (z, d);
      f = mpq_get_d (z);
      if (f != d || !mpq_equal (y, z))
	{
	  fprintf (stderr, "mpq_set_d/mpq_get_d failed:\n");
	dumperror:
	  dump ("ny", mpq_numref (y));
	  dump ("dy", mpq_denref (y));
	  fprintf (stderr, "m = %lx, e = %i\n", m, e);
	  fprintf (stderr, "d = %.35g\n", d);
	  fprintf (stderr, "f = %.35g\n", f);
	  fprintf (stderr, "f - d = %.35g\n", f - d);
	  abort ();
	}

      mini_rrandomb (x, CHAR_BIT * sizeof (unsigned long));
      m = mpz_get_ui (x);
      mini_urandomb (x, 8);
      e = mpz_get_ui (x) - 128;

      d = ldexp ((double) m, e);
      mpq_set_d (y, d);

      if (i == 0)
	mpq_neg (z, y);

      mpq_add (y, y, z);
      mpq_set_d (z, mpq_get_d (y));
      f = mpq_get_d (z);
      c = mpq_cmp (y, z);

#if defined(HAVE_EXACT_P)
      if (mpq_get_d_exact_p (y) ? c != 0 : (f > 0 ? c <= 0 : c >= 0))
#else
      if (f > 0 ? c < 0 : c > 0)
#endif
	{
	  fprintf (stderr, "mpq_get_d/mpq_set_d failed: %i %i\n", i, c);
	  goto dumperror;
	}
    }

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


void
check_data (void)
{
  static const struct {
    double        y;
    long int      n;
    unsigned long d;
  } data[] = {
    {  0.0,  0, 1 },
    {  1.0,  1, 1 },
    { -1.0, -1, 1 },
    { -1.5, -3, 2 },
    {-1.25, -5, 4 },
    {0.125,  1, 8 },

    {24685,24685,1},
    {-9876,-9876,1},
    {463.5,  927,2},

    {1234.5/8192,  2469, 16384 },
    {-543.0/1024,  -543,  1024 },
    {9876.5/ 512, 19753,  1024 },
    {9753.0/ 128,  9753,   128 },
    {-789.0/  32,  -789,    32 },
    {4.580078125,  2345,   512 },
  };

  mpq_t    x, r;
  unsigned i;
  double d;

  mpq_init (x);
  mpq_init (r);

  for (i = 0; i < numberof (data); i++)
    {
      mpq_set_d (x, data[i].y);
      mpq_set_si (r, data[i].n, data[i].d);
      mpq_canonicalize (r);
      if (!mpq_equal (x, r))
	{
	  fprintf (stderr, "mpq_set_d failed: %li / %lu != %g\n", data[i].n, data[i].d, data[i].y);
	  abort ();
	}
      d = mpq_get_d (r);
      if (d != data[i].y)
	{
	  fprintf (stderr, "mpq_get_d failed: %li / %lu != %g\n", data[i].n, data[i].d, data[i].y);
	  abort ();
	}
    }

  mpq_clear (x);
  mpq_clear (r);
}

void
testmain (int argc, char *argv[])
{
  check_data ();
  check_random ();
}