~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tdiv_ui.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for mpfr_div_ui.
 
2
 
 
3
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR Library is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The MPFR Library is distributed in the hope that it will be useful, but
 
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <float.h>
 
25
 
 
26
#include "mpfr-test.h"
 
27
 
 
28
static void
 
29
check (const char *ds, unsigned long u, mp_rnd_t rnd, const char *es)
 
30
{
 
31
  mpfr_t x, y;
 
32
 
 
33
  mpfr_init2 (x, 53);
 
34
  mpfr_init2 (y, 53);
 
35
  mpfr_set_str1 (x, ds);
 
36
  mpfr_div_ui (y, x, u, rnd);
 
37
  if (mpfr_cmp_str1 (y, es))
 
38
    {
 
39
      printf ("mpfr_div_ui failed for x=%s, u=%lu, rnd=%s\n", ds, u,
 
40
              mpfr_print_rnd_mode (rnd));
 
41
      printf ("expected result is %s, got", es);
 
42
      mpfr_out_str(stdout, 10, 0, y, GMP_RNDN);
 
43
      exit (1);
 
44
    }
 
45
  mpfr_clear (x);
 
46
  mpfr_clear (y);
 
47
}
 
48
 
 
49
static void
 
50
special (void)
 
51
{
 
52
  mpfr_t x, y;
 
53
  unsigned xprec, yprec;
 
54
 
 
55
  mpfr_init (x);
 
56
  mpfr_init (y);
 
57
 
 
58
  mpfr_set_prec (x, 32);
 
59
  mpfr_set_prec (y, 32);
 
60
  mpfr_set_ui (x, 1, GMP_RNDN);
 
61
  mpfr_div_ui (y, x, 3, GMP_RNDN);
 
62
 
 
63
  mpfr_set_prec (x, 100);
 
64
  mpfr_set_prec (y, 100);
 
65
  mpfr_random (x);
 
66
  mpfr_div_ui (y, x, 123456, GMP_RNDN);
 
67
  mpfr_set_ui (x, 0, GMP_RNDN);
 
68
  mpfr_div_ui (y, x, 123456789, GMP_RNDN);
 
69
  if (mpfr_cmp_ui (y, 0))
 
70
    {
 
71
      printf ("mpfr_div_ui gives non-zero for 0/ui\n");
 
72
      exit (1);
 
73
    }
 
74
 
 
75
  /* bug found by Norbert Mueller, 21 Aug 2001 */
 
76
  mpfr_set_prec (x, 110);
 
77
  mpfr_set_prec (y, 60);
 
78
  mpfr_set_str_binary (x, "0.110101110011111110011111001110011001110111000000111110001000111011000011E-44");
 
79
  mpfr_div_ui (y, x, 17, GMP_RNDN);
 
80
  mpfr_set_str_binary (x, "0.11001010100101100011101110000001100001010110101001010011011E-48");
 
81
  if (mpfr_cmp (x, y))
 
82
    {
 
83
      printf ("Error in x/17 for x=1/16!\n");
 
84
      printf ("Expected ");
 
85
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
86
      printf ("\nGot      ");
 
87
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
88
      printf ("\n");
 
89
      exit (1);
 
90
    }
 
91
 
 
92
  /* corner case */
 
93
  mpfr_set_prec (x, 2 * mp_bits_per_limb);
 
94
  mpfr_set_prec (y, 2);
 
95
  mpfr_set_ui (x, 4, GMP_RNDN);
 
96
  mpfr_nextabove (x);
 
97
  mpfr_div_ui (y, x, 2, GMP_RNDN); /* exactly in the middle */
 
98
  MPFR_ASSERTN(mpfr_cmp_ui (y, 2) == 0);
 
99
 
 
100
  mpfr_set_prec (x, 3 * mp_bits_per_limb);
 
101
  mpfr_set_prec (y, 2);
 
102
  mpfr_set_ui (x, 2, GMP_RNDN);
 
103
  mpfr_nextabove (x);
 
104
  mpfr_div_ui (y, x, 2, GMP_RNDN);
 
105
  MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0);
 
106
 
 
107
  mpfr_set_prec (x, 3 * mp_bits_per_limb);
 
108
  mpfr_set_prec (y, 2);
 
109
  mpfr_set_si (x, -4, GMP_RNDN);
 
110
  mpfr_nextbelow (x);
 
111
  mpfr_div_ui (y, x, 2, GMP_RNDD);
 
112
  MPFR_ASSERTN(mpfr_cmp_si (y, -3) == 0);
 
113
 
 
114
  for (xprec = 53; xprec <= 128; xprec++)
 
115
    {
 
116
      mpfr_set_prec (x, xprec);
 
117
      mpfr_set_str_binary (x, "0.1100100100001111110011111000000011011100001100110111E2");
 
118
      for (yprec = 53; yprec <= 128; yprec++)
 
119
        {
 
120
          mpfr_set_prec (y, yprec);
 
121
          mpfr_div_ui (y, x, 1, GMP_RNDN);
 
122
          if (mpfr_cmp(x,y))
 
123
            {
 
124
              printf ("division by 1.0 fails for xprec=%u, yprec=%u\n", xprec, yprec);
 
125
              printf ("expected "); mpfr_print_binary (x); puts ("");
 
126
              printf ("got      "); mpfr_print_binary (y); puts ("");
 
127
              exit (1);
 
128
            }
 
129
        }
 
130
    }
 
131
 
 
132
  mpfr_clear (x);
 
133
  mpfr_clear (y);
 
134
}
 
135
 
 
136
static void
 
137
check_inexact (void)
 
138
{
 
139
  mpfr_t x, y, z;
 
140
  mp_prec_t px, py;
 
141
  int inexact, cmp;
 
142
  unsigned long int u;
 
143
  int rnd;
 
144
 
 
145
  mpfr_init (x);
 
146
  mpfr_init (y);
 
147
  mpfr_init (z);
 
148
 
 
149
  for (px=2; px<300; px++)
 
150
    {
 
151
      mpfr_set_prec (x, px);
 
152
      mpfr_random (x);
 
153
      do
 
154
        {
 
155
          u = randlimb ();
 
156
        }
 
157
      while (u == 0);
 
158
      for (py=2; py<300; py++)
 
159
        {
 
160
          mpfr_set_prec (y, py);
 
161
          mpfr_set_prec (z, py + mp_bits_per_limb);
 
162
          for (rnd = 0; rnd < GMP_RND_MAX; rnd++)
 
163
            {
 
164
              inexact = mpfr_div_ui (y, x, u, (mp_rnd_t) rnd);
 
165
              if (mpfr_mul_ui (z, y, u, (mp_rnd_t) rnd))
 
166
                {
 
167
                  printf ("z <- y * u should be exact for u=%lu\n", u);
 
168
                  printf ("y="); mpfr_print_binary (y); puts ("");
 
169
                  printf ("z="); mpfr_print_binary (z); puts ("");
 
170
                  exit (1);
 
171
                }
 
172
              cmp = mpfr_cmp (z, x);
 
173
              if (((inexact == 0) && (cmp != 0)) ||
 
174
                  ((inexact > 0) && (cmp <= 0)) ||
 
175
                  ((inexact < 0) && (cmp >= 0)))
 
176
                {
 
177
                  printf ("Wrong inexact flag for u=%lu, rnd=%s\n", u,
 
178
                          mpfr_print_rnd_mode ((mp_rnd_t) rnd));
 
179
                  printf ("x="); mpfr_print_binary (x); puts ("");
 
180
                  printf ("y="); mpfr_print_binary (y); puts ("");
 
181
                  exit (1);
 
182
                }
 
183
            }
 
184
        }
 
185
    }
 
186
 
 
187
  mpfr_clear (x);
 
188
  mpfr_clear (y);
 
189
  mpfr_clear (z);
 
190
}
 
191
 
 
192
#define TEST_FUNCTION mpfr_div_ui
 
193
#define INTEGER_TYPE  unsigned long
 
194
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1)
 
195
#include "tgeneric_ui.c"
 
196
 
 
197
int
 
198
main (int argc, char **argv)
 
199
{
 
200
  mpfr_t x;
 
201
 
 
202
  MPFR_TEST_USE_RANDS ();
 
203
  tests_start_mpfr ();
 
204
 
 
205
  special ();
 
206
 
 
207
  check_inexact ();
 
208
 
 
209
  check("1.0", 3, GMP_RNDN, "3.3333333333333331483e-1");
 
210
  check("1.0", 3, GMP_RNDZ, "3.3333333333333331483e-1");
 
211
  check("1.0", 3, GMP_RNDU, "3.3333333333333337034e-1");
 
212
  check("1.0", 3, GMP_RNDD, "3.3333333333333331483e-1");
 
213
  check("1.0", 2116118, GMP_RNDN, "4.7256343927890600483e-7");
 
214
  check("1.098612288668109782", 5, GMP_RNDN, "0.21972245773362195087");
 
215
 
 
216
  mpfr_init2 (x, 53);
 
217
  mpfr_set_ui (x, 3, GMP_RNDD);
 
218
  mpfr_log (x, x, GMP_RNDD);
 
219
  mpfr_div_ui (x, x, 5, GMP_RNDD);
 
220
  if (mpfr_cmp_str1 (x, "0.21972245773362189536"))
 
221
    {
 
222
      printf ("Error in mpfr_div_ui for x=ln(3), u=5\n");
 
223
      exit (1);
 
224
    }
 
225
  mpfr_clear (x);
 
226
 
 
227
  test_generic_ui (2, 200, 100);
 
228
 
 
229
  tests_end_mpfr ();
 
230
  return 0;
 
231
}