1
/* Test file for mpfr_add_ui
3
Copyright 2000, 2001, 2002 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., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
31
#include "mpfr-impl.h"
32
#include "mpfr-test.h"
34
void check3 _PROTO((double, unsigned long, unsigned int, double));
35
void special _PROTO((void));
37
#define check(x,y,r) check3(x,y,r,0.0)
39
/* checks that x+y gives the same results in double
40
and with mpfr with 53 bits of precision */
42
check3 (double x, unsigned long y, unsigned int rnd_mode, double z1)
44
double z2; mpfr_t xx,zz;
48
mpfr_set_d(xx, x, rnd_mode);
49
mpfr_add_ui(zz, xx, y, rnd_mode);
50
#ifdef MPFR_HAVE_FESETROUND
51
mpfr_set_machine_rnd_mode(rnd_mode);
53
if (z1==0.0) z1 = x+y;
54
z2 = mpfr_get_d1 (zz);
55
if (z1!=z2 && !(isnan(z1) && isnan(z2))) {
56
printf("expected sum is %1.20e, got %1.20e\n",z1,z2);
57
printf("mpfr_add_ui failed for x=%1.20e y=%lu with rnd_mode=%s\n",
58
x, y, mpfr_print_rnd_mode(rnd_mode));
61
mpfr_clear(xx); mpfr_clear(zz);
71
mpfr_set_str_raw (x, "0.110100000000000001110001110010111111000000000101100011100100011");
72
mpfr_add_ui (y, x, 1, GMP_RNDD);
78
main (int argc, char *argv[])
80
#ifdef MPFR_HAVE_FESETROUND
81
double x; unsigned long y, N; int i,rnd_mode,rnd;
85
SEED_RAND (time(NULL));
86
N = (argc<2) ? 1000000 : atoi(argv[1]);
87
rnd_mode = (argc<3) ? -1 : atoi(argv[2]);
88
for (i=0;i<1000000;i++) {
91
if (ABS(x)>2.2e-307 && x+y<1.7e+308 && x+y>-1.7e308) {
92
/* avoid denormalized numbers and overflows */
93
rnd = (rnd_mode==-1) ? LONG_RAND()%4 : rnd_mode;
99
check3 (-1.716113812768534e-140, 1271212614, GMP_RNDZ,
100
1.27121261399999976e9);
101
check3 (1.22191250737771397120e+20, 948002822, GMP_RNDN,
102
122191250738719408128.0);
103
check3 (-6.72658901114033715233e-165, 2000878121, GMP_RNDZ,
104
2.0008781209999997615e9);
105
check3 (-2.0769715792901673e-5, 880524, GMP_RNDN, 8.8052399997923023e5);
107
check3 (DBL_POS_INF, 2394875, GMP_RNDN, DBL_POS_INF);
108
check3 (DBL_NEG_INF, 2394875, GMP_RNDN, DBL_NEG_INF);
109
check3 (DBL_NAN, 2394875, GMP_RNDN, DBL_NAN);