]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////// |
2 | // Copyright 2011 John Maddock. Distributed under the Boost | |
3 | // Software License, Version 1.0. (See accompanying file | |
92f5a8d4 | 4 | // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt |
7c673cae FG |
5 | |
6 | #ifdef _MSC_VER | |
92f5a8d4 | 7 | #define _SCL_SECURE_NO_WARNINGS |
7c673cae FG |
8 | #endif |
9 | ||
10 | #include "test.hpp" | |
11 | ||
92f5a8d4 TL |
12 | #if !defined(TEST_MPF_50) && !defined(TEST_MPF) && !defined(TEST_BACKEND) && !defined(TEST_MPZ) && \ |
13 | !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR) && !defined(TEST_MPFR_50) && !defined(TEST_MPQ) && \ | |
14 | !defined(TEST_TOMMATH) && !defined(TEST_CPP_INT) && !defined(TEST_MPFI_50) && !defined(TEST_FLOAT128) && !defined(TEST_CPP_BIN_FLOAT) | |
15 | #define TEST_MPF_50 | |
16 | #define TEST_MPF | |
17 | #define TEST_BACKEND | |
18 | #define TEST_MPZ | |
19 | #define TEST_MPFR | |
20 | #define TEST_MPFR_50 | |
21 | #define TEST_CPP_DEC_FLOAT | |
22 | #define TEST_MPQ | |
23 | #define TEST_TOMMATH | |
24 | #define TEST_CPP_INT | |
25 | #define TEST_MPFI_50 | |
26 | #define TEST_FLOAT128 | |
27 | #define TEST_CPP_BIN_FLOAT | |
7c673cae FG |
28 | |
29 | #ifdef _MSC_VER | |
30 | #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!") | |
31 | #endif | |
32 | #ifdef __GNUC__ | |
33 | #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!" | |
34 | #endif | |
35 | ||
36 | #endif | |
37 | ||
38 | #if defined(TEST_MPF_50) || defined(TEST_MPF) || defined(TEST_MPZ) || defined(TEST_MPQ) | |
39 | #include <boost/multiprecision/gmp.hpp> | |
40 | #endif | |
41 | #ifdef TEST_BACKEND | |
42 | #include <boost/multiprecision/concepts/mp_number_archetypes.hpp> | |
43 | #endif | |
44 | #ifdef TEST_CPP_DEC_FLOAT | |
45 | #include <boost/multiprecision/cpp_dec_float.hpp> | |
46 | #endif | |
47 | #if defined(TEST_MPFR) || defined(TEST_MPFR_50) | |
48 | #include <boost/multiprecision/mpfr.hpp> | |
49 | #endif | |
50 | #if defined(TEST_MPFI_50) | |
51 | #include <boost/multiprecision/mpfi.hpp> | |
52 | #endif | |
53 | #ifdef TEST_TOMMATH | |
54 | #include <boost/multiprecision/tommath.hpp> | |
55 | #endif | |
56 | #ifdef TEST_CPP_INT | |
57 | #include <boost/multiprecision/cpp_int.hpp> | |
58 | #endif | |
59 | #ifdef TEST_FLOAT128 | |
60 | #include <boost/multiprecision/float128.hpp> | |
61 | #endif | |
62 | #ifdef TEST_CPP_BIN_FLOAT | |
63 | #include <boost/multiprecision/cpp_bin_float.hpp> | |
64 | #endif | |
65 | ||
66 | #ifdef BOOST_MSVC | |
92f5a8d4 | 67 | #pragma warning(disable : 4127) |
7c673cae FG |
68 | #endif |
69 | ||
92f5a8d4 | 70 | #define PRINT(x) \ |
7c673cae FG |
71 | std::cout << BOOST_STRINGIZE(x) << " = " << std::numeric_limits<Number>::x << std::endl; |
72 | ||
73 | template <class Number> | |
1e59de90 | 74 | void test_specific(const std::integral_constant<int, boost::multiprecision::number_kind_floating_point>&) |
7c673cae FG |
75 | { |
76 | Number minv, maxv; | |
77 | minv = (std::numeric_limits<Number>::min)(); | |
78 | maxv = (std::numeric_limits<Number>::max)(); | |
79 | BOOST_CHECK((boost::math::isnormal)(minv)); | |
80 | BOOST_CHECK((boost::math::isnormal)(maxv)); | |
81 | BOOST_CHECK((boost::math::isnormal)(log(minv))); | |
82 | BOOST_CHECK((boost::math::isnormal)(log(maxv))); | |
83 | BOOST_CHECK((boost::math::isnormal)(sqrt(minv))); | |
84 | BOOST_CHECK((boost::math::isnormal)(sqrt(maxv))); | |
85 | ||
92f5a8d4 | 86 | if (std::numeric_limits<Number>::is_specialized) |
7c673cae | 87 | { |
92f5a8d4 | 88 | if (std::numeric_limits<Number>::has_quiet_NaN) |
7c673cae FG |
89 | { |
90 | BOOST_TEST((boost::math::isnan)(std::numeric_limits<Number>::quiet_NaN())); | |
91 | BOOST_TEST(FP_NAN == (boost::math::fpclassify)(std::numeric_limits<Number>::quiet_NaN())); | |
92 | BOOST_TEST(!(boost::math::isfinite)(std::numeric_limits<Number>::quiet_NaN())); | |
93 | BOOST_TEST(!(boost::math::isnormal)(std::numeric_limits<Number>::quiet_NaN())); | |
94 | BOOST_TEST(!(boost::math::isinf)(std::numeric_limits<Number>::quiet_NaN())); | |
95 | } | |
92f5a8d4 | 96 | if (std::numeric_limits<Number>::has_signaling_NaN) |
7c673cae FG |
97 | { |
98 | BOOST_TEST((boost::math::isnan)(std::numeric_limits<Number>::signaling_NaN())); | |
99 | BOOST_TEST(FP_NAN == (boost::math::fpclassify)(std::numeric_limits<Number>::signaling_NaN())); | |
100 | BOOST_TEST(!(boost::math::isfinite)(std::numeric_limits<Number>::signaling_NaN())); | |
101 | BOOST_TEST(!(boost::math::isnormal)(std::numeric_limits<Number>::signaling_NaN())); | |
102 | BOOST_TEST(!(boost::math::isinf)(std::numeric_limits<Number>::signaling_NaN())); | |
103 | } | |
92f5a8d4 | 104 | if (std::numeric_limits<Number>::has_infinity) |
7c673cae FG |
105 | { |
106 | BOOST_TEST((boost::math::isinf)(std::numeric_limits<Number>::infinity())); | |
107 | BOOST_TEST(FP_INFINITE == (boost::math::fpclassify)(std::numeric_limits<Number>::infinity())); | |
108 | BOOST_TEST(!(boost::math::isfinite)(std::numeric_limits<Number>::infinity())); | |
109 | BOOST_TEST(!(boost::math::isnormal)(std::numeric_limits<Number>::infinity())); | |
110 | BOOST_TEST(!(boost::math::isnan)(std::numeric_limits<Number>::infinity())); | |
111 | } | |
92f5a8d4 | 112 | if (std::numeric_limits<Number>::has_denorm == std::denorm_present) |
7c673cae FG |
113 | { |
114 | BOOST_TEST(FP_SUBNORMAL == (boost::math::fpclassify)(std::numeric_limits<Number>::denorm_min())); | |
92f5a8d4 | 115 | BOOST_TEST(FP_SUBNORMAL == (boost::math::fpclassify)((std::numeric_limits<Number>::min)() / 2)); |
7c673cae FG |
116 | BOOST_TEST((boost::math::isfinite)(std::numeric_limits<Number>::denorm_min())); |
117 | BOOST_TEST(!(boost::math::isnormal)(std::numeric_limits<Number>::denorm_min())); | |
118 | BOOST_TEST(!(boost::math::isinf)(std::numeric_limits<Number>::denorm_min())); | |
119 | BOOST_TEST(!(boost::math::isnan)(std::numeric_limits<Number>::denorm_min())); | |
120 | BOOST_TEST(0 == std::numeric_limits<Number>::denorm_min() / 2); | |
92f5a8d4 | 121 | BOOST_TEST(0 != (std::numeric_limits<Number>::min)() / 2); |
7c673cae FG |
122 | BOOST_TEST(0 != std::numeric_limits<Number>::denorm_min()); |
123 | } | |
124 | } | |
125 | Number n = 0; | |
126 | BOOST_TEST((boost::math::fpclassify)(n) == FP_ZERO); | |
127 | BOOST_TEST((boost::math::isfinite)(n)); | |
128 | BOOST_TEST(!(boost::math::isnormal)(n)); | |
129 | BOOST_TEST(!(boost::math::isinf)(n)); | |
130 | BOOST_TEST(!(boost::math::isnan)(n)); | |
131 | n = 2; | |
132 | BOOST_TEST((boost::math::fpclassify)(n) == FP_NORMAL); | |
133 | BOOST_TEST((boost::math::isfinite)(n)); | |
134 | BOOST_TEST((boost::math::isnormal)(n)); | |
135 | BOOST_TEST(!(boost::math::isinf)(n)); | |
136 | BOOST_TEST(!(boost::math::isnan)(n)); | |
137 | ||
92f5a8d4 | 138 | if (std::numeric_limits<Number>::round_style == std::round_to_nearest) |
7c673cae FG |
139 | { |
140 | BOOST_CHECK_EQUAL(std::numeric_limits<Number>::round_error(), 0.5); | |
141 | } | |
92f5a8d4 | 142 | else if (std::numeric_limits<Number>::round_style != std::round_indeterminate) |
7c673cae FG |
143 | { |
144 | // Round error is 1.0: | |
145 | BOOST_CHECK_EQUAL(std::numeric_limits<Number>::round_error(), 1); | |
146 | } | |
147 | else | |
148 | { | |
149 | // Round error is presumably somewhere between 0.5 and 1: | |
150 | BOOST_CHECK((std::numeric_limits<Number>::round_error() <= 1) && (std::numeric_limits<Number>::round_error() >= 0.5)); | |
151 | } | |
152 | } | |
153 | ||
154 | template <class Number> | |
1e59de90 | 155 | void test_specific(const std::integral_constant<int, boost::multiprecision::number_kind_integer>&) |
7c673cae | 156 | { |
92f5a8d4 | 157 | if (std::numeric_limits<Number>::is_modulo) |
7c673cae | 158 | { |
92f5a8d4 | 159 | if (!std::numeric_limits<Number>::is_signed) |
7c673cae FG |
160 | { |
161 | BOOST_TEST(1 + (std::numeric_limits<Number>::max)() == 0); | |
162 | BOOST_TEST(--Number(0) == (std::numeric_limits<Number>::max)()); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | template <class Number, class T> | |
168 | void test_specific(const T&) | |
169 | { | |
170 | } | |
171 | ||
172 | template <class Number> | |
173 | void test() | |
174 | { | |
1e59de90 | 175 | typedef typename std::conditional< |
92f5a8d4 TL |
176 | std::numeric_limits<Number>::is_specialized, |
177 | typename boost::multiprecision::number_category<Number>::type, | |
1e59de90 | 178 | std::integral_constant<int, 500> // not a number type |
92f5a8d4 | 179 | >::type fp_test_type; |
7c673cae FG |
180 | |
181 | test_specific<Number>(fp_test_type()); | |
182 | ||
183 | // | |
184 | // Note really a test just yet, but we can at least print out all the values: | |
185 | // | |
186 | std::cout << "numeric_limits values for type " << typeid(Number).name() << std::endl; | |
187 | ||
188 | PRINT(is_specialized); | |
92f5a8d4 | 189 | if (std::numeric_limits<Number>::is_integer) |
7c673cae FG |
190 | { |
191 | std::cout << std::hex << std::showbase; | |
192 | } | |
92f5a8d4 TL |
193 | std::cout << "max()" |
194 | << " = " << (std::numeric_limits<Number>::max)() << std::endl; | |
195 | if (std::numeric_limits<Number>::is_integer) | |
7c673cae FG |
196 | { |
197 | std::cout << std::dec; | |
198 | } | |
92f5a8d4 TL |
199 | std::cout << "max()" |
200 | << " = " << (std::numeric_limits<Number>::max)() << std::endl; | |
201 | std::cout << "min()" | |
202 | << " = " << (std::numeric_limits<Number>::min)() << std::endl; | |
7c673cae | 203 | PRINT(lowest()); |
7c673cae FG |
204 | PRINT(digits); |
205 | PRINT(digits10); | |
7c673cae | 206 | PRINT(max_digits10); |
7c673cae FG |
207 | PRINT(is_signed); |
208 | PRINT(is_integer); | |
209 | PRINT(is_exact); | |
210 | PRINT(radix); | |
211 | PRINT(epsilon()); | |
212 | PRINT(round_error()); | |
213 | PRINT(min_exponent); | |
214 | PRINT(min_exponent10); | |
215 | PRINT(max_exponent); | |
216 | PRINT(max_exponent10); | |
217 | PRINT(has_infinity); | |
218 | PRINT(has_quiet_NaN); | |
219 | PRINT(has_signaling_NaN); | |
220 | PRINT(has_denorm); | |
221 | PRINT(has_denorm_loss); | |
222 | PRINT(infinity()); | |
223 | PRINT(quiet_NaN()); | |
224 | PRINT(signaling_NaN()); | |
225 | PRINT(denorm_min()); | |
226 | PRINT(is_iec559); | |
227 | PRINT(is_bounded); | |
228 | PRINT(is_modulo); | |
229 | PRINT(traps); | |
230 | PRINT(tinyness_before); | |
231 | PRINT(round_style); | |
232 | } | |
233 | ||
7c673cae FG |
234 | int main() |
235 | { | |
236 | #ifdef TEST_BACKEND | |
237 | test<boost::multiprecision::number<boost::multiprecision::concepts::number_backend_float_architype> >(); | |
238 | #endif | |
239 | #ifdef TEST_MPF_50 | |
240 | test<boost::multiprecision::mpf_float_50>(); | |
241 | #endif | |
242 | #ifdef TEST_MPF | |
243 | boost::multiprecision::mpf_float::default_precision(1000); | |
244 | /* | |
245 | boost::multiprecision::mpf_float r; | |
246 | r.precision(50); | |
247 | BOOST_TEST(r.precision() >= 50); | |
248 | */ | |
249 | BOOST_TEST(boost::multiprecision::mpf_float::default_precision() == 1000); | |
250 | test<boost::multiprecision::mpf_float>(); | |
251 | #endif | |
252 | #ifdef TEST_MPZ | |
253 | test<boost::multiprecision::mpz_int>(); | |
254 | #endif | |
255 | #ifdef TEST_MPQ | |
256 | test<boost::multiprecision::mpq_rational>(); | |
257 | #endif | |
258 | #ifdef TEST_CPP_DEC_FLOAT | |
259 | test<boost::multiprecision::cpp_dec_float_50>(); | |
260 | test<boost::multiprecision::cpp_dec_float_100>(); | |
261 | #endif | |
262 | #ifdef TEST_MPFR | |
263 | test<boost::multiprecision::mpfr_float>(); | |
264 | #endif | |
265 | #ifdef TEST_MPFR_50 | |
266 | test<boost::multiprecision::mpfr_float_50>(); | |
267 | #endif | |
268 | #ifdef TEST_MPFI_50 | |
269 | test<boost::multiprecision::mpfi_float_50>(); | |
270 | test<boost::multiprecision::mpfi_float>(); | |
271 | #endif | |
272 | #ifdef TEST_TOMMATH | |
273 | test<boost::multiprecision::tom_int>(); | |
274 | #endif | |
275 | #ifdef TEST_CPP_INT | |
276 | test<boost::multiprecision::cpp_int>(); | |
277 | test<boost::multiprecision::int256_t>(); | |
278 | test<boost::multiprecision::uint512_t>(); | |
279 | test<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<200, 200, boost::multiprecision::unsigned_magnitude, boost::multiprecision::checked, void> > >(); | |
280 | test<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<70, 70, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> > >(); | |
281 | #endif | |
282 | #ifdef TEST_FLOAT128 | |
283 | test<boost::multiprecision::float128>(); | |
284 | #endif | |
285 | #ifdef TEST_CPP_BIN_FLOAT | |
286 | test<boost::multiprecision::cpp_bin_float_50>(); | |
287 | #endif | |
288 | return boost::report_errors(); | |
289 | } |