-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReal.cpp
157 lines (142 loc) · 3.2 KB
/
Real.cpp
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
#include "Long.h"
#include "Real.h"
const char* Real::delim_mant = ".";
std::ostream & operator<<(std::ostream & stream, Real b) {
stream << b.operator std::string();
return stream;
}
Real::operator std::string() const {
std::string s = "";
if (!sign) s += "-";
int i = size() - 1, j = real_size();
int q = mantisa_place;
if (q >= j) {
--q;
s += "0" + std::string(delim_mant);
while (q-- > j)
s += "0";
while (i >= 0)
s += my_to_string(a[i--], base);
}
else {
s += std::to_string(a[i--]);
while (i >= 0) {
if (j > mantisa_place)
for (auto tmp = 0; tmp < bs; ++tmp) {
if (j - tmp == mantisa_place)
s += std::string(delim_mant);
}
else
j -= bs;
s += my_to_string(a[i--], base);
}
}
return s;
}
Real Real::normalmant()
{
if (mantisa_place == 0)
return *this;
int bs = std::log10(base);
vector<ull> vec;
int j = 0;
if (a[0] == 0) {
do {
++j;
} while (j < size() && a[j] == 0);
while (j < size())
vec.push_back(a[j++]);
a = vec;
return normalmant();
}
return *this;
}
Real Real::cut(uint t)
{
if (size() >= t) {
mantisa_place -= (size() - t) * bs;
a.erase(a.begin(), a.end() - t);
}
return *this;
}
Real::Real(const Long & p1, uint m) : Long(p1), mantisa_place(m) {
normalmant();
}
Real Real::operator+(const Real & o) const
{
bool mmant = mantisa_place > o.mantisa_place;
Long p1 = (mmant) ? *this : o;
Long p2 = (mmant) ? o : *this;
auto m1 = mmant ? mantisa_place : o.mantisa_place;
auto m2 = mmant ? o.mantisa_place : mantisa_place;
p1 = p1 + p2.shift(m1 - m2);
Real p3 = static_cast<Real>(p1);
p3.mantisa_place = m1;
return p3.normalmant();
}
Real Real::operator-(const Real & o) const {
bool mmant = mantisa_place > o.mantisa_place;
Long p1 = (mmant) ? *this : o;
Long p2 = (mmant) ? o : *this;
auto m1 = mmant ? mantisa_place : o.mantisa_place;
auto m2 = mmant ? o.mantisa_place : mantisa_place;
int bs = std::log10(base);
auto t10 = 0; auto t = p2[0];
while (t % 10 == 0) {
++t10; t = t / 10;
}
int shifting = (m1 - m2 + t10);
p1 = mmant ? (p1 - p2.shiftback(shifting)) : (p2.shiftback(shifting) - p1);
Real p3 = static_cast<Real>(p1);
p3.mantisa_place = m1; // x(n+1) = xn(2 - b * xn)
return p3.normalmant();
}
Real Real::operator*(const Real & o) const {
Long p1 = Long(*this) * Long(o);
Real p2 = static_cast<Real>(p1);
p2.mantisa_place = mantisa_place + o.mantisa_place;
return p2.normalmant();
}
Real::Real(const Real & o) : Long(o)
{
operator=(o);
}
Real::Real(Real && o)
{
operator=(o);
}
Real & Real::operator=(const Real & o)
{
mantisa_place = o.mantisa_place;
clear();
for (auto i : o.a) {
a.push_back(i);
}
sign = o.sign;
mantisa_place = o.mantisa_place;
return *this;
}
Real & Real::operator=(Real && o) {
clear();
for (auto i : o.a) {
a.push_back(i);
}
sign = o.sign;
mantisa_place = o.mantisa_place;
o.clear();
return *this;
}
Long to_Long(const Real & a)
{
vector<ull> c;
int i = a.real_size(), j = a.get_mant(), k = a.size() - 1;
int tmp = 0;
while (k > 0 && i > j) {
c.push_back(a[k]);
i -= (a[k] == 0) ? std::log10(a[k]) + 1 : Long::bs;
--k;
}
for (int d = 0; d < c.size() / 2; ++d)
std::swap(c[d], c[c.size() - d - 1]);
return Long(c);// / std::pow(10, j - i - tmp);
}