1
/* Test file for mpfr_div_ui.
3
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
5
This file is part of the MPFR Library.
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.
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.
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. */
26
#include "mpfr-test.h"
29
check (const char *ds, unsigned long u, mp_rnd_t rnd, const char *es)
35
mpfr_set_str1 (x, ds);
36
mpfr_div_ui (y, x, u, rnd);
37
if (mpfr_cmp_str1 (y, es))
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);
53
unsigned xprec, yprec;
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);
63
mpfr_set_prec (x, 100);
64
mpfr_set_prec (y, 100);
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))
71
printf ("mpfr_div_ui gives non-zero for 0/ui\n");
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");
83
printf ("Error in x/17 for x=1/16!\n");
85
mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
87
mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
93
mpfr_set_prec (x, 2 * mp_bits_per_limb);
95
mpfr_set_ui (x, 4, GMP_RNDN);
97
mpfr_div_ui (y, x, 2, GMP_RNDN); /* exactly in the middle */
98
MPFR_ASSERTN(mpfr_cmp_ui (y, 2) == 0);
100
mpfr_set_prec (x, 3 * mp_bits_per_limb);
101
mpfr_set_prec (y, 2);
102
mpfr_set_ui (x, 2, GMP_RNDN);
104
mpfr_div_ui (y, x, 2, GMP_RNDN);
105
MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0);
107
mpfr_set_prec (x, 3 * mp_bits_per_limb);
108
mpfr_set_prec (y, 2);
109
mpfr_set_si (x, -4, GMP_RNDN);
111
mpfr_div_ui (y, x, 2, GMP_RNDD);
112
MPFR_ASSERTN(mpfr_cmp_si (y, -3) == 0);
114
for (xprec = 53; xprec <= 128; xprec++)
116
mpfr_set_prec (x, xprec);
117
mpfr_set_str_binary (x, "0.1100100100001111110011111000000011011100001100110111E2");
118
for (yprec = 53; yprec <= 128; yprec++)
120
mpfr_set_prec (y, yprec);
121
mpfr_div_ui (y, x, 1, GMP_RNDN);
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 ("");
149
for (px=2; px<300; px++)
151
mpfr_set_prec (x, px);
158
for (py=2; py<300; py++)
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++)
164
inexact = mpfr_div_ui (y, x, u, (mp_rnd_t) rnd);
165
if (mpfr_mul_ui (z, y, u, (mp_rnd_t) rnd))
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 ("");
172
cmp = mpfr_cmp (z, x);
173
if (((inexact == 0) && (cmp != 0)) ||
174
((inexact > 0) && (cmp <= 0)) ||
175
((inexact < 0) && (cmp >= 0)))
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 ("");
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"
198
main (int argc, char **argv)
202
MPFR_TEST_USE_RANDS ();
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");
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"))
222
printf ("Error in mpfr_div_ui for x=ln(3), u=5\n");
227
test_generic_ui (2, 200, 100);