]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////// |
2 | // Copyright 2012 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 | // | |
7 | // Compare arithmetic results using fixed_int to GMP results. | |
8 | // | |
9 | ||
10 | #ifdef _MSC_VER | |
92f5a8d4 | 11 | #define _SCL_SECURE_NO_WARNINGS |
7c673cae FG |
12 | #endif |
13 | ||
14 | #include <boost/multiprecision/cpp_int.hpp> | |
15 | #include "test.hpp" | |
16 | ||
17 | template <class Number, class BigNumber> | |
18 | void test() | |
19 | { | |
20 | using namespace boost::multiprecision; | |
21 | typedef Number test_type; | |
22 | ||
23 | test_type h = (std::numeric_limits<test_type>::max)(); | |
24 | test_type l = (std::numeric_limits<test_type>::max)(); | |
25 | BigNumber r; | |
26 | ||
27 | add(r, h, h); | |
28 | BOOST_CHECK_EQUAL(r, cpp_int(h) + cpp_int(h)); | |
29 | ||
30 | multiply(r, h, h); | |
31 | BOOST_CHECK_EQUAL(r, cpp_int(h) * cpp_int(h)); | |
32 | ||
92f5a8d4 | 33 | if (std::numeric_limits<test_type>::is_signed) |
7c673cae FG |
34 | { |
35 | subtract(r, l, h); | |
36 | BOOST_CHECK_EQUAL(r, cpp_int(l) - cpp_int(h)); | |
37 | subtract(r, h, l); | |
38 | BOOST_CHECK_EQUAL(r, cpp_int(h) - cpp_int(l)); | |
39 | multiply(r, l, l); | |
40 | BOOST_CHECK_EQUAL(r, cpp_int(l) * cpp_int(l)); | |
41 | } | |
42 | ||
43 | // | |
44 | // Try again with integer types as the source: | |
45 | // | |
92f5a8d4 TL |
46 | enum |
47 | { | |
48 | max_digits = std::numeric_limits<test_type>::is_signed ? std::numeric_limits<long long>::digits : std::numeric_limits<unsigned long long>::digits | |
49 | }; | |
50 | enum | |
51 | { | |
52 | require_digits = std::numeric_limits<test_type>::digits <= 2 * max_digits ? std::numeric_limits<test_type>::digits / 2 : max_digits | |
53 | }; | |
1e59de90 TL |
54 | |
55 | using uint_least = typename boost::multiprecision::detail::uint_t<require_digits>::least; | |
56 | using int_least = typename boost::multiprecision::detail::int_t<require_digits>::least; | |
57 | using i_type = typename std::conditional<std::numeric_limits<test_type>::is_signed, int_least, uint_least>::type; | |
7c673cae FG |
58 | |
59 | i_type ih = (std::numeric_limits<i_type>::max)(); | |
60 | i_type il = (std::numeric_limits<i_type>::max)(); | |
61 | ||
62 | add(r, ih, ih); | |
63 | BOOST_CHECK_EQUAL(r, cpp_int(ih) + cpp_int(ih)); | |
64 | ||
65 | multiply(r, ih, ih); | |
66 | BOOST_CHECK_EQUAL(r, cpp_int(ih) * cpp_int(ih)); | |
67 | ||
92f5a8d4 | 68 | if (std::numeric_limits<test_type>::is_signed) |
7c673cae FG |
69 | { |
70 | subtract(r, il, ih); | |
71 | BOOST_CHECK_EQUAL(r, cpp_int(il) - cpp_int(ih)); | |
72 | subtract(r, ih, il); | |
73 | BOOST_CHECK_EQUAL(r, cpp_int(ih) - cpp_int(il)); | |
74 | multiply(r, il, il); | |
75 | BOOST_CHECK_EQUAL(r, cpp_int(il) * cpp_int(il)); | |
76 | } | |
77 | } | |
78 | ||
79 | void test_rational_mixed() | |
80 | { | |
81 | using namespace boost::multiprecision; | |
92f5a8d4 | 82 | cpp_int a(2); |
7c673cae FG |
83 | cpp_rational r(10); |
84 | ||
85 | BOOST_CHECK_EQUAL(a + -r, -8); | |
86 | BOOST_CHECK_EQUAL(-r + a, -8); | |
87 | BOOST_CHECK_EQUAL(-a + r, 8); | |
88 | BOOST_CHECK_EQUAL(r + -a, 8); | |
89 | ||
90 | BOOST_CHECK_EQUAL(a - -r, 12); | |
91 | BOOST_CHECK_EQUAL(-r - a, -12); | |
92 | BOOST_CHECK_EQUAL(-a - r, -12); | |
93 | BOOST_CHECK_EQUAL(r - -a, 12); | |
94 | ||
95 | BOOST_CHECK_EQUAL(a * -r, -20); | |
96 | BOOST_CHECK_EQUAL(-r * a, -20); | |
97 | BOOST_CHECK_EQUAL(-a * r, -20); | |
98 | BOOST_CHECK_EQUAL(r * -a, -20); | |
99 | ||
100 | BOOST_CHECK_EQUAL(a / -r, cpp_rational(-2, 10)); | |
101 | BOOST_CHECK_EQUAL(-r / a, -5); | |
102 | BOOST_CHECK_EQUAL(cpp_rational(-a / r), cpp_rational(-2, 10)); | |
103 | BOOST_CHECK_EQUAL(r / -a, -5); | |
104 | } | |
105 | ||
106 | int main() | |
107 | { | |
108 | using namespace boost::multiprecision; | |
109 | ||
110 | test_rational_mixed(); | |
111 | ||
112 | test<checked_int512_t, checked_int1024_t>(); | |
113 | test<checked_int256_t, checked_int512_t>(); | |
114 | test<number<cpp_int_backend<64, 64, signed_magnitude, checked, void>, et_off>, checked_int128_t>(); | |
1e59de90 TL |
115 | test<std::int64_t, checked_int128_t>(); |
116 | test<number<cpp_int_backend<4096, 4096, signed_magnitude, checked, void>, et_off>, cpp_int>(); | |
117 | test<number<cpp_int_backend<4096> >, cpp_int>(); | |
7c673cae FG |
118 | |
119 | test<checked_uint512_t, checked_uint1024_t>(); | |
120 | test<checked_uint256_t, checked_uint512_t>(); | |
121 | test<number<cpp_int_backend<64, 64, unsigned_magnitude, checked, void>, et_off>, checked_uint128_t>(); | |
1e59de90 | 122 | test<std::uint64_t, checked_int128_t>(); |
7c673cae FG |
123 | |
124 | return boost::report_errors(); | |
125 | } |