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

« back to all changes in this revision

Viewing changes to mpfr/tests/terf.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_erf.
 
2
 
 
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
4
Contributed by Ludovic Meunier and Paul Zimmermann.
 
5
 
 
6
This file is part of the MPFR Library.
 
7
 
 
8
The MPFR Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The MPFR Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
21
MA 02110-1301, USA. */
 
22
 
 
23
#include <math.h>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#include "mpfr-test.h"
 
28
 
 
29
#define TEST_FUNCTION mpfr_erf
 
30
#define test_generic test_generic_erf
 
31
#include "tgeneric.c"
 
32
 
 
33
#define TEST_FUNCTION mpfr_erfc
 
34
#define test_generic test_generic_erfc
 
35
#include "tgeneric.c"
 
36
 
 
37
static void
 
38
special_erf (void)
 
39
{
 
40
  mpfr_t x, y;
 
41
  int inex;
 
42
 
 
43
  mpfr_init2 (x, 53);
 
44
  mpfr_init2 (y, 53);
 
45
 
 
46
  /* erf(NaN) = NaN */
 
47
  mpfr_set_nan (x);
 
48
  mpfr_erf (y, x, GMP_RNDN);
 
49
  if (!mpfr_nan_p (y))
 
50
    {
 
51
      printf ("mpfr_erf failed for x=NaN\n");
 
52
      exit (1);
 
53
    }
 
54
 
 
55
  /* erf(+Inf) = 1 */
 
56
  mpfr_set_inf (x, 1);
 
57
  mpfr_erf (y, x, GMP_RNDN);
 
58
  if (mpfr_cmp_ui (y, 1))
 
59
    {
 
60
      printf ("mpfr_erf failed for x=+Inf\n");
 
61
      printf ("expected 1.0, got ");
 
62
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
63
      printf ("\n");
 
64
      exit (1);
 
65
    }
 
66
 
 
67
  /* erf(-Inf) = -1 */
 
68
  mpfr_set_inf (x, -1);
 
69
  mpfr_erf (y, x, GMP_RNDN);
 
70
  if (mpfr_cmp_si (y, -1))
 
71
    {
 
72
      printf ("mpfr_erf failed for x=-Inf\n");
 
73
      exit (1);
 
74
    }
 
75
 
 
76
  /* erf(+0) = +0 */
 
77
  mpfr_set_ui (x, 0, GMP_RNDN);
 
78
  mpfr_erf (y, x, GMP_RNDN);
 
79
  if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0)
 
80
    {
 
81
      printf ("mpfr_erf failed for x=+0\n");
 
82
      exit (1);
 
83
    }
 
84
 
 
85
  /* erf(-0) = -0 */
 
86
  mpfr_neg (x, x, GMP_RNDN);
 
87
  mpfr_erf (y, x, GMP_RNDN);
 
88
  if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0)
 
89
    {
 
90
      printf ("mpfr_erf failed for x=-0\n");
 
91
      exit (1);
 
92
    }
 
93
 
 
94
  mpfr_set_ui (x, 1, GMP_RNDN);
 
95
  mpfr_erf (x, x, GMP_RNDN);
 
96
  mpfr_set_str_binary (y, "0.11010111101110110011110100111010000010000100010001011");
 
97
  if (mpfr_cmp (x, y))
 
98
    {
 
99
      printf ("mpfr_erf failed for x=1.0, rnd=GMP_RNDN\n");
 
100
      printf ("expected ");
 
101
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
102
      printf ("\n");
 
103
      printf ("got      ");
 
104
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
105
      printf ("\n");
 
106
      exit (1);
 
107
    }
 
108
 
 
109
  mpfr_set_str (x, "6.6", 10, GMP_RNDN);
 
110
  mpfr_erf (x, x, GMP_RNDN);
 
111
  if (mpfr_cmp_ui (x, 1))
 
112
    {
 
113
      printf ("mpfr_erf failed for x=6.6, rnd=GMP_RNDN\n");
 
114
      printf ("expected 1\n");
 
115
      printf ("got      ");
 
116
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
117
      printf ("\n");
 
118
      exit (1);
 
119
    }
 
120
 
 
121
  mpfr_set_str (x, "-6.6", 10, GMP_RNDN);
 
122
  mpfr_erf (x, x, GMP_RNDN);
 
123
  if (mpfr_cmp_si (x, -1))
 
124
    {
 
125
      printf ("mpfr_erf failed for x=-6.6, rnd=GMP_RNDN\n");
 
126
      printf ("expected -1\n");
 
127
      printf ("got      ");
 
128
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
129
      printf ("\n");
 
130
      exit (1);
 
131
    }
 
132
 
 
133
  mpfr_set_str (x, "6.6", 10, GMP_RNDN);
 
134
  mpfr_erf (x, x, GMP_RNDZ);
 
135
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111111111111111111111111");
 
136
  if (mpfr_cmp (x, y))
 
137
    {
 
138
      printf ("mpfr_erf failed for x=6.6, rnd=GMP_RNDZ\n");
 
139
      printf ("expected ");
 
140
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
141
      printf ("\n");
 
142
      printf ("got      ");
 
143
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
144
      printf ("\n");
 
145
      exit (1);
 
146
    }
 
147
 
 
148
  mpfr_set_str (x, "4.5", 10, GMP_RNDN);
 
149
  mpfr_erf (x, x, GMP_RNDN);
 
150
  mpfr_set_str_binary (y, "0.1111111111111111111111111111111100100111110100011");
 
151
  if (mpfr_cmp (x, y))
 
152
    {
 
153
      printf ("mpfr_erf failed for x=4.5, rnd=GMP_RNDN\n");
 
154
      printf ("expected ");
 
155
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
156
      printf ("\n");
 
157
      printf ("got      ");
 
158
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
159
      printf ("\n");
 
160
      exit (1);
 
161
    }
 
162
 
 
163
  mpfr_set_prec (x, 120);
 
164
  mpfr_set_prec (y, 120);
 
165
  mpfr_set_str_binary (x, "0.110100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011E3");
 
166
  mpfr_erf (x, x, GMP_RNDN);
 
167
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111111111111111111111111111111111111100111111000100111011111011010000110101111100011001101");
 
168
  if (mpfr_cmp (x, y))
 
169
    {
 
170
      printf ("mpfr_erf failed for x=6.6, rnd=GMP_RNDN\n");
 
171
      printf ("expected ");
 
172
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
173
      printf ("\n");
 
174
      printf ("got      ");
 
175
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
176
      printf ("\n");
 
177
      exit (1);
 
178
    }
 
179
 
 
180
  mpfr_set_prec (x, 8);
 
181
  mpfr_set_prec (y, 8);
 
182
  mpfr_set_ui (x, 50, GMP_RNDN);
 
183
  inex = mpfr_erf (y, x, GMP_RNDN);
 
184
  if (mpfr_cmp_ui (y, 1))
 
185
    {
 
186
      printf ("mpfr_erf failed for x=50, rnd=GMP_RNDN\n");
 
187
      printf ("expected 1, got ");
 
188
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
189
      printf ("\n");
 
190
      exit (1);
 
191
    }
 
192
  if (inex <= 0)
 
193
    {
 
194
      printf ("mpfr_erf failed for x=50, rnd=GMP_RNDN: wrong ternary value\n"
 
195
              "expected positive, got %d\n", inex);
 
196
      exit (1);
 
197
    }
 
198
  inex = mpfr_erf (x, x, GMP_RNDZ);
 
199
  mpfr_nextbelow (y);
 
200
  if (mpfr_cmp (x, y))
 
201
    {
 
202
      printf ("mpfr_erf failed for x=50, rnd=GMP_RNDZ\n");
 
203
      printf ("expected ");
 
204
      mpfr_out_str (stdout, 2, 0, y, GMP_RNDN);
 
205
      printf ("\n");
 
206
      printf ("got      ");
 
207
      mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
 
208
      printf ("\n");
 
209
      exit (1);
 
210
    }
 
211
  if (inex >= 0)
 
212
    {
 
213
      printf ("mpfr_erf failed for x=50, rnd=GMP_RNDN: wrong ternary value\n"
 
214
              "expected negative, got %d\n", inex);
 
215
      exit (1);
 
216
    }
 
217
 
 
218
  mpfr_set_prec (x, 32);
 
219
  mpfr_set_prec (y, 32);
 
220
 
 
221
  mpfr_set_str_binary (x, "0.1010100100111011001111100101E-1");
 
222
  mpfr_set_str_binary (y, "0.10111000001110011010110001101011E-1");
 
223
  mpfr_erf (x, x, GMP_RNDN);
 
224
  if (mpfr_cmp (x, y))
 
225
    {
 
226
      printf ("Error: erf for prec=32 (1)\n");
 
227
      exit (1);
 
228
    }
 
229
 
 
230
  mpfr_set_str_binary (x, "-0.10110011011010111110010001100001");
 
231
  mpfr_set_str_binary (y, "-0.1010110110101011100010111000111");
 
232
  mpfr_erf (x, x, GMP_RNDN);
 
233
  if (mpfr_cmp (x, y))
 
234
    {
 
235
      printf ("Error: erf for prec=32 (2)\n");
 
236
      mpfr_print_binary (x); printf ("\n");
 
237
      exit (1);
 
238
    }
 
239
 
 
240
  mpfr_set_str_binary (x, "100.10001110011110100000110000111");
 
241
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111");
 
242
  mpfr_erf (x, x, GMP_RNDN);
 
243
  if (mpfr_cmp (x, y))
 
244
    {
 
245
      printf ("Error: erf for prec=32 (3)\n");
 
246
      exit (1);
 
247
    }
 
248
  mpfr_set_str_binary (x, "100.10001110011110100000110000111");
 
249
  mpfr_erf (x, x, GMP_RNDZ);
 
250
  if (mpfr_cmp (x, y))
 
251
    {
 
252
      printf ("Error: erf for prec=32 (4)\n");
 
253
      exit (1);
 
254
    }
 
255
  mpfr_set_str_binary (x, "100.10001110011110100000110000111");
 
256
  mpfr_erf (x, x, GMP_RNDU);
 
257
  if (mpfr_cmp_ui (x, 1))
 
258
    {
 
259
      printf ("Error: erf for prec=32 (5)\n");
 
260
      exit (1);
 
261
    }
 
262
 
 
263
  mpfr_set_str_binary (x, "100.10001110011110100000110001000");
 
264
  mpfr_erf (x, x, GMP_RNDN);
 
265
  if (mpfr_cmp_ui (x, 1))
 
266
    {
 
267
      printf ("Error: erf for prec=32 (6)\n");
 
268
      exit (1);
 
269
    }
 
270
  mpfr_set_str_binary (x, "100.10001110011110100000110001000");
 
271
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111");
 
272
  mpfr_erf (x, x, GMP_RNDZ);
 
273
  if (mpfr_cmp (x, y))
 
274
    {
 
275
      printf ("Error: erf for prec=32 (7)\n");
 
276
      exit (1);
 
277
    }
 
278
  mpfr_set_str_binary (x, "100.10001110011110100000110001000");
 
279
  mpfr_erf (x, x, GMP_RNDU);
 
280
  if (mpfr_cmp_ui (x, 1))
 
281
    {
 
282
      printf ("Error: erf for prec=32 (8)\n");
 
283
      exit (1);
 
284
    }
 
285
 
 
286
  mpfr_set_ui (x, 5, GMP_RNDN);
 
287
  mpfr_erf (x, x, GMP_RNDN);
 
288
  if (mpfr_cmp_ui (x, 1))
 
289
    {
 
290
      printf ("Error: erf for prec=32 (9)\n");
 
291
      exit (1);
 
292
    }
 
293
  mpfr_set_ui (x, 5, GMP_RNDN);
 
294
  mpfr_erf (x, x, GMP_RNDU);
 
295
  if (mpfr_cmp_ui (x, 1))
 
296
    {
 
297
      printf ("Error: erf for prec=32 (10)\n");
 
298
      exit (1);
 
299
    }
 
300
  mpfr_set_ui (x, 5, GMP_RNDN);
 
301
  mpfr_erf (x, x, GMP_RNDZ);
 
302
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111");
 
303
  if (mpfr_cmp (x, y))
 
304
    {
 
305
      printf ("Error: erf for prec=32 (11)\n");
 
306
      exit (1);
 
307
    }
 
308
  mpfr_set_ui (x, 5, GMP_RNDN);
 
309
  mpfr_erf (x, x, GMP_RNDD);
 
310
  mpfr_set_str_binary (y, "0.11111111111111111111111111111111");
 
311
  if (mpfr_cmp (x, y))
 
312
    {
 
313
      printf ("Error: erf for prec=32 (12)\n");
 
314
      exit (1);
 
315
    }
 
316
 
 
317
  mpfr_set_prec (x, 43);
 
318
  mpfr_set_prec (y, 64);
 
319
  mpfr_set_str_binary (x, "-0.1101110110101111100101011101110101101001001e3");
 
320
  mpfr_erf (y, x, GMP_RNDU);
 
321
  mpfr_set_prec (x, 64);
 
322
  mpfr_set_str_binary (x, "-0.1111111111111111111111111111111111111111111111111111111111111111");
 
323
  if (mpfr_cmp (x, y))
 
324
    {
 
325
      printf ("Error: erf for prec=43,64 (13)\n");
 
326
      exit (1);
 
327
    }
 
328
 
 
329
 
 
330
  mpfr_clear (x);
 
331
  mpfr_clear (y);
 
332
}
 
333
 
 
334
static void
 
335
special_erfc (void)
 
336
{
 
337
  mpfr_t x, y;
 
338
 
 
339
  mpfr_inits (x, y, NULL);
 
340
 
 
341
  /* erfc (NaN) = NaN */
 
342
  mpfr_set_nan (x);
 
343
  mpfr_erfc (y, x, GMP_RNDN);
 
344
  if (!mpfr_nan_p (y))
 
345
    {
 
346
      printf ("mpfr_erfc failed for x=NaN\n");
 
347
      exit (1);
 
348
    }
 
349
  /* erfc(+Inf) = 0+ */
 
350
  mpfr_set_inf (x, 1);
 
351
  mpfr_erfc (y, x, GMP_RNDN);
 
352
  if (!MPFR_IS_ZERO (y) || !MPFR_IS_POS (y))
 
353
    {
 
354
      printf ("mpfr_erf failed for x=+Inf\n");
 
355
      printf ("expected 0+, got ");
 
356
      mpfr_dump (y);
 
357
      exit (1);
 
358
    }
 
359
  /* erfc(-Inf) = 2 */
 
360
  mpfr_set_inf (x, -1);
 
361
  mpfr_erfc (y, x, GMP_RNDN);
 
362
  if (mpfr_cmp_ui (y, 2))
 
363
    {
 
364
      printf ("mpfr_erf failed for x=-Inf\n");
 
365
      printf ("expected 2, got ");
 
366
      mpfr_dump (y);
 
367
      exit (1);
 
368
    }
 
369
  /* erf(+0) = 1 */
 
370
  mpfr_set_ui (x, 0, GMP_RNDN);
 
371
  mpfr_erfc (y, x, GMP_RNDN);
 
372
  if (mpfr_cmp_ui (y, 1))
 
373
    {
 
374
      printf ("mpfr_erf failed for x=+0\n");
 
375
      printf ("expected 1, got ");
 
376
      mpfr_dump (y);
 
377
      exit (1);
 
378
    }
 
379
 
 
380
  mpfr_clears (x, y, NULL);
 
381
}
 
382
 
 
383
int
 
384
main (int argc, char *argv[])
 
385
{
 
386
  tests_start_mpfr ();
 
387
 
 
388
  special_erf ();
 
389
  special_erfc ();
 
390
 
 
391
  test_generic_erf (2, 100, 15);
 
392
  test_generic_erfc (2, 100, 15);
 
393
 
 
394
  tests_end_mpfr ();
 
395
  return 0;
 
396
}