1 /* Test file for mpfr_reldiff.
2
3 Copyright 2000-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU 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 3 of the License, or (at your
11 option) any later version.
12
13 The GNU 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 GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include "mpfr-test.h"
24
25 #define SPECIAL_MAX 12
26
27 /* copied from reuse.c */
28 static void
29 set_special (mpfr_ptr x, unsigned int select)
30 {
31 MPFR_ASSERTN (select < SPECIAL_MAX);
32 switch (select)
33 {
34 case 0:
35 MPFR_SET_NAN (x);
36 break;
37 case 1:
38 MPFR_SET_INF (x);
39 MPFR_SET_POS (x);
40 break;
41 case 2:
42 MPFR_SET_INF (x);
43 MPFR_SET_NEG (x);
44 break;
45 case 3:
46 MPFR_SET_ZERO (x);
47 MPFR_SET_POS (x);
48 break;
49 case 4:
50 MPFR_SET_ZERO (x);
51 MPFR_SET_NEG (x);
52 break;
53 case 5:
54 mpfr_set_str_binary (x, "1");
55 break;
56 case 6:
57 mpfr_set_str_binary (x, "-1");
58 break;
59 case 7:
60 mpfr_set_str_binary (x, "1e-1");
61 break;
62 case 8:
63 mpfr_set_str_binary (x, "1e+1");
64 break;
65 case 9:
66 mpfr_const_pi (x, MPFR_RNDN);
67 break;
68 case 10:
69 mpfr_const_pi (x, MPFR_RNDN);
70 MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1);
71 break;
72 case 11:
73 mpfr_urandomb (x, RANDS);
74 if (RAND_BOOL ())
75 mpfr_neg (x, x, MPFR_RNDN);
76 break;
77 default:
78 MPFR_ASSERTN (0);
79 }
80 }
81
82 int
83 main (void)
84 {
85 mpfr_t a1, a2, b, c;
86 int pa, pb, pc, i, j, r;
87 int prec[] = { MPFR_PREC_MIN, 3, 16, 32, 53 };
88
89 tests_start_mpfr ();
90
91 /* Just check that mpfr_reldiff(a,b,c,rnd) computes |b-c|/b using the
92 precision of a, without any optimization. */
93
94 for (pa = 0; pa < numberof(prec); pa++)
95 {
96 mpfr_inits2 (prec[pa], a1, a2, (mpfr_ptr) 0);
97 for (pb = 0; pb < numberof(prec); pb++)
98 {
99 mpfr_init2 (b, prec[pb]);
100 for (pc = 0; pc < numberof(prec); pc++)
101 {
102 mpfr_init2 (c, prec[pc]);
103 for (i = 0; i < SPECIAL_MAX; i++)
104 {
105 set_special (b, i);
106 for (j = 0; j < SPECIAL_MAX; j++)
107 {
108 set_special (c, j);
109 RND_LOOP (r)
110 {
111 mpfr_rnd_t rnd = (mpfr_rnd_t) r;
112
113 mpfr_sub (a1, b, c, rnd);
114 mpfr_abs (a1, a1, rnd);
115 mpfr_div (a1, a1, b, rnd);
116 mpfr_reldiff (a2, b, c, rnd);
117 if (! SAME_VAL (a1, a2))
118 {
119 printf ("Error for pa=%d pb=%d pc=%d "
120 "i=%d j=%d rnd=%s\n", pa, pb, pc,
121 i, j, mpfr_print_rnd_mode (rnd));
122 printf ("expected ");
123 mpfr_dump (a1);
124 printf ("got ");
125 mpfr_dump (a2);
126 exit (1);
127 }
128 }
129 }
130 }
131 mpfr_clear (c);
132 }
133 mpfr_clear (b);
134 }
135 mpfr_clears (a1, a2, (mpfr_ptr) 0);
136 }
137
138 tests_end_mpfr ();
139 return 0;
140 }