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

« back to all changes in this revision

Viewing changes to mpfr/tests/tconst_log2.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_const_log2.
 
2
 
 
3
Copyright 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
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
 
 
25
#include "mpfr-test.h"
 
26
 
 
27
/* tlog2 [prec] [rnd] [0 = no print] */
 
28
 
 
29
static void
 
30
check (mp_prec_t p0, mp_prec_t p1)
 
31
{
 
32
  mpfr_t x, y, z;
 
33
  mp_rnd_t rnd;
 
34
  int dif;
 
35
 
 
36
  mpfr_init (x);
 
37
  mpfr_init (y);
 
38
  mpfr_init2 (z, p1 + 10);
 
39
  mpfr_const_log2 (z, GMP_RNDN);
 
40
  mpfr_clear_cache (__gmpfr_cache_const_log2);
 
41
 
 
42
  for (; p0<=p1; p0++)
 
43
    {
 
44
      mpfr_set_prec (x, p0);
 
45
      mpfr_set_prec (y, p0);
 
46
        {
 
47
          rnd = (mp_rnd_t) RND_RAND ();
 
48
          mpfr_const_log2 (x, rnd);
 
49
          mpfr_set (y, z, rnd);
 
50
          if ((dif = mpfr_cmp (x, y))
 
51
              && mpfr_can_round (z, mpfr_get_prec(z), GMP_RNDN,
 
52
                                                 rnd, p0))
 
53
            {
 
54
              printf ("mpfr_const_log2 fails for prec=%u, rnd=%s Diff=%d\n",
 
55
                      (unsigned int) p0, mpfr_print_rnd_mode (rnd), dif);
 
56
              printf ("expected "), mpfr_dump (y);
 
57
              printf ("got      "), mpfr_dump (x);
 
58
              exit (1);
 
59
            }
 
60
        }
 
61
    }
 
62
 
 
63
  mpfr_clear (x);
 
64
  mpfr_clear (y);
 
65
  mpfr_clear (z);
 
66
}
 
67
 
 
68
static void
 
69
check_large (void)
 
70
{
 
71
  mpfr_t x, y;
 
72
  mpfr_init2 (x, 25000);
 
73
  mpfr_init2 (y, 26000);
 
74
  (mpfr_const_log2) (x, GMP_RNDN); /* First one ! */
 
75
  (mpfr_const_log2) (y, GMP_RNDN); /* Then the other - cache - */
 
76
  mpfr_prec_round (y, 25000, GMP_RNDN);
 
77
  if (mpfr_cmp (x, y))
 
78
    {
 
79
      printf ("const_log2: error for large prec\n");
 
80
      exit (1);
 
81
    }
 
82
 
 
83
  /* worst-case with 15 successive ones after last bit,
 
84
     to exercise can_round loop */
 
85
  mpfr_set_prec (x, 26249);
 
86
  mpfr_const_log2 (x, GMP_RNDZ);
 
87
 
 
88
  mpfr_clears (x, y, NULL);
 
89
}
 
90
 
 
91
static void
 
92
check_cache ()
 
93
{
 
94
  mpfr_t x;
 
95
  int i;
 
96
 
 
97
  mpfr_init2 (x, 195);
 
98
  mpfr_free_cache ();
 
99
  i = mpfr_const_log2 (x, GMP_RNDN);
 
100
  if (i == 0)
 
101
    {
 
102
      printf("Error for log2. Invalid ternary value (1).\n");
 
103
      exit (1);
 
104
    }
 
105
  mpfr_set_prec (x, 194);
 
106
  i = mpfr_const_log2 (x, GMP_RNDN);
 
107
  if (i == 0)
 
108
    {
 
109
      printf("Error for log2. Invalid ternary value (2).\n");
 
110
      exit (1);
 
111
    }
 
112
 
 
113
  mpfr_free_cache ();
 
114
  mpfr_set_prec (x, 9);
 
115
  mpfr_const_log2 (x, GMP_RNDN);
 
116
  mpfr_set_prec (x, 8);
 
117
  mpfr_const_log2 (x, GMP_RNDN);
 
118
  if (mpfr_cmp_str (x, "0.10110001E0", 2, GMP_RNDN))
 
119
    {
 
120
      printf("Error for log2. Wrong rounding.\n");
 
121
      exit (1);
 
122
    }
 
123
 
 
124
  mpfr_clear (x);
 
125
}
 
126
 
 
127
int
 
128
main (int argc, char *argv[])
 
129
{
 
130
  mpfr_t x;
 
131
  int p;
 
132
  mp_rnd_t rnd;
 
133
 
 
134
  tests_start_mpfr ();
 
135
 
 
136
  p = (argc>1) ? atoi(argv[1]) : 53;
 
137
  rnd = (argc>2) ? (mp_rnd_t) atoi(argv[2]) : GMP_RNDZ;
 
138
 
 
139
  mpfr_init (x);
 
140
 
 
141
  check (2, 1000);
 
142
 
 
143
  /* check precision of 2 bits */
 
144
  mpfr_set_prec (x, 2);
 
145
  mpfr_const_log2 (x, GMP_RNDN);
 
146
  if (mpfr_cmp_ui_2exp(x, 3, -2)) /* 3*2^-2 */
 
147
    {
 
148
      printf ("mpfr_const_log2 failed for prec=2, rnd=GMP_RNDN\n"
 
149
              "expected 0.75, got ");
 
150
      mpfr_out_str(stdout, 10, 0, x, GMP_RNDN);
 
151
      putchar('\n');
 
152
      exit (1);
 
153
    }
 
154
 
 
155
  if (argc>=2)
 
156
    {
 
157
      mpfr_set_prec (x, p);
 
158
      mpfr_const_log2 (x, rnd);
 
159
      printf ("log(2)=");
 
160
      mpfr_out_str (stdout, 10, 0, x, rnd);
 
161
      puts ("");
 
162
    }
 
163
 
 
164
  mpfr_set_prec (x, 53);
 
165
  mpfr_const_log2 (x, GMP_RNDZ);
 
166
  if (mpfr_cmp_str1 (x, "6.9314718055994530941e-1") )
 
167
    {
 
168
      printf ("mpfr_const_log2 failed for prec=53\n");
 
169
      exit (1);
 
170
    }
 
171
 
 
172
  mpfr_set_prec (x, 32);
 
173
  mpfr_const_log2 (x, GMP_RNDN);
 
174
  if (mpfr_cmp_str1 (x, "0.69314718060195446"))
 
175
    {
 
176
      printf ("mpfr_const_log2 failed for prec=32\n");
 
177
      exit (1);
 
178
    }
 
179
 
 
180
  mpfr_clear(x);
 
181
 
 
182
  check_large();
 
183
  check_cache ();
 
184
 
 
185
  tests_end_mpfr ();
 
186
  return 0;
 
187
}