1
/* Test file for in-place operations.
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. */
26
#include "mpfr-impl.h"
28
typedef void (*fct_t)();
31
void test3 _PROTO ((char *, mp_prec_t, mp_rnd_t));
32
void test4 _PROTO ((char *, mp_prec_t, mp_rnd_t));
33
void test3a _PROTO ((char *, mp_prec_t, mp_rnd_t));
34
void test2ui _PROTO ((char *, mp_prec_t, mp_rnd_t));
35
void testui2 _PROTO ((char *, mp_prec_t, mp_rnd_t));
36
void test2 _PROTO ((char *, mp_prec_t, mp_rnd_t));
37
void test2a _PROTO ((char *, mp_prec_t));
38
int mpfr_compare _PROTO ((mpfr_t, mpfr_t));
40
/* same than mpfr_cmp, but returns 0 for both NaN's */
42
mpfr_compare (mpfr_t a, mpfr_t b)
44
return (MPFR_IS_NAN(a)) ? !MPFR_IS_NAN(b) :
45
(MPFR_IS_NAN(b) || mpfr_cmp(a, b));
49
test3 (char *foo, mp_prec_t prec, mp_rnd_t rnd)
51
mpfr_t ref1, ref2, ref3;
56
printf("checking %s\n", foo);
58
mpfr_init2 (ref1, prec);
59
mpfr_init2 (ref2, prec);
60
mpfr_init2 (ref3, prec);
61
mpfr_init2 (res1, prec);
63
/* for each variable, consider each of the following 6 possibilities:
64
NaN, +Infinity, -Infinity, +0, -0 or a random number */
65
for (i=0; i<36; i++) {
66
if (i%6==0) mpfr_set_nan (ref2);
67
if (i%6==1) mpfr_set_inf (ref2, 1);
68
if (i%6==2) mpfr_set_inf (ref2, -1);
69
if (i%6==3) mpfr_set_d (ref2, 0.0, GMP_RNDN);
70
if (i%6==4) mpfr_set_d (ref2, -0.0, GMP_RNDN);
71
if (i%6==5) mpfr_random (ref2);
73
if (i/6==0) mpfr_set_nan (ref3);
74
if (i/6==1) mpfr_set_inf (ref3, 1);
75
if (i/6==2) mpfr_set_inf (ref3, -1);
76
if (i/6==3) mpfr_set_d (ref3, 0.0, GMP_RNDN);
77
if (i/6==4) mpfr_set_d (ref3, -0.0, GMP_RNDN);
78
if (i/6==5) mpfr_random (ref3);
80
/* reference call: foo(a, b, c) */
81
testfunc (ref1, ref2, ref3, rnd);
84
mpfr_set (res1, ref2, rnd); /* exact operation */
85
testfunc (res1, res1, ref3, rnd);
87
if (mpfr_compare (res1, ref1)) {
88
fprintf (stderr, "Error for %s(a, a, c) for a=%e, c=%e\n", foo,
89
mpfr_get_d1 (ref2), mpfr_get_d1 (ref3));
90
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
96
mpfr_set (res1, ref3, rnd);
97
testfunc (res1, ref2, res1, rnd);
98
if (mpfr_compare (res1, ref1)) {
99
fprintf (stderr, "Error for %s(a, b, a) for b=%e, a=%e\n", foo,
100
mpfr_get_d1 (ref2), mpfr_get_d1 (ref3));
101
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
107
mpfr_set (ref3, ref2, rnd);
108
testfunc (ref1, ref2, ref3, rnd);
109
mpfr_set (res1, ref2, rnd);
110
testfunc (res1, res1, res1, rnd);
112
if (mpfr_compare (res1, ref1)) {
113
fprintf (stderr, "Error for %s(a, a, a) for a=%e\n", foo,
115
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
128
test4 (char *foo, mp_prec_t prec, mp_rnd_t rnd)
130
mpfr_t ref, op1, op2, op3;
135
printf("checking %s\n", foo);
137
mpfr_init2 (ref, prec);
138
mpfr_init2 (op1, prec);
139
mpfr_init2 (op2, prec);
140
mpfr_init2 (op3, prec);
141
mpfr_init2 (res, prec);
143
/* for each variable, consider each of the following 6 possibilities:
144
NaN, +Infinity, -Infinity, +0, -0 or a random number */
149
MPFR_CLEAR_FLAGS(op1);
150
if (i==0) mpfr_set_nan (op1);
151
if (i==1) mpfr_set_inf (op1, 1);
152
if (i==2) mpfr_set_inf (op1, -1);
153
if (i==3) mpfr_set_d (op1, 0.0, GMP_RNDN);
154
if (i==4) mpfr_set_d (op1, -0.0, GMP_RNDN);
155
if (i==5) mpfr_random (op1);
160
MPFR_CLEAR_FLAGS(op2);
161
if (j==0) mpfr_set_nan (op2);
162
if (j==1) mpfr_set_inf (op2, 1);
163
if (j==2) mpfr_set_inf (op2, -1);
164
if (j==3) mpfr_set_d (op2, 0.0, GMP_RNDN);
165
if (j==4) mpfr_set_d (op2, -0.0, GMP_RNDN);
166
if (j==5) mpfr_random (op2);
171
MPFR_CLEAR_FLAGS(op3);
172
if (k==0) mpfr_set_nan (op3);
173
if (k==1) mpfr_set_inf (op3, 1);
174
if (k==2) mpfr_set_inf (op3, -1);
175
if (k==3) mpfr_set_d (op3, 0.0, GMP_RNDN);
176
if (k==4) mpfr_set_d (op3, -0.0, GMP_RNDN);
177
if (k==5) mpfr_random (op3);
179
/* reference call: foo(s, a, b, c) */
180
testfunc (ref, op1, op2, op3, rnd);
182
/* foo(a, a, b, c) */
183
mpfr_set (res, op1, rnd); /* exact operation */
184
testfunc (res, res, op2, op3, rnd);
186
if (mpfr_compare (res, ref))
189
"Error for %s(a, a, b, c) for a=%e, b=%e, c=%e\n",
191
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
192
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
197
/* foo(b, a, b, c) */
198
mpfr_set (res, op2, rnd);
199
testfunc (res, op1, res, op3, rnd);
201
if (mpfr_compare (res, ref))
204
"Error for %s(b, a, b, c) for a=%e, b=%e, c=%e\n",
206
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
207
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
212
/* foo(c, a, b, c) */
213
mpfr_set (res, op3, rnd);
214
testfunc (res, op1, op2, res, rnd);
216
if (mpfr_compare (res, ref))
219
"Error for %s(c, a, b, c) for a=%e, b=%e, c=%e\n",
221
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
222
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
228
testfunc (ref, op1, op1, op3, rnd);
229
mpfr_set (res, op1, rnd);
230
testfunc (res, res, res, op3, rnd);
231
if (mpfr_compare (res, ref))
234
"Error for %s(a, a, a, c) for a=%e, a=%e, c=%e\n",
236
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
237
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
243
testfunc (ref, op1, op2, op1, rnd);
244
mpfr_set (res, op1, rnd);
245
testfunc (res, res, op2, res, rnd);
246
if (mpfr_compare (res, ref))
249
"Error for %s(a, a, b, a) for a=%e, a=%e, c=%e\n",
251
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
252
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
257
/* foo(b, a, b, b) */
258
testfunc (ref, op1, op2, op2, rnd);
259
mpfr_set (res, op2, rnd);
260
testfunc (res, op1, res, res, rnd);
261
if (mpfr_compare (res, ref))
264
"Error for %s(b, a, b, b) for a=%e, a=%e, c=%e\n",
266
mpfr_get_d1 (op1), mpfr_get_d1 (op2), mpfr_get_d1 (op3));
267
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
272
/* foo (a, a, a, a) */
273
testfunc (ref, op1, op1, op1 ,rnd);
274
mpfr_set (res, op1, rnd);
275
testfunc (res, res, res, res, rnd);
276
if (mpfr_compare (res, ref))
279
"Error for %s(a, a, a, a) for a=%e\n", foo,
281
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref),
298
test2ui (char *foo, mp_prec_t prec, mp_rnd_t rnd)
307
printf("checking %s\n", foo);
309
mpfr_init2 (ref1, prec);
310
mpfr_init2 (ref2, prec);
311
mpfr_init2 (res1, prec);
315
/* ref2 can be NaN, +Inf, -Inf, +0, -0 or any number
316
ref3 can be 0 or any number */
319
if (i%6==0) mpfr_set_nan (ref2);
320
if (i%6==1) mpfr_set_inf (ref2, 1);
321
if (i%6==2) mpfr_set_inf (ref2, -1);
322
if (i%6==3) mpfr_set_d (ref2, 0.0, GMP_RNDN);
323
if (i%6==4) mpfr_set_d (ref2, -0.0, GMP_RNDN);
324
if (i%6==5) mpfr_random (ref2);
331
ref3 = (unsigned int) c[0];
334
/* reference call: foo(a, b, c) */
335
testfunc (ref1, ref2, ref3, rnd);
338
mpfr_set (res1, ref2, rnd); /* exact operation */
339
testfunc (res1, res1, ref3, rnd);
341
if (mpfr_compare (res1, ref1))
343
fprintf (stderr, "Error for %s(a, a, c) for a=%e c=%u\n", foo,
344
mpfr_get_d1 (ref2), ref3);
345
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
357
testui2 (char *foo, mp_prec_t prec, mp_rnd_t rnd)
366
printf("checking %s\n", foo);
368
mpfr_init2 (ref1, prec);
369
mpfr_init2 (ref3, prec);
370
mpfr_init2 (res1, prec);
373
ref2 = (unsigned int) c[0];
375
for (i=0; i<12; i++) {
376
if (i%6==0) mpfr_set_nan (ref3);
377
if (i%6==1) mpfr_set_inf (ref3, 1);
378
if (i%6==2) mpfr_set_inf (ref3, -1);
379
if (i%6==3) mpfr_set_d (ref3, 0.0, GMP_RNDN);
380
if (i%6==4) mpfr_set_d (ref3, -0.0, GMP_RNDN);
381
if (i%6==5) mpfr_random (ref3);
386
ref2 = (unsigned int) c[0];
389
/* reference call: foo(a, b, c) */
390
testfunc (ref1, ref2, ref3, rnd);
393
mpfr_set (res1, ref3, rnd); /* exact operation */
394
testfunc (res1, ref2, res1, rnd);
395
if (mpfr_compare (res1, ref1)) {
396
fprintf (stderr, "Error for %s(a, b, a) for b=%u a=%e\n", foo,
397
ref2, mpfr_get_d1 (ref3));
398
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
409
/* foo(mpfr_ptr, mpfr_srcptr, mp_rndt) */
411
test2 (char *foo, mp_prec_t prec, mp_rnd_t rnd)
418
printf("checking %s\n", foo);
420
mpfr_init2 (ref1, prec);
421
mpfr_init2 (ref2, prec);
422
mpfr_init2 (res1, prec);
425
for (i=0; i<6; i++) {
426
if (i==0) mpfr_set_nan (ref2);
427
if (i==1) mpfr_set_inf (ref2, 1);
428
if (i==2) mpfr_set_inf (ref2, -1);
429
if (i==3) mpfr_set_d (ref2, 0.0, GMP_RNDN);
430
if (i==4) mpfr_set_d (ref2, -0.0, GMP_RNDN);
431
if (i==5) mpfr_random (ref2);
433
/* reference call: foo(a, b) */
434
testfunc (ref1, ref2, rnd);
437
mpfr_set (res1, ref2, rnd); /* exact operation */
438
testfunc (res1, res1, rnd);
439
if (mpfr_compare (res1, ref1)) {
440
fprintf (stderr, "Error for %s(a, a) for a=%e\n", foo, mpfr_get_d1 (ref2));
441
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
452
/* foo(mpfr_ptr, mpfr_srcptr) */
454
test2a (char *foo, mp_prec_t prec)
461
printf("checking %s\n", foo);
463
mpfr_init2 (ref1, prec);
464
mpfr_init2 (ref2, prec);
465
mpfr_init2 (res1, prec);
468
for (i=0; i<6; i++) {
469
if (i==0) mpfr_set_nan (ref2);
470
if (i==1) mpfr_set_inf (ref2, 1);
471
if (i==2) mpfr_set_inf (ref2, -1);
472
if (i==3) mpfr_set_d (ref2, 0.0, GMP_RNDN);
473
if (i==4) mpfr_set_d (ref2, -0.0, GMP_RNDN);
474
if (i==5) mpfr_random (ref2);
476
/* reference call: foo(a, b) */
477
testfunc (ref1, ref2);
480
mpfr_set (res1, ref2, GMP_RNDN); /* exact operation */
481
testfunc (res1, res1);
482
if (mpfr_compare (res1, ref1)) {
483
fprintf (stderr, "Error for %s(a, a) for a=%e\n", foo, mpfr_get_d1 (ref2));
484
fprintf (stderr, "expected %e, got %e\n", mpfr_get_d1 (ref1),
495
/* one operand, two results */
497
test3a (char *foo, mp_prec_t prec, mp_rnd_t rnd)
499
mpfr_t ref1, ref2, ref3;
504
printf("checking %s\n", foo);
506
mpfr_init2 (ref1, prec);
507
mpfr_init2 (ref2, prec);
508
mpfr_init2 (ref3, prec);
509
mpfr_init2 (res1, prec);
510
mpfr_init2 (res2, prec);
513
for (i=0; i<6; i++) {
514
if (i==0) mpfr_set_nan (ref3);
515
if (i==1) mpfr_set_inf (ref3, 1);
516
if (i==2) mpfr_set_inf (ref3, -1);
517
if (i==3) mpfr_set_d (ref3, 0.0, GMP_RNDN);
518
if (i==4) mpfr_set_d (ref3, -0.0, GMP_RNDN);
519
if (i==5) mpfr_random (ref3);
521
/* reference call: foo(a, b, c) */
522
testfunc (ref1, ref2, ref3, rnd);
525
mpfr_set (res1, ref3, rnd); /* exact operation */
526
testfunc (res1, res2, res1, rnd);
527
if (mpfr_compare (res1, ref1) || mpfr_compare (res2, ref2)) {
528
fprintf (stderr, "Error for %s(a, b, a) for a=%e\n", foo, mpfr_get_d1 (ref3));
529
fprintf (stderr, "expected (%e,%e), got (%e,%e)\n", mpfr_get_d1 (ref1),
530
mpfr_get_d1 (ref2), mpfr_get_d1 (res1), mpfr_get_d1 (res2));
535
mpfr_set (res2, ref3, rnd); /* exact operation */
536
testfunc (res1, res2, res2, rnd);
537
if (mpfr_compare (res1, ref1) || mpfr_compare (res2, ref2)) {
538
fprintf (stderr, "Error for %s(a, b, b) for b=%e\n", foo, mpfr_get_d1 (ref3));
539
fprintf (stderr, "expected (%e,%e), got (%e,%e)\n", mpfr_get_d1 (ref1),
540
mpfr_get_d1 (ref2), mpfr_get_d1 (res1), mpfr_get_d1 (res2));
555
testfunc = (fct_t) mpfr_add; test3 ("mpfr_add", 53, GMP_RNDN);
556
testfunc = (fct_t) mpfr_add_ui; test2ui ("mpfr_add_ui", 53, GMP_RNDN);
557
testfunc = (fct_t) mpfr_agm; test3 ("mpfr_agm", 53, GMP_RNDN);
558
testfunc = (fct_t) mpfr_ceil; test2 ("mpfr_ceil", 53, GMP_RNDN);
559
testfunc = (fct_t) mpfr_div; test3 ("mpfr_div", 53, GMP_RNDN);
560
testfunc = (fct_t) mpfr_div_2exp; test2ui ("mpfr_div_2exp", 53, GMP_RNDN);
561
testfunc = (fct_t) mpfr_div_ui; test2ui ("mpfr_div_ui", 53, GMP_RNDN);
562
testfunc = (fct_t) mpfr_exp; test2 ("mpfr_exp", 53, GMP_RNDN);
563
testfunc = (fct_t) mpfr_floor; test2 ("mpfr_floor", 53, GMP_RNDN);
564
testfunc = (fct_t) mpfr_log; test2 ("mpfr_log", 53, GMP_RNDN);
565
testfunc = (fct_t) mpfr_mul; test3 ("mpfr_mul", 53, GMP_RNDN);
566
testfunc = (fct_t) mpfr_mul_2exp; test2ui ("mpfr_mul_2exp", 53, GMP_RNDN);
567
testfunc = (fct_t) mpfr_mul_ui; test2ui ("mpfr_mul_ui", 53, GMP_RNDN);
568
testfunc = (fct_t) mpfr_neg; test2 ("mpfr_neg", 53, GMP_RNDN);
569
testfunc = (fct_t) mpfr_pow_ui; test2ui ("mpfr_pow_ui", 53, GMP_RNDN);
570
testfunc = (fct_t) mpfr_reldiff; test3 ("mpfr_reldiff", 53, GMP_RNDN);
571
testfunc = (fct_t) mpfr_sub; test3 ("mpfr_sub", 53, GMP_RNDN);
572
testfunc = (fct_t) mpfr_sub_ui; test2ui ("mpfr_sub_ui", 53, GMP_RNDN);
573
testfunc = (fct_t) mpfr_sqrt; test2 ("mpfr_sqrt", 53, GMP_RNDN);
574
testfunc = (fct_t) mpfr_ui_div; testui2 ("mpfr_ui_div", 53, GMP_RNDN);
575
testfunc = (fct_t) mpfr_ui_sub; testui2 ("mpfr_ui_sub", 53, GMP_RNDN);
576
testfunc = (fct_t) mpfr_trunc; test2 ("mpfr_trunc", 53, GMP_RNDN);
577
testfunc = (fct_t) mpfr_asin; test2 ("mpfr_asin", 53, GMP_RNDN);
578
testfunc = (fct_t) mpfr_acos; test2 ("mpfr_acos", 53, GMP_RNDN);
579
testfunc = (fct_t) mpfr_atan; test2 ("mpfr_atan", 53, GMP_RNDN);
580
testfunc = (fct_t) mpfr_sinh; test2 ("mpfr_sinh", 53, GMP_RNDN);
581
testfunc = (fct_t) mpfr_cosh; test2 ("mpfr_cosh", 53, GMP_RNDN);
582
testfunc = (fct_t) mpfr_tanh; test2 ("mpfr_tanh", 53, GMP_RNDN);
583
testfunc = (fct_t) mpfr_asinh; test2 ("mpfr_asinh", 53, GMP_RNDN);
584
testfunc = (fct_t) mpfr_acosh; test2 ("mpfr_acosh", 53, GMP_RNDN);
585
testfunc = (fct_t) mpfr_atanh; test2 ("mpfr_atanh", 53, GMP_RNDN);
586
testfunc = (fct_t) mpfr_exp2; test2 ("mpfr_exp2", 53, GMP_RNDN);
587
testfunc = (fct_t) mpfr_cos; test2 ("mpfr_cos", 53, GMP_RNDN);
588
testfunc = (fct_t) mpfr_sin; test2 ("mpfr_sin", 53, GMP_RNDN);
589
testfunc = (fct_t) mpfr_tan; test2 ("mpfr_tan", 53, GMP_RNDN);
590
testfunc = (fct_t) mpfr_log10; test2 ("mpfr_log10", 53, GMP_RNDN);
591
testfunc = (fct_t) mpfr_log2; test2 ("mpfr_log2", 53, GMP_RNDN);
592
testfunc = (fct_t) mpfr_pow; test3 ("mpfr_pow", 53, GMP_RNDN);
593
testfunc = (fct_t) mpfr_fma; test4 ("mpfr_fma", 53, GMP_RNDN);