summaryrefslogtreecommitdiff
path: root/tests/tcmpabs.c
blob: da4fc73b48d9fcad014c51a974491fccbeec7cc2 (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
291
292
293
294
295
/* Test file for mpfr_cmpabs and mpfr_cmpabs_ui.

Copyright 2004-2021 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(s) do { printf ("Error: %s\n", s); exit (1); } while (0)

static int
cmpabs (mpfr_srcptr x, mpfr_srcptr y)
{
  unsigned int i;
  int r[4];
  mpfr_flags_t f1, f2, flags[2] = { 0, MPFR_FLAGS_ALL };
  mpfr_exp_t emin, emax;

  emin = mpfr_get_emin ();
  emax = mpfr_get_emax ();

  for (i = 0; i < 4; i++)
    {
      if (i & 2)
        {
          mpfr_exp_t ex = MPFR_IS_SINGULAR (x) ? emax : MPFR_GET_EXP (x);
          mpfr_exp_t ey = MPFR_IS_SINGULAR (y) ? emax : MPFR_GET_EXP (y);
          set_emin (ex < ey ? ex : ey);
          set_emax (ex < ey ? ey : ex);
        }

      __gmpfr_flags = f1 = flags[i % 2];
      r[i] = mpfr_cmpabs (x, y);
      f2 = __gmpfr_flags;
      if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y))
        f1 |= MPFR_FLAGS_ERANGE;

      if (i & 2)
        {
          set_emin (emin);
          set_emax (emax);
        }

      if (f1 != f2)
        {
          printf ("Flags error in mpfr_cmpabs for i = %u\n  x = ", i);
          mpfr_dump (x);
          printf ("  y = ");
          mpfr_dump (y);
          printf ("Expected flags = ");
          flags_out (f1);
          printf ("Obtained flags = ");
          flags_out (f2);
          exit (1);
        }

      if (i > 0)
        MPFR_ASSERTN (r[i] == r[0]);
    }

  return r[0];
}

static void
test_cmpabs (void)
{
  mpfr_t xx, yy;

  mpfr_init2 (xx, 2);
  mpfr_init2 (yy, 2);

  mpfr_clear_erangeflag ();
  MPFR_SET_NAN (xx);
  MPFR_SET_NAN (yy);
  if (cmpabs (xx, yy) != 0)
    PRINT_ERROR ("mpfr_cmpabs (NAN,NAN) returns non-zero");
  if (!mpfr_erangeflag_p ())
    PRINT_ERROR ("mpfr_cmpabs (NAN,NAN) doesn't set erange flag");

  mpfr_set_si (yy, -1, MPFR_RNDN);
  if (cmpabs (xx, yy) != 0)
    PRINT_ERROR ("mpfr_cmpabs (NAN,-1) returns non-zero");
  if (cmpabs (yy, xx) != 0)
    PRINT_ERROR ("mpfr_cmpabs (-1,NAN) returns non-zero");

  mpfr_set_str_binary (xx, "0.10E0");
  mpfr_set_str_binary (yy, "-0.10E0");
  if (cmpabs (xx, yy) != 0)
    PRINT_ERROR ("mpfr_cmpabs (xx, yy) returns non-zero for prec=2");

  mpfr_set_prec (xx, 65);
  mpfr_set_prec (yy, 65);
  mpfr_set_str_binary (xx, "-0.10011010101000110101010000000011001001001110001011101011111011101E623");
  mpfr_set_str_binary (yy, "0.10011010101000110101010000000011001001001110001011101011111011100E623");
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error (1) in mpfr_cmpabs");

  mpfr_set_str_binary (xx, "-0.10100010001110110111000010001000010011111101000100011101000011100");
  mpfr_set_str_binary (yy, "-0.10100010001110110111000010001000010011111101000100011101000011011");
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error (2) in mpfr_cmpabs");

  mpfr_set_prec (xx, 160);
  mpfr_set_prec (yy, 160);
  mpfr_set_str_binary (xx, "0.1E1");
  mpfr_set_str_binary (yy, "-0.1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000110001110100");
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error (3) in mpfr_cmpabs");

  mpfr_set_prec(xx, 53);
  mpfr_set_prec(yy, 200);
  mpfr_set_ui (xx, 1, (mpfr_rnd_t) 0);
  mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
  if (cmpabs (xx, yy) != 0)
    PRINT_ERROR ("Error in mpfr_cmpabs: 1.0 != 1.0");

  mpfr_set_prec (yy, 31);
  mpfr_set_str (xx, "-1.0000000002", 10, (mpfr_rnd_t) 0);
  mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs: not 1.0000000002 > 1.0");
  mpfr_set_prec(yy, 53);

  mpfr_set_ui(xx, 0, MPFR_RNDN);
  mpfr_set_str (yy, "-0.1", 10, MPFR_RNDN);
  if (cmpabs (xx, yy) >= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(0.0, 0.1)");

  mpfr_set_inf (xx, -1);
  mpfr_set_str (yy, "23489745.0329", 10, MPFR_RNDN);
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(-Inf, 23489745.0329)");

  mpfr_set_inf (xx, 1);
  mpfr_set_inf (yy, -1);
  if (cmpabs (xx, yy) != 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(Inf, -Inf)");

  mpfr_set_inf (yy, -1);
  mpfr_set_str (xx, "2346.09234", 10, MPFR_RNDN);
  if (cmpabs (xx, yy) >= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(-Inf, 2346.09234)");

  mpfr_set_prec (xx, 2);
  mpfr_set_prec (yy, 128);
  mpfr_set_str_binary (xx, "0.1E10");
  mpfr_set_str_binary (yy,
                       "0.100000000000000000000000000000000000000000000000"
                       "00000000000000000000000000000000000000000000001E10");
  if (cmpabs (xx, yy) >= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(10.235, 2346.09234)");
  mpfr_swap (xx, yy);
  if (cmpabs (xx, yy) <= 0)
    PRINT_ERROR ("Error in mpfr_cmpabs(2346.09234, 10.235)");
  mpfr_swap (xx, yy);

  mpfr_clear (xx);
  mpfr_clear (yy);
}

static int
cmpabs_ui (mpfr_srcptr x, unsigned long u)
{
  unsigned int i;
  int r[4];
  mpfr_flags_t f1, f2, flags[2] = { 0, MPFR_FLAGS_ALL };
  mpfr_exp_t emin, emax;

  emin = mpfr_get_emin ();
  emax = mpfr_get_emax ();

  for (i = 0; i < 4; i++)
    {
      if (i & 2)
        {
          mpfr_exp_t e = MPFR_IS_SINGULAR (x) ? emax : MPFR_GET_EXP (x);
          set_emin (e);
          set_emax (e);
        }

      __gmpfr_flags = f1 = flags[i % 2];
      r[i] = mpfr_cmpabs_ui (x, u);
      f2 = __gmpfr_flags;
      if (MPFR_IS_NAN (x))
        f1 |= MPFR_FLAGS_ERANGE;

      if (i & 2)
        {
          set_emin (emin);
          set_emax (emax);
        }

      if (f1 != f2)
        {
          printf ("Flags error in mpfr_cmpabs_ui for i = %u\n  x = ", i);
          mpfr_dump (x);
          printf ("  u = %lu\n", u);
          printf ("Expected flags = ");
          flags_out (f1);
          printf ("Obtained flags = ");
          flags_out (f2);
          exit (1);
        }

      if (i > 0)
        MPFR_ASSERTN (r[i] == r[0]);
    }

  return r[0];
}

static void
test_cmpabs_ui (void)
{
  mpfr_t x;

  mpfr_init2 (x, 64); /* should be enough so that ULONG_MAX fits */
  /* check NaN */
  mpfr_set_nan (x);
  if (cmpabs_ui (x, 17) != 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (NaN, 17)");
  /* check -Inf */
  mpfr_set_inf (x, -1);
  if (cmpabs_ui (x, 17) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (-Inf, 17)");
  /* check +Inf */
  mpfr_set_inf (x, +1);
  if (cmpabs_ui (x, 17) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (+Inf, 17)");
  /* check -1 */
  mpfr_set_si (x, -1, MPFR_RNDN);
  if (cmpabs_ui (x, 0) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (-1, 0)");
  if (cmpabs_ui (x, 17) >= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (-1, 17)");
  /* check -0 */
  mpfr_set_zero (x, -1);
  if (cmpabs_ui (x, 0) != 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (-0, 0)");
  if (cmpabs_ui (x, 17) >= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (-0, 17)");
  /* check +0 */
  mpfr_set_zero (x, +1);
  if (cmpabs_ui (x, 0) != 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (+0, 0)");
  if (cmpabs_ui (x, 17) >= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (+0, 17)");
  /* check 1 */
  mpfr_set_ui (x, 1, MPFR_RNDN);
  if (cmpabs_ui (x, 0) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (1, 0)");
  if (cmpabs_ui (x, 1) != 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (1, 1)");
  if (cmpabs_ui (x, 17) >= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (1, 17)");
  /* check ULONG_MAX */
  mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
  if (cmpabs_ui (x, 0) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (ULONG_MAX, 0)");
  if (cmpabs_ui (x, 1) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (ULONG_MAX, 1)");
  if (cmpabs_ui (x, 17) <= 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (ULONG_MAX, 17)");
  if (cmpabs_ui (x, ULONG_MAX) != 0)
    PRINT_ERROR ("mpfr_cmpabs_ui (ULONG_MAX, ULONG_MAX)");
  mpfr_clear (x);
}

int
main (void)
{
  tests_start_mpfr ();

  test_cmpabs ();
  test_cmpabs_ui ();

  tests_end_mpfr ();
  return 0;
}