summaryrefslogtreecommitdiff
path: root/mpfr/tests/tcmp2.c
blob: 568f5e860ea548f48658aa9f22dfa08ee5a582aa (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
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
/* Test file for mpfr_cmp2.

Copyright (C) 1999-2001 Free Software Foundation.

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 <math.h>
#include "gmp.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#include "mpfr-test.h"

void tcmp2 (double, double, int);
void special (void);
void worst_cases (void);
void set_bit (mpfr_t, unsigned int, int);

/* set bit n of x to b, where bit 0 is the most significant one */
void
set_bit (mpfr_t x, unsigned int n, int b)
{
  unsigned l;
  mp_size_t xn;

  xn = (MPFR_PREC(x) - 1) / mp_bits_per_limb;
  l = n / mp_bits_per_limb;
  n %= mp_bits_per_limb;
  n = mp_bits_per_limb - 1 - n;
  if (b)
    MPFR_MANT(x)[xn - l] |= (mp_limb_t) 1 << n;
  else
    MPFR_MANT(x)[xn - l] &= ~((mp_limb_t) 1 << n);
}

/* check that for x = 1.u 1 v 0^k low(x)
                  y = 1.u 0 v 1^k low(y)
   mpfr_cmp2 (x, y) returns 1 + |u| + |v| + k for low(x) >= low(y),
                        and 1 + |u| + |v| + k + 1 otherwise */
void
worst_cases ()
{
  mpfr_t x, y;
  unsigned int i, j, k, l, b, expected;

  mpfr_init2 (x, 200);
  mpfr_init2 (y, 200);

  mpfr_set_ui (y, 1, GMP_RNDN);
  for (i=1; i<MPFR_PREC(x); i++)
    {
      mpfr_set_ui (x, 1, GMP_RNDN);
      mpfr_div_2exp (y, y, 1, GMP_RNDN); /* y = 1/2^i */

      if ((l = mpfr_cmp2 (x, y)) != 1)
	{
	  fprintf (stderr, "Error in mpfr_cmp2:\nx=");
	  mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
	  fprintf (stderr, "\ny=");
	  mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
	  fprintf (stderr, "\ngot %u instead of %u\n", l, 1);
	  exit(1);
	}

      mpfr_add (x, x, y, GMP_RNDN); /* x = 1 + 1/2^i */
      if ((l = mpfr_cmp2 (x, y)) != 0)
	{
	  fprintf (stderr, "Error in mpfr_cmp2:\nx=");
	  mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
	  fprintf (stderr, "\ny=");
	  mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
	  fprintf (stderr, "\ngot %u instead of %u\n", l, 0);
	  exit(1);
	}
    }

  for (i=0; i<64; i++) /* |u| = i */
    {
      mpfr_random (x);
      mpfr_set (y, x, GMP_RNDN);
      set_bit (x, i + 1, 1);
      set_bit (y, i + 1, 0);
      for (j=0; j<64; j++) /* |v| = j */
	{
	  b = random () % 2;
	  set_bit (x, i + j + 2, b);
	  set_bit (y, i + j + 2, b);

	  for (k=0; k<64; k++)
	    {

	      if (k) set_bit (x, i + j + k + 1, 0);
	      if (k) set_bit (y, i + j + k + 1, 1);

	      set_bit (x, i + j + k + 2, 1);
	      set_bit (y, i + j + k + 2, 0);
	      l = mpfr_cmp2 (x, y);
	      expected = i + j + k + 1;
	      if (l != expected)
		{
		  fprintf (stderr, "Error in mpfr_cmp2:\nx=");
		  mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
		  fprintf (stderr, "\ny=");
		  mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
		  fprintf (stderr, "\ngot %u instead of %u\n", l, expected);
		  exit(1);
		}

	      set_bit (x, i + j + k + 2, 0);
	      set_bit (x, i + j + k + 3, 0);
	      set_bit (y, i + j + k + 3, 1);
	      l = mpfr_cmp2 (x, y);
	      expected = i + j + k + 2;
	      if (l != expected)
		{
		  fprintf (stderr, "Error in mpfr_cmp2:\nx=");
		  mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
		  fprintf (stderr, "\ny=");
		  mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
		  fprintf (stderr, "\ngot %u instead of %u\n", l, expected);
		  exit(1);
		}
	    }
	}
    }

  mpfr_clear (x);
  mpfr_clear (y);
}

void
tcmp2 (double x, double y, int i)
{
  mpfr_t xx, yy;
  int j;

  if (i==-1) {
    if (x==y) i=53;
    else i = (int) floor(log(x)/log(2.0)) - (int) floor(log(x-y)/log(2.0));
  }
  mpfr_init2(xx, 53); mpfr_init2(yy, 53);
  mpfr_set_d (xx, x, GMP_RNDN);
  mpfr_set_d (yy, y, GMP_RNDN);
  j = mpfr_cmp2 (xx, yy);
  if (j != i) {
    fprintf (stderr, "Error in mpfr_cmp2 for\nx=");
    mpfr_out_str (stderr, 2, 0, xx, GMP_RNDN);
    fprintf (stderr, "\ny=");
    mpfr_out_str (stderr, 2, 0, yy, GMP_RNDN);
    fprintf (stderr, "\ngot %u instead of %u\n", j, i);
    exit(1);
  }
  mpfr_clear(xx); mpfr_clear(yy);
}

void special ()
{
  mpfr_t x, y;
  int j;

  mpfr_init (x); mpfr_init (y);

  /* bug found by Nathalie Revol, 21 March 2001 */
  mpfr_set_prec (x, 65);
  mpfr_set_prec (y, 65);
  mpfr_set_str_raw (x, "0.10000000000000000000000000000000000001110010010110100110011110000E1");
  mpfr_set_str_raw (y, "0.11100100101101001100111011111111110001101001000011101001001010010E-35");
  if ((j = mpfr_cmp2 (x, y)) != 1) {
    printf ("Error in mpfr_cmp2:\n");
    printf ("x=");
    mpfr_print_raw (x);
    putchar ('\n');
    printf ("y=");
    mpfr_print_raw (y);
    putchar ('\n');
    printf ("got %d, expected 1\n", j);
    exit (1);
  }

  mpfr_set_prec(x, 127); mpfr_set_prec(y, 127);
  mpfr_set_str_raw(x, "0.1011010000110111111000000101011110110001000101101011011110010010011110010000101101000010011001100110010000000010110000101000101E6");
  mpfr_set_str_raw(y, "0.1011010000110111111000000101011011111100011101000011001111000010100010100110110100110010011001100110010000110010010110000010110E6");
  if ((j=mpfr_cmp2(x, y)) != 32) {
    printf("Error in mpfr_cmp2:\n");
    printf("x="); mpfr_print_raw(x); putchar('\n');
    printf("y="); mpfr_print_raw(y); putchar('\n');
    printf("got %d, expected 32\n", j);
    exit(1);
  }

  mpfr_set_prec (x, 128); mpfr_set_prec (y, 239);
  mpfr_set_str_raw (x, "0.10001000110110000111011000101011111100110010010011001101000011111010010110001000000010100110100111111011011010101100100000000000E167");
  mpfr_set_str_raw (y, "0.10001000110110000111011000101011111100110010010011001101000011111010010110001000000010100110100111111011011010101100011111111111111111111111111111111111111111111111011111100101011100011001101000100111000010000000000101100110000111111000101E167");
  if ((j=mpfr_cmp2(x, y)) != 164) {
    printf("Error in mpfr_cmp2:\n");
    printf("x="); mpfr_print_raw(x); putchar('\n');
    printf("y="); mpfr_print_raw(y); putchar('\n');
    printf("got %d, expected 164\n", j);
    exit(1);
  }

  /* bug found by Nathalie Revol, 29 March 2001 */
  mpfr_set_prec (x, 130); mpfr_set_prec (y, 130);
  mpfr_set_str_raw (x, "0.1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E2");
  mpfr_set_str_raw (y, "0.1011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100E2");
  if ((j=mpfr_cmp2(x, y)) != 127) {
    printf("Error in mpfr_cmp2:\n");
    printf("x="); mpfr_print_raw(x); putchar('\n');
    printf("y="); mpfr_print_raw(y); putchar('\n');
    printf("got %d, expected 127\n", j);
    exit(1);
  }

  /* bug found by Nathalie Revol, 2 Apr 2001 */
  mpfr_set_prec (x, 65); mpfr_set_prec (y, 65);
  mpfr_set_ui (x, 5, GMP_RNDN);
  mpfr_set_str_raw (y, "0.10011111111111111111111111111111111111111111111111111111111111101E3");
  if ((j=mpfr_cmp2(x, y)) != 63) {
    printf("Error in mpfr_cmp2:\n");
    printf("x="); mpfr_print_raw(x); putchar('\n');
    printf("y="); mpfr_print_raw(y); putchar('\n');
    printf("got %d, expected 63\n", j);
    exit(1);
  }

  /* bug found by Nathalie Revol, 2 Apr 2001 */
  mpfr_set_prec (x, 65); mpfr_set_prec (y, 65);
  mpfr_set_str_raw (x, "0.10011011111000101001110000000000000000000000000000000000000000000E-69");
  mpfr_set_str_raw (y, "0.10011011111000101001101111111111111111111111111111111111111111101E-69");
  if ((j=mpfr_cmp2(x, y)) != 63) {
    printf("Error in mpfr_cmp2:\n");
    printf("x="); mpfr_print_raw(x); putchar('\n');
    printf("y="); mpfr_print_raw(y); putchar('\n');
    printf("got %d, expected 63\n", j);
    exit(1);
  }

  mpfr_clear(x); mpfr_clear(y);
}

int
main (void)
{
  int i,j; double x=1.0, y, z;
#ifdef __mips
    /* to get denormalized numbers on IRIX64 */
    union fpc_csr exp;
    exp.fc_word = get_fpc_csr();
    exp.fc_struct.flush = 0;
    set_fpc_csr(exp.fc_word);
#endif

  worst_cases ();
  special ();
  tcmp2(5.43885304644369510000e+185, -1.87427265794105340000e-57, 1);
  tcmp2(1.06022698059744327881e+71, 1.05824655795525779205e+71, -1);
  tcmp2(1.0, 1.0, 53);
  for (i=0;i<54;i++) {
    tcmp2 (1.0, 1.0-x, i);
    x /= 2.0;
  }
  for (x=0.5, i=1; i<100; i++) {
    tcmp2 (1.0, x, 1);
    x /= 2.0;
  }
  for (j=0; j<100000; j++) {
    x = drand48();
    y = drand48();
    if (x<y) { z=x; x=y; y=z; }
    if (y != 0.0 && y != -0.0) tcmp2(x, y, -1);
  }

  return 0;
}