1
/* Test file for mpfr_mul_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. */
25
#include "mpfr-test.h"
28
check_inexact (mp_prec_t p)
38
mpfr_init2 (z, p + mp_bits_per_limb);
41
if (mpfr_mul_ui (z, x, u, GMP_RNDN))
43
printf ("Error: result should be exact\n");
47
for (q = 2; q <= p; q++)
48
for (rnd = 0; rnd < GMP_RND_MAX; rnd++)
51
inexact = mpfr_mul_ui (y, x, u, (mp_rnd_t) rnd);
52
cmp = mpfr_cmp (y, z);
53
if (((inexact == 0) && (cmp != 0)) ||
54
((inexact < 0) && (cmp >= 0)) ||
55
((inexact > 0) && (cmp <= 0)))
57
printf ("Wrong inexact flag for p=%u, q=%u, rnd=%s\n",
58
(unsigned int) p, (unsigned int) q,
59
mpfr_print_rnd_mode ((mp_rnd_t) rnd));
65
mpfr_set_ui (x, 1, GMP_RNDN);
66
if (mpfr_mul_ui (x, x, 5, GMP_RNDZ) == 0)
68
printf ("mul_ui(1, 5) cannot be exact with prec=2\n");
77
#define TEST_FUNCTION mpfr_mul_ui
78
#define INTEGER_TYPE unsigned long
79
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1)
80
#include "tgeneric_ui.c"
83
main (int argc, char *argv[])
86
unsigned int xprec, yprec, i;
90
MPFR_TEST_USE_RANDS ();
100
/* checks that result is normalized */
101
mpfr_set_str (y, "6.93147180559945286227e-01", 10, GMP_RNDZ);
102
mpfr_mul_ui (x, y, 1, GMP_RNDZ);
103
if (MPFR_MANT(x)[MPFR_PREC(x)/mp_bits_per_limb] >> (mp_bits_per_limb-1) == 0)
105
printf ("Error in mpfr_mul_ui: result not normalized\n");
110
printf ("Error in mpfr_mul_ui: 1*y != y\n");
111
printf ("y= "); mpfr_print_binary (y); puts ("");
112
printf ("1*y="); mpfr_print_binary (x); puts ("");
117
mpfr_mul_ui (x, x, 3, GMP_RNDU);
118
if (!mpfr_inf_p (x) || (mpfr_sgn (x) <= 0))
120
printf ("Error in mpfr_mul_ui: +Inf*3 does not give +Inf\n");
124
mpfr_set_inf (x, -1);
125
mpfr_mul_ui (x, x, 3, GMP_RNDU);
126
if (!mpfr_inf_p (x) || (mpfr_sgn (x) >= 0))
128
printf ("Error in mpfr_mul_ui: -Inf*3 does not give -Inf\n");
133
mpfr_mul_ui (x, x, 3, GMP_RNDU);
136
printf ("Error in mpfr_mul_ui: NaN*3 does not give NaN\n");
141
mpfr_mul_ui (x, x, 0, GMP_RNDU);
142
MPFR_ASSERTN(mpfr_nan_p (x));
144
mpfr_set_ui (x, 1, GMP_RNDU);
145
mpfr_mul_ui (x, x, 0, GMP_RNDU);
146
MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
148
emax = mpfr_get_emax ();
150
mpfr_set_str_binary (x, "0.1E0");
151
mpfr_mul_ui (x, x, 2, GMP_RNDN);
152
MPFR_ASSERTN(mpfr_inf_p (x) && MPFR_IS_POS(x));
155
mpfr_set_str (x, /*1.0/3.0*/
156
"0.333333333333333333333333333333333", 10, GMP_RNDZ);
157
mpfr_mul_ui (x, x, 3, GMP_RNDU);
158
if (mpfr_cmp_ui (x, 1))
160
printf ("Error in mpfr_mul_ui: U(Z(1/3)*3) does not give 1\n");
164
/* checks sign is correct */
165
mpfr_set_si (x, -2, GMP_RNDZ);
166
mpfr_set_si (y, 3, GMP_RNDZ);
167
mpfr_mul_ui(x, y, 4, GMP_RNDZ);
168
if (mpfr_cmp_ui(x, 0) <= 0)
170
printf("Error in mpfr_mul_ui: 4*3.0 does not give a positive result:\n");
171
mpfr_print_binary(x); puts ("");
172
printf("mpfr_cmp_ui(x, 0) = %d\n", mpfr_cmp_ui(x, 0));
176
mpfr_set_prec (x, 9);
177
mpfr_set_prec (y, 9);
178
mpfr_set_str_binary (y, "0.100001111E9"); /* 271 */
179
mpfr_mul_ui (x, y, 1335, GMP_RNDN);
180
mpfr_set_str_binary (y, "0.101100001E19"); /* 361472 */
183
printf ("Error in mul_ui for 1335*(0.100001111E9)\n");
184
printf ("got "); mpfr_print_binary (x); puts ("");
188
mpfr_set_prec(y, 100);
189
mpfr_set_prec(x, 100);
190
/* y = 1199781142214086656 */
191
mpfr_set_str_binary(y, "0.1000010100110011110101001011110010101111000100001E61");
192
mpfr_mul_ui(x, y, 121, GMP_RNDD);
193
/* 121*y = 145173518207904485376, representable exactly */
194
mpfr_set_str_binary(y, "0.1111101111010101111111100011010010111010111110110011001E67");
197
printf("Error for 121*y: expected result is:\n");
198
mpfr_print_binary(y); puts ("");
201
mpfr_set_prec (x, 32);
202
mpfr_set_str_binary (x, "0.10000000000000000000000000000000E1");
203
mpfr_set_prec (y, 93);
204
mpfr_mul_ui (y, x, 1, GMP_RNDN);
206
mpfr_set_prec (x, 287);
207
mpfr_set_str_binary (x, "0.1111E7");
208
mpfr_set_prec (y, 289);
209
mpfr_mul_ui (y, x, 6, GMP_RNDN);
210
mpfr_set_str_binary (x, "0.101101E10");
213
printf ("Error for 6 * 120\n");
217
mpfr_set_prec (x, 68);
218
mpfr_set_prec (y, 64);
219
mpfr_set_str (x, "2143861251406875.0", 10, GMP_RNDN);
220
mpfr_mul_ui (y, x, 23, GMP_RNDN);
221
mpfr_set_str_binary (x, "10101111001011100001100110101111110001010010011001101101.0");
224
printf ("Error for 23 * 2143861251406875.0\n");
225
printf ("expected "); mpfr_print_binary (x); puts ("");
226
printf ("got "); mpfr_print_binary (y); puts ("");
231
for (xprec = 53; xprec <= 128; xprec++)
233
mpfr_set_prec (x, xprec);
234
mpfr_set_str_binary (x, "0.1100100100001111110011111000000011011100001100110111E2");
235
for (yprec = 53; yprec <= 128; yprec++)
237
mpfr_set_prec (y, yprec);
238
mpfr_mul_ui (y, x, 1, GMP_RNDN);
241
printf ("multiplication by 1.0 fails for xprec=%u, yprec=%u\n",
243
printf ("expected "); mpfr_print_binary (x); puts ("");
244
printf ("got "); mpfr_print_binary (y); puts ("");
250
mpfr_set_prec (x, 128);
251
mpfr_set_ui (x, 17, GMP_RNDN);
252
mpfr_mul_ui (x, x, ULONG_HIGHBIT, GMP_RNDN);
253
mpfr_set_prec (y, 128);
254
mpfr_set_ui (y, ULONG_HIGHBIT, GMP_RNDN);
255
mpfr_mul_ui (y, y, 17, GMP_RNDN);
258
printf ("Error for 17 * ULONG_HIGHBIT\n");
262
/* Check regression */
263
mpfr_set_prec (x, 32);
264
mpfr_set_prec (y, 96);
265
mpfr_set_ui (x, 1742175942, GMP_RNDN);
266
mpfr_mul_ui (y, x, 59, GMP_RNDN);
267
if (mpfr_cmp_str (y, "0.10111111011101010101000110111101000100000000000000"
268
"0000000000000000000000000000000000000000000000E37",
271
printf ("Regression tested failed for x=1742175942 * 59\n");
278
test_generic_ui (2, 500, 100);