1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5
6 //
7 // Compare arithmetic results using fixed_int to GMP results.
8 //
9
10 #ifdef _MSC_VER
11 #define _SCL_SECURE_NO_WARNINGS
12 #endif
13
14 #include <boost/multiprecision/cpp_int.hpp>
15 #include "test.hpp"
16
17 template <class Number, class BigNumber>
test()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
33 if (std::numeric_limits<test_type>::is_signed)
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 //
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 };
54 typedef typename boost::uint_t<require_digits>::least uint_least;
55 typedef typename boost::int_t<require_digits>::least int_least;
56 typedef typename boost::mpl::if_c<std::numeric_limits<test_type>::is_signed, int_least, uint_least>::type i_type;
57
58 i_type ih = (std::numeric_limits<i_type>::max)();
59 i_type il = (std::numeric_limits<i_type>::max)();
60
61 add(r, ih, ih);
62 BOOST_CHECK_EQUAL(r, cpp_int(ih) + cpp_int(ih));
63
64 multiply(r, ih, ih);
65 BOOST_CHECK_EQUAL(r, cpp_int(ih) * cpp_int(ih));
66
67 if (std::numeric_limits<test_type>::is_signed)
68 {
69 subtract(r, il, ih);
70 BOOST_CHECK_EQUAL(r, cpp_int(il) - cpp_int(ih));
71 subtract(r, ih, il);
72 BOOST_CHECK_EQUAL(r, cpp_int(ih) - cpp_int(il));
73 multiply(r, il, il);
74 BOOST_CHECK_EQUAL(r, cpp_int(il) * cpp_int(il));
75 }
76 }
77
test_rational_mixed()78 void test_rational_mixed()
79 {
80 using namespace boost::multiprecision;
81 cpp_int a(2);
82 cpp_rational r(10);
83
84 BOOST_CHECK_EQUAL(a + -r, -8);
85 BOOST_CHECK_EQUAL(-r + a, -8);
86 BOOST_CHECK_EQUAL(-a + r, 8);
87 BOOST_CHECK_EQUAL(r + -a, 8);
88
89 BOOST_CHECK_EQUAL(a - -r, 12);
90 BOOST_CHECK_EQUAL(-r - a, -12);
91 BOOST_CHECK_EQUAL(-a - r, -12);
92 BOOST_CHECK_EQUAL(r - -a, 12);
93
94 BOOST_CHECK_EQUAL(a * -r, -20);
95 BOOST_CHECK_EQUAL(-r * a, -20);
96 BOOST_CHECK_EQUAL(-a * r, -20);
97 BOOST_CHECK_EQUAL(r * -a, -20);
98
99 BOOST_CHECK_EQUAL(a / -r, cpp_rational(-2, 10));
100 BOOST_CHECK_EQUAL(-r / a, -5);
101 BOOST_CHECK_EQUAL(cpp_rational(-a / r), cpp_rational(-2, 10));
102 BOOST_CHECK_EQUAL(r / -a, -5);
103 }
104
main()105 int main()
106 {
107 using namespace boost::multiprecision;
108
109 test_rational_mixed();
110
111 test<checked_int512_t, checked_int1024_t>();
112 test<checked_int256_t, checked_int512_t>();
113 test<number<cpp_int_backend<64, 64, signed_magnitude, checked, void>, et_off>, checked_int128_t>();
114 test<boost::int64_t, checked_int128_t>();
115
116 test<checked_uint512_t, checked_uint1024_t>();
117 test<checked_uint256_t, checked_uint512_t>();
118 test<number<cpp_int_backend<64, 64, unsigned_magnitude, checked, void>, et_off>, checked_uint128_t>();
119 test<boost::uint64_t, checked_int128_t>();
120
121 return boost::report_errors();
122 }
123