forked from dfyning/bignumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sldecimal.cpp
417 lines (370 loc) · 13.1 KB
/
sldecimal.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include"bignumber.h"
void sldecimal::setprecision(uint32 precision) { // 精度调整
if (this->pointer > static_cast<long long>(precision & 0x8fffffff)) {
this->integer = (this->integer >> (this->pointer - precision));
this->pointer = precision;
}
this->precision = precision;
}
void sldecimal::currectsetting(const std::string & strnum) {
uint32 positionofpoint = strnum.find('.'); // 小数点所在位置
if (positionofpoint == std::string::npos) { // 无小数部分按整形处理
this->integer = biginteger(strnum);
this->pointer = 0;
}
else {
std::string::const_iterator start = strnum.begin();
std::string newstr, working;
if (*start != '+' && *start != '-') {// 处理输入字符串无正负及进制信息情况
newstr += '+';
if (strnum.size() > 2) {
start += 2;
if (*start != 'x' && *start != 'X' && *start != 'b' && *start != 't') {
newstr += "0x";;
}
}
else {
newstr += "0x";
}
working = newstr + strnum;
}
else if (strnum.size() > 3) {
if (*(start + 2) != 'x' && *(start + 2) != 'X' && *(start + 2) != 'b' && *(start + 2) != 't') {
newstr = "0x";
working = strnum[0] + newstr + working;
}
working = strnum;
}
int whichsys = 16; // 默认进制为16
std::string::const_iterator strindex = working.begin();
if ((*strindex == '+') | (*strindex == '-')) { // 忽略正负问题
strindex++;
}
if ((*strindex == '0') && *(strindex + 1) == 'x') {
whichsys = 16;
strindex += 2;
}
if (((*strindex) == '0') && *(strindex + 1) == 'b') {
whichsys = 2;
strindex += 2;
}
if ((*strindex == '0') && *(strindex + 1) == 'X') {
whichsys = 8;
strindex += 2;
}
if ((*strindex == '0') && *(strindex + 1) == 't') { // 十进制非常麻烦, 需要另外处理
std::string::iterator beg = working.begin();
sldecimal answer = biginteger(std::string(beg, beg + positionofpoint));
biginteger ten("+0xa");
sldecimal index = sldecimal("+0x1") / ten;
beg = beg + positionofpoint + 1;
if (answer.integer.ispositive()) {
while (beg != working.end()) {
answer = answer + index * int(*beg - '0');
index = index / ten;
beg++;
}
}
else {
while (beg != working.end()) {
answer = answer - index * (*beg - '0');
index = index / 10;
beg++;
}
}
answer.integer.delete0intop();
this->integer = answer.integer;
this->pointer = answer.pointer;
this->precision = answer.precision;
return;
}
std::string::const_iterator iter = working.begin();
std::string sys2;
sys2 = std::string((*iter == '+') ? "+" : "-") + "0b";
intotwo(working, whichsys, sys2);
positionofpoint = sys2.find('.'); // 新的二进制数的小数点位置
std::string::iterator iterofpoint = sys2.begin() + static_cast<int>(positionofpoint);
std::string newstrfordecimal(iterofpoint + 1, sys2.end());
if ((newstrfordecimal.length() % 32)) {// 初始化小数部分的字符串 长度必须为32整数倍 以便对齐
size_t dddd = 32 - (newstrfordecimal.length() % 32);
std::string ing(dddd, '0');
newstrfordecimal += ing;
}
std::string::iterator beg = sys2.begin();
this->integer = biginteger(std::string(beg, iterofpoint) + newstrfordecimal);
this->pointer = newstrfordecimal.length() / 32;
this->integer.delete0intop();
}
}
sldecimal::sldecimal() {
this->integer = biginteger();
this->pointer = 0;
}
sldecimal::sldecimal(const std::string & strnum, uint32 numofdecimal) {
this->precision = numofdecimal;
this->currectsetting(strnum);
if (this->pointer > static_cast<long long>(numofdecimal & 0x8fffffffffffffff)) {
this->integer = this->integer >> (this->pointer - numofdecimal);
this->pointer = numofdecimal;
}
}
sldecimal::sldecimal(const std::string & strnum){
this->precision = sldecimal_precision;
this->currectsetting(strnum);
if (this->pointer > sldecimal_precision) {
this->integer = this->integer >> (this->pointer - sldecimal_precision);
this->pointer = 16;
}
}
sldecimal::sldecimal(const biginteger& integer) {
this->integer = integer;
this->pointer = 0;
this->precision = sldecimal_precision;
}
sldecimal::sldecimal(const biginteger& integer, uint32 numofdecimal) {
this->precision = numofdecimal;
this->integer = integer;
this->pointer = numofdecimal;
this->precision = numofdecimal;
}
sldecimal::sldecimal(const sldecimal& slnum){
this->precision = slnum.precision;
this->integer = slnum.integer;
this->pointer = slnum.pointer;
}
sldecimal::sldecimal(const sldecimal& slnum, uint32 numofdecimal) {
if (slnum.pointer <= static_cast<long long>(numofdecimal & 0x8fffffffffffffff)) {
this->integer = slnum.integer;
this->pointer = slnum.pointer;
this->precision = slnum.precision;
}
else {
sldecimal once(slnum);
this->integer = once.integer >> (slnum.pointer - numofdecimal);
this->pointer = numofdecimal;
this->precision = numofdecimal;
}
}
biginteger sldecimal::getinteger() const{
if (this->pointer == 0) return this->integer;
else if (this->pointer > 0) return (this->integer >> (this->pointer));
else return (this->integer << ( 0 - this->pointer));
}
sldecimal sldecimal::getdecimal() const {
return (*this - this->getinteger());
}
sldecimal sldecimal::oppositenumber() const{
sldecimal answer(*this);
if (answer.integer == 0) return answer;
answer.integer = answer.integer.oppositenumber();
return answer;
}
sldecimal sldecimal::abs() const {
sldecimal answer(*this);
if (answer.integer == 0) return answer;
answer.integer = answer.integer.abs();
return answer;
}
bool operator==(const sldecimal & number, const sldecimal & number1) {
if (number.integer != number1.integer) return false;
if (number.pointer != number1.pointer) return false;
return true;
}
bool operator!=(const sldecimal & number, const sldecimal & number1) {
if (number == number1) return false;
return true;
}
bool operator>(const sldecimal & number, const sldecimal & number1) {
if (number.integer.ispositive() == 1 && number1.integer.ispositive() == 0) return true;
if (number.integer.ispositive() == 0 && number1.integer.ispositive() == 1) return false;
int longerdecimal = max(number.pointer, number1.pointer); // 对齐 此时无需考虑正负
biginteger copy, copy1;
copy = number.integer << (longerdecimal - number.pointer);
copy1 = number1.integer << (longerdecimal - number1.pointer);
if (copy > copy1) return true;
return false;
}
bool operator>=(const sldecimal & number, const sldecimal & number1) {
if ((number > number1) | (number == number1)) return true;
return false;
}
bool operator<(const sldecimal & number, const sldecimal & number1) {
if (number1 > number) return true;
return false;
}
bool operator<=(const sldecimal & number, const sldecimal & number1) {
if ((number1 > number) | (number1 == number)) return true;
return false;
}
bool operator==(const sldecimal & number, const biginteger & number1) {
if (number == sldecimal(number1)) return true;
return false;
}
bool operator==(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) == number1) return true;
return false;
}
bool operator!=(const sldecimal & number, const biginteger & number1) {
if (number == sldecimal(number1)) return false;
return true;
}
bool operator!=(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) == number1) return false;
return true;
}
bool operator>(const sldecimal & number, const biginteger & number1) {
if (number > sldecimal(number1)) return true;
return false;
}
bool operator>(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) > number1) return true;
return false;
}
bool operator>=(const sldecimal & number, const biginteger & number1) {
if (number >= sldecimal(number1)) return true;
return false;
}
bool operator>=(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) >= number1) return true;
return false;
}
bool operator<(const sldecimal & number, const biginteger & number1) {
if (number < sldecimal(number1)) return true;
return false;
}
bool operator<(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) < number1) return true;
return false;
}
bool operator<=(const sldecimal & number, const biginteger & number1) {
if (number <= sldecimal(number1)) return true;
return false;
}
bool operator<=(const biginteger & number, const sldecimal & number1) {
if (sldecimal(number) <= number1) return true;
return false;
}
sldecimal operator+(const sldecimal & number, const sldecimal & number1) {
sldecimal answer;
if (number.pointer < number1.pointer) {
answer.integer = biginteger((number.integer << (number1.pointer - number.pointer)) + number1.integer);
answer.pointer = number1.pointer;
}
else if (number.pointer > number1.pointer) {
biginteger copy(number1.integer);
answer.integer = biginteger((copy << (number.pointer - number1.pointer)) + number.integer);
answer.pointer = number.pointer;
}
else {
answer.integer = (number.integer + number1.integer);
answer.pointer = number.pointer;
}
answer.delete0inbottom();
return answer;
}
sldecimal operator-(const sldecimal & number, const sldecimal & number1) {
if (number1 == 0) return number;
if (number == number1) return sldecimal();
sldecimal answer;
if (number.pointer < number1.pointer) {
answer.integer = biginteger((number.integer << (number1.pointer - number.pointer)) - number1.integer);
answer.pointer = number1.pointer;
}
else if (number.pointer > number1.pointer) {
answer.integer = biginteger(number.integer - (number1.integer << (number.pointer - number1.pointer)));
answer.pointer = number.pointer;
}
else {
answer.integer = (number.integer - number1.integer);
answer.pointer = number.pointer;
}
answer.delete0inbottom();
return answer;
}
sldecimal operator*(const sldecimal & number, const sldecimal & number1) {
sldecimal answer;
answer.integer = number.integer * number1.integer;
if (number.pointer + number1.pointer > sldecimal_precision) {
answer.pointer = sldecimal_precision;
answer.precision = sldecimal_precision;
answer.integer = answer.integer >> (number.pointer + number1.pointer - sldecimal_precision);
return answer;
}
answer.pointer = number.pointer + number1.pointer;
answer.precision = sldecimal_precision;
answer.delete0inbottom();
return answer;
}
sldecimal operator/(const sldecimal & number, const sldecimal & number1) {
sldecimal answer;
answer.integer = number.integer / number1.integer;
answer.pointer = number.pointer - number1.pointer;
biginteger remainder = number.integer % number1.integer;
while (remainder != 0 && answer.pointer < sldecimal_precision) { // 精度需求未达到且余数不为零
remainder = remainder << 1;
answer.integer = (answer.integer << 1) + remainder / number1.integer;
remainder = remainder % number1.integer;
answer.pointer += 1;
}
answer.precision = sldecimal_precision;
answer.integer.delete0intop();
answer.delete0inbottom();
return answer;
}
sldecimal operator+(const biginteger & number, const sldecimal & number1) {
return sldecimal(number) + number1;
}
sldecimal operator+(const sldecimal & number, const biginteger & number1) {
return number + sldecimal(number1);
}
sldecimal operator-(const biginteger & number, const sldecimal & number1) {
return sldecimal(number) - number1;
}
sldecimal operator-(const sldecimal & number, const biginteger & number1) {
return number - sldecimal(number1);
}
sldecimal operator*(const biginteger & number, const sldecimal & number1) {
return sldecimal(number) * number1;
}
sldecimal operator*(const sldecimal & number, const biginteger & number1) {
return number * sldecimal(number1);
}
sldecimal operator/(const biginteger & number, const sldecimal & number1) {
return sldecimal(number) / number1;
}
sldecimal operator/(const sldecimal & number, const biginteger & number1) {
return number / sldecimal(number1);
}
sldecimal sldecimal::operator<<(const uint32 leftlen) const {
sldecimal answer(*this);
answer.pointer = answer.pointer - leftlen;
return answer;
}
sldecimal sldecimal::operator>>(const uint32 rightlen) const {
sldecimal answer(*this);
answer.pointer = answer.pointer + rightlen;
return answer;
}
void sldecimal::delete0inbottom() {
uint32 numsof0;
numsof0 = this->integer.delete0inbottom();
this->pointer -= numsof0;
}
std::string sldecimal::output(uint32 index) {
std::string outforint = (this->getinteger().output(index));
sldecimal onlydecimal = (*this - this->getinteger());
std::string strofdecimal;
std::string listofchar = "0123456789abcdef";
while ((strofdecimal.length() < sldecimal_precision * index) && (onlydecimal != 0)) {
onlydecimal = onlydecimal * index;
int s = onlydecimal.getinteger().lastnumber();
strofdecimal += listofchar[onlydecimal.getinteger().lastnumber()];
onlydecimal = onlydecimal.getdecimal();
}
return outforint + "." + strofdecimal;
}
void sldecimal::show() {
std::cout << "小数点在" << this->pointer << std::endl;
std::cout << "精度为" << this->precision << std::endl;
this->integer.show();
}