1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_MULTIPRECISION_TEST_HPP
#define BOOST_MULTIPRECISION_TEST_HPP
#include <limits>
#include <cmath>
#include <typeinfo>
#include <boost/detail/lightweight_test.hpp>
#include <boost/current_function.hpp>
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include <boost/multiprecision/number.hpp>
namespace detail {
template <class T>
inline typename boost::disable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
abs(const T& a)
{
return a < 0 ? -a : a;
}
template <class T>
inline typename boost::enable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
abs(const T& a)
{
return a;
}
} // namespace detail
template <class T>
typename boost::enable_if_c<boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer, T>::type relative_error(T a, T b)
{
return a > b ? a - b : b - a;
}
template <class T>
typename boost::disable_if_c<(boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer) || boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
{
using ::detail::abs;
using std::abs;
T min_val = (std::numeric_limits<T>::min)();
T max_val = (std::numeric_limits<T>::max)();
if ((a != 0) && (b != 0))
{
if (a == b)
return 0;
// TODO: use isfinite:
if (abs(b) >= max_val)
{
if (abs(a) >= max_val)
return 0; // one infinity is as good as another!
}
// If the result is denormalised, treat all denorms as equivalent:
if ((a < min_val) && (a > 0))
a = min_val;
else if ((a > -min_val) && (a < 0))
a = -min_val;
if ((b < min_val) && (b > 0))
b = min_val;
else if ((b > -min_val) && (b < 0))
b = -min_val;
return (std::max)(abs(T((a - b) / a)), abs(T((a - b) / b))) / std::numeric_limits<T>::epsilon();
}
// Handle special case where one or both are zero:
if (min_val == 0)
return abs(T(a - b));
if (abs(a) < min_val)
a = min_val;
if (abs(b) < min_val)
b = min_val;
return (std::max)(abs(T((a - b) / a)), abs(T((a - b) / b))) / std::numeric_limits<T>::epsilon();
}
template <class T, class U>
typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type
relative_error(T a, U b)
{
typedef typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type cast_type;
return relative_error<cast_type>(static_cast<cast_type>(a), static_cast<cast_type>(b));
}
template <class T>
typename boost::enable_if_c<boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
{
typename boost::multiprecision::component_type<T>::type am = median(a);
typename boost::multiprecision::component_type<T>::type bm = median(b);
return relative_error<typename boost::multiprecision::component_type<T>::type>(am, bm);
}
enum
{
warn_on_fail,
error_on_fail,
abort_on_fail
};
template <class T>
inline T epsilon_of(const T&)
{
BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
return std::numeric_limits<T>::is_integer ? static_cast<T>(1) : std::numeric_limits<T>::epsilon();
}
template <class T>
inline int digits_of(const T&)
{
return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits10 + 3 : std::numeric_limits<long double>::digits10 + 3;
}
inline std::ostream& report_where(const char* file, int line, const char* function)
{
if (function)
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: " << function << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
}
#define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
inline void report_severity(int severity)
{
if (severity == error_on_fail)
++boost::detail::test_errors();
else if (severity == abort_on_fail)
{
++boost::detail::test_errors();
abort();
}
}
#define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
template <class E>
void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
{
report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
BOOST_MP_REPORT_SEVERITY(severity);
}
#ifndef BOOST_NO_EXCEPTIONS
#define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
catch (const std::exception& e) \
{ \
report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
catch (...) \
{ \
std::cout << "Exception of unknown type was thrown" << std::endl; \
report_severity(severity); \
}
#define BOOST_MP_TRY try
#else
#define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_MP_TRY
#endif
#define BOOST_CHECK_IMP(x, severity) \
BOOST_MP_TRY \
{ \
if (x) \
{ \
} \
else \
{ \
BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
#define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
#define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
#define BOOST_CLOSE_IMP(x, y, tol, severity) \
BOOST_MP_TRY \
{ \
if (relative_error(x, y) > tol) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(5) << std::fixed \
<< "Relative error was: " << relative_error(x, y) << "eps\n" \
<< "Tolerance was: " << tol << "eps" << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_EQUAL_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!((x) == (y))) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << (x) << "\n" \
<< "Value of RHS was: " << (y) << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_NE_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!(x != y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LT_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!(x < y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GT_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!(x > y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LE_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!(x <= y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GE_IMP(x, y, severity) \
BOOST_MP_TRY \
{ \
if (!(x >= y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
<< std::setprecision(digits_of(x)) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#ifndef BOOST_NO_EXCEPTIONS
#define BOOST_MT_CHECK_THROW_IMP(x, E, severity) \
BOOST_MP_TRY \
{ \
x; \
BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
catch (const E&) {} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#else
#define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
#endif
#define BOOST_CHECK_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (100 * epsilon_of(x)))), error_on_fail)
#define BOOST_WARN_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), warn_on_fail)
#define BOOST_REQUIRE_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), abort_on_fail)
#define BOOST_CHECK_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (epsilon_of(x)))), error_on_fail)
#define BOOST_WARN_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), warn_on_fail)
#define BOOST_REQUIRE_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), abort_on_fail)
#define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
#define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
#define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
#define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
#define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
#define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
#define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
#define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
#define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)
#endif
|