1 /* Test precision of mpf_class expressions.
3 Copyright 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP 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 3 of the License, or (at your
10 option) any later version.
12 The GNU MP 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 GNU MP Library. If not, see http://www.gnu.org/licenses/. */
33 small_prec = 64, medium_prec = 128, large_prec = 192, very_large_prec = 256;
35 #define ASSERT_ALWAYS_PREC(a, s, prec) \
37 mpf_srcptr _a = a.get_mpf_t(); \
38 mpf_class _b(s, prec); \
39 mpf_srcptr _c = _b.get_mpf_t(); \
40 ASSERT_ALWAYS(mpf_eq(_a, _c, prec)); \
48 mpf_set_default_prec(medium_prec);
52 mpf_class f(3.0, small_prec);
53 mpf_class g(1 / f, very_large_prec);
55 (g, "0.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
56 " 33333 33333 33333 33333 33333 333", very_large_prec);
59 mpf_class f(9.0, medium_prec);
60 mpf_class g(0.0, very_large_prec);
63 (g, "0.11111 11111 11111 11111 11111 11111 11111 11111 11111 11111"
64 " 11111 11111 11111 11111 11111 111", very_large_prec);
67 mpf_class f(15.0, large_prec);
68 mpf_class g(0.0, very_large_prec);
71 (g, "0.06666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
72 " 66666 66666 66666 66666 66666 667", very_large_prec);
75 // compound expressions
77 mpf_class f(3.0, small_prec);
78 mpf_class g(-(-(-1 / f)), very_large_prec);
80 (g, "-0.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
81 " 33333 33333 33333 33333 33333 333", very_large_prec);
84 mpf_class f(3.0, small_prec), g(9.0, medium_prec);
85 mpf_class h(0.0, very_large_prec);
88 (h, "0.44444 44444 44444 44444 44444 44444 44444 44444 44444 44444"
89 " 44444 44444 44444 44444 44444 444", very_large_prec);
92 mpf_class f(3.0, small_prec), g(9.0, medium_prec), h(15.0, large_prec);
93 mpf_class i(0.0, very_large_prec);
96 (i, "15.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
97 " 33333 33333 33333 33333 33333 3", very_large_prec);
100 mpf_class f(3.0, small_prec);
101 mpf_class g(-(1 + f) / 3, very_large_prec);
103 (g, "-1.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
104 " 33333 33333 33333 33333 33333 33", very_large_prec);
107 mpf_class f(9.0, medium_prec);
108 mpf_class g(0.0, very_large_prec);
111 (g, "0.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
112 " 33333 33333 33333 33333 33333 333", very_large_prec);
115 mpf_class f(15.0, large_prec);
116 mpf_class g(0.0, very_large_prec);
117 g = hypot(1 + 5 / f, 1.0);
119 (g, "1.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
120 " 66666 66666 66666 66666 66666 67", very_large_prec);
123 // compound assignments
125 mpf_class f(3.0, small_prec), g(9.0, medium_prec);
126 mpf_class h(1.0, very_large_prec);
129 (h, "0.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
130 " 66666 66666 66666 66666 66666 667", very_large_prec);
133 // construction from expressions
135 mpf_class f(3.0, small_prec);
136 mpf_class g(0.0, very_large_prec);
137 g = mpf_class(1 / f);
138 ASSERT_ALWAYS_PREC(g, "0.33333 33333 33333 33333", small_prec);
141 mpf_class f(9.0, medium_prec);
142 mpf_class g(0.0, very_large_prec);
143 g = mpf_class(1 / f);
145 (g, "0.11111 11111 11111 11111 11111 11111 11111 1111", medium_prec);
148 mpf_class f(15.0, large_prec);
149 mpf_class g(0.0, very_large_prec);
150 g = mpf_class(1 / f);
152 (g, "0.06666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
153 " 66666 6667", large_prec);
157 mpf_class f(3.0, small_prec), g(9.0, medium_prec);
158 mpf_class h(0.0, very_large_prec);
159 h = mpf_class(f / g + 1, large_prec);
161 (h, "1.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"
166 // mixed mpf/mpq expressions
168 mpf_class f(3.0, small_prec);
170 mpf_class g(0.0, very_large_prec);
173 (g, "2.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
174 " 66666 66666 66666 66666 66666 67", very_large_prec);
178 mpf_class f(3.0, small_prec);
180 mpf_class g(0.0, very_large_prec);
181 g = mpf_class(f - q, large_prec);
183 (g, "2.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666"
188 mpf_class f(3.0, small_prec);
190 mpf_class g(0.0, very_large_prec);
191 g = mpf_class(f - q);
193 (g, "2.66666 66666 66666 66666 66666 66666 66666 667", medium_prec);
196 mpf_class f(15.0, large_prec);
198 mpf_class g(0.0, very_large_prec);
199 g = mpf_class(f + q);
201 (g, "15.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333"