1 ///////////////////////////////////////////////////////////////
2 // Copyright Christopher Kormanyos 2002 - 2011.
3 // Copyright 2011 John Maddock. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6 //
7 // This work is based on an earlier work:
8 // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
9 // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
10
11 #ifdef _MSC_VER
12 #define _SCL_SECURE_NO_WARNINGS
13 #endif
14
15 #include <boost/detail/lightweight_test.hpp>
16 #include "test.hpp"
17
18 #include <boost/multiprecision/gmp.hpp>
19
20 template <class T>
make_rvalue_copy(const T a)21 T make_rvalue_copy(const T a)
22 {
23 return a;
24 }
25
main()26 int main()
27 {
28 using namespace boost::multiprecision;
29 //
30 // Test change of default precision:
31 //
32 mpf_float::default_precision(100);
33 mpf_float a("0.1");
34 BOOST_CHECK_GE(a.precision(), 100);
35 mpf_float::default_precision(20);
36 {
37 // test assignment from lvalue:
38 mpf_float b(2);
39 BOOST_CHECK_GE(b.precision(), 20);
40 b = a;
41 BOOST_CHECK_EQUAL(b.precision(), a.precision());
42 }
43 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
44 {
45 // test assignment from rvalue:
46 mpf_float b(2);
47 BOOST_CHECK_GE(b.precision(), 20);
48 b = make_rvalue_copy(a);
49 BOOST_CHECK_EQUAL(b.precision(), a.precision());
50 }
51 #endif
52 mpf_float::default_precision(20);
53 {
54 // test construct from lvalue:
55 mpf_float b(a);
56 BOOST_CHECK_EQUAL(b.precision(), a.precision());
57 }
58 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
59 {
60 // test construct from rvalue:
61 mpf_float b(make_rvalue_copy(a));
62 BOOST_CHECK_EQUAL(b.precision(), a.precision());
63 }
64 #endif
65 {
66 mpf_float f150(2, 150);
67 BOOST_CHECK_GE(f150.precision(), 150);
68 }
69 {
70 mpf_float f150("1.2", 150);
71 BOOST_CHECK_GE(f150.precision(), 150);
72 }
73 //
74 // From https://github.com/boostorg/multiprecision/issues/65
75 //
76 {
77 mpf_float a(2);
78 a.precision(100);
79 BOOST_CHECK_EQUAL(a, 2);
80 BOOST_CHECK_GE(a.precision(), 100);
81 }
82
83 //
84 // string_view with explicit precision:
85 //
86 #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
87 {
88 std::string s("222");
89 std::string_view v(s.c_str(), 1);
90 mpf_float f(v, 100);
91 BOOST_CHECK_EQUAL(f, 2);
92 BOOST_CHECK_GE(f.precision(), 100);
93 }
94 #endif
95 // Swap:
96 {
97 mpf_float x(2, 100); // 100 digits precision.
98 mpf_float y(3, 50); // 50 digits precision.
99 swap(x, y);
100 BOOST_CHECK_EQUAL(x, 3);
101 BOOST_CHECK_EQUAL(y, 2);
102 BOOST_CHECK_GE(x.precision(), 50);
103 BOOST_CHECK_GE(y.precision(), 100);
104 x.swap(y);
105 BOOST_CHECK_EQUAL(x, 2);
106 BOOST_CHECK_EQUAL(y, 3);
107 BOOST_CHECK_GE(x.precision(), 100);
108 BOOST_CHECK_GE(y.precision(), 50);
109 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
110 x = std::move(mpf_float(y));
111 BOOST_CHECK_EQUAL(x, y);
112 BOOST_CHECK_EQUAL(x.precision(), y.precision());
113 #endif
114 }
115 {
116 mpf_float c(4), d(8), e(9), f;
117 f = (c + d) * d / e;
118 mpf_float g((c + d) * d / e);
119 }
120 {
121 mpf_float::default_precision(100);
122 mpf_float f1;
123 f1 = 3;
124 BOOST_CHECK_GE(f1.precision(), 100);
125 f1 = 3.5;
126 BOOST_CHECK_GE(f1.precision(), 100);
127 mpf_float f2(3.5);
128 BOOST_CHECK_GE(f2.precision(), 100);
129 mpf_float f3("5.1");
130 BOOST_CHECK_GE(f3.precision(), 100);
131
132 mpf_float::default_precision(50);
133 mpf_float f4(f3, 50);
134 BOOST_CHECK_GE(f4.precision(), 50);
135 f4.assign(f1, f4.precision());
136 BOOST_CHECK_GE(f4.precision(), 50);
137 }
138
139 return boost::report_errors();
140 }
141