summaryrefslogtreecommitdiff
path: root/tests/tget_d.c
blob: d8ef44771ee021d07719c02edd581bf0660b4c2c (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
/* Test file for mpfr_get_d

Copyright 1999, 2000, 2001, 2002 Free Software Foundation.

This file is part of the MPFR Library.

The 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 2.1 of the License, or (at your
option) any later version.

The 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 MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#include "mpfr-test.h"

int
main (void)
{

#ifdef HAVE_FENV

   mpfr_t half, x, y;
   mp_rnd_t rnd_mode;

   mpfr_init2(half, 2);
   mpfr_set_ui(half, 1, GMP_RNDZ);
   mpfr_div_2ui(half, half, 1, GMP_RNDZ); /* has exponent 0 */

   mpfr_init2(x, 128);
   mpfr_init2(y, 128);

   for (rnd_mode = 0; rnd_mode <= 3; rnd_mode++)
     {
       int i, j, si, sj;
       double di, dj;

       mpfr_set_machine_rnd_mode (rnd_mode);
       for (i = 1, di = 0.25; i < 127; i++, di *= 0.5)
         for (si = 0; si <= 1; si++)
           {
             mpfr_div_2ui(x, half, i, GMP_RNDZ);
             (si ? mpfr_sub : mpfr_add)(x, half, x, GMP_RNDZ);
             for (j = i+1, dj = di * 0.5; j < 128 && j < i+53; j++, dj *= 0.5)
               for (sj = 0; sj <= 1; sj++)
                 {
                   double c, d, dd;
                   int exp;
                   char *f;

                   mpfr_div_2ui(y, half, j, GMP_RNDZ);
                   (sj ? mpfr_sub : mpfr_add)(y, x, y, GMP_RNDZ);
                   exp = (LONG_RAND() % 47) - 23;
                   mpfr_mul_2si(y, y, exp, GMP_RNDZ);
                   if (mpfr_inexflag_p())
                     {
                       fprintf(stderr, "Error in tget_d: inexact flag for "
                               "(i,si,j,sj,rnd,exp) = (%d,%d,%d,%d,%d,%d)\n",
                               i, si, j, sj, rnd_mode, exp);
                       exit(1);
                     }
                   dd = si != sj ? di - dj : di + dj;
                   d = si ? 0.5 - dd : 0.5 + dd;
                   if ((LONG_RAND() / 1024) & 1)
                     {
                       c = mpfr_get_d2(y, rnd_mode);
                       f = "mpfr_get_d2";
                     }
                   else
                     {
                       exp = (LONG_RAND() % 47) - 23;
                       c = mpfr_get_d3(y, exp, rnd_mode);
                       f = "mpfr_get_d3";
                       if (si) /* then real d < 0.5 */
                         d *= sj && i == 1 ? 4 : 2; /* normalize real d */
                     }
                   if (exp > 0)
                     d *= 1 << exp;
                   if (exp < 0)
                     d /= 1 << -exp;
                   if (c != d)
                     {
                       fprintf (stderr, "Error in tget_d (%s) for "
                                "(i,si,j,sj,rnd,exp) = (%d,%d,%d,%d,%d,%d)\n"
                                "got %.25Le instead of %.25Le\n"
                                "Difference: %.19e\n",
                                f, i, si, j, sj, rnd_mode, exp,
                                (long double) c, (long double) d, d - c);
                       exit (1);
                     }
                 }
           }
     }

   mpfr_clear(half);
   mpfr_clear(x);
   mpfr_clear(y);

#endif

   return 0;
}